将bean自动装入JSF Managed bean时的空指针

时间:2012-10-11 19:53:29

标签: spring jsf-2 dependency-injection autowired managed-bean

我使用Spring Java邮件和Velocity Template开发了一个电子邮件服务,如下所示。

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;
        }

//发送电子邮件的方法。 }

我的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.

}

我正在尝试将我的Email类自动装入Jsf managedBean但我得到空指针异常。哪里出错了。

1 个答案:

答案 0 :(得分:7)

你不能在JSF托管bean中注入这样的Spring bean。将其更改为

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

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

}

另见: