4

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

电子邮件.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.

}

我正在尝试将我的电子邮件类自动装配到 Jsf managedBean 中,但我得到了空指针异常。我要去哪里错了。

4

1 回答 1

7

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

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

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

}

也可以看看:

于 2012-10-11T23:55:39.080 回答