4

I have developed a Email service using Spring Java mail and Velocity Template like below.

Email.java

@Component
public class Email {    

        private JavaMailSender mailSender;      
        private VelocityEngine velocityEngine;  


         @Autowired
        private ApplReviewService applReviewService;

       @Autowired
        private UserService userService;


        public void setUserService(UserService userService ) {
            this.userService=userService;
        }


        public UserService getuserService() {
            return userService;
        }

        @Autowired
        @Required
        public void setMailSender(JavaMailSender mailSender) {
            this.mailSender = mailSender;
        }

        public VelocityEngine getVelocityEngine() {
            return velocityEngine;
        }

        @Autowired
        @Required
        public void setVelocityEngine(VelocityEngine velocityEngine) {
            this.velocityEngine = velocityEngine;
        }

// Method to send Email. }

My Spring.xml

<context:component-scan base-package="com.test.common"/>

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
           </bean>

   <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
      <property name="velocityProperties">
         <value>
            resource.loader=class
            class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
         </value>
      </property>
   </bean>


@ManagedBean(name="person")
@SessionScoped
Public class Person{

@Autowired
private Email email ; // getter and setter for this.

}

I am trying autowire my Email class into Jsf managedBean but I am getting null pointer exception. Where I am going wrong.

mdp
  • 815
  • 4
  • 17
  • 32

1 Answers1

7

You cannot inject a Spring bean like that in a JSF managed bean. Change it to

@ManagedBean(name="person")
@SessionScoped
Public class Person{

@ManagedProperty(value="#{email}")
private Email email ; // getter and setter for this.

}

See also:

Community
  • 1
  • 1
Ravi Kadaboina
  • 8,494
  • 3
  • 30
  • 42
  • @Ravi.....I tried the above answer but it's not working.I am still getting the null pointer.Can you clarify me one thing.I have defined the Email bean in Spring.xml which is the main/java/resources/config folder.Is this a problem.Do I need to specifically define it in ApplicationContext.xml which is WEB-INF folder? – mdp Oct 12 '12 at 03:21
  • It doesn't matter where you put your xml files as long as you mention in your contextConfigLocation of your web.xml though the default location being /WEB-INF/applicationContext.xml. See also [http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/context/ContextLoader.html](http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/context/ContextLoader.html). How did you specify in your web.xml? Did you also add org.springframework.web.jsf.el.SpringBeanFacesELResolver in your faces-config.xml – Ravi Kadaboina Oct 12 '12 at 04:12
  • Hey Thanks Ravi.....YOur suggeestion gave clear Idea about loading of spring beans into application.Now I am able to make my application working and got clear picture about the loading of spring beans. – mdp Oct 12 '12 at 04:29
  • One more question.Once my start my application,ApplicationContext is loading six times into my application.How can stop that? – mdp Oct 12 '12 at 05:20
  • Did you see that in the console output? – Ravi Kadaboina Oct 12 '12 at 05:22
  • Yes. I am logging all those. I can see that it's loading 6 times.how can avoid that so that I can load only on time – mdp Oct 12 '12 at 05:55
  • Can Inject services which have @service by using ManagedPropety into the ManagedBean? – mdp Oct 12 '12 at 17:38
  • Yes, you should be able to that as well. – Ravi Kadaboina Oct 12 '12 at 19:00