为什么mailSender总是得到null?春/ JSF

时间:2015-05-28 20:16:17

标签: spring jsf javamail jsf-2.2

我尝试在managedbean(jsf)中使用spring发送邮件。但是我得到了一个nullpointer异常。

MailServiceImpl.class

RewriteRule ^es/empresa/terminos-y-condiciones.html$ http://domain/terminos-y-condiciones.html [L]
RewriteRule ^es/servicios/registro-de-dominios/.*$ http://domain/dominios.html [R=301,NC,L]
RewriteRule ^es/empresa/.*$ http://domain/nosotros.html [R=301,NC,L]   
RewriteRule ^es/component/.*$ http://domain/contacto.html [R=301,NC,L]
RewriteRule ^es(/.*)?$ / [R=301,NC,L]

\ WEB-INF \应用context.xml中

@Service("MailService")
public class MailServiceImpl implements MailService, Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    @Autowired
    private JavaMailSender mailSender;
    @Autowired
    private SimpleMailMessage alertMailMessage;

    Logger log = Logger.getLogger(getClass());

    @Override
    public void sendMail(String from, String to, String subject, String body) {

        try {
            final SimpleMailMessage message = new SimpleMailMessage();

            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(body);
            if (mailSender != null) {
                mailSender.send(message);
            } else {
                log.info("mailSender is null." + mailSender);
            }
        } catch (final MailException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    @Override
    public void sendAlertMail(String alert) {

        final SimpleMailMessage mailMessage = new SimpleMailMessage(
                alertMailMessage);
        mailMessage.setText(alert);
        mailSender.send(mailMessage);

    }

    public JavaMailSender getMailSender() {
        return mailSender;
    }

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

}

ManagedBean.class

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="port" value="587" />
    <property name="host" value="smtp.mail.yahoo.com" />
    <property name="username" value="my@yahoo.com" />
    <property name="password" value="mypassword" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.starttls.enable">true</prop>
            <prop key="mail.debug">true</prop>
        </props>
    </property>

</bean>

<bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage">
    <property name="from">
        <value>my@yahoo.com</value>
    </property>
    <property name="to">
        <value>my@yahoo.com</value>
    </property>
    <property name="subject"
        value="Alert - Exception occurred. Please investigate" />
</bean>

<bean id="MailService" class="spring.service.MailServiceImpl">
    <property name="mailSender" ref="mailSender"></property>
</bean>

的pom.xml

@ManagedProperty(value = "#{MailService}")
MailServiceImpl mailServiceImpl;

public void sendingEmail() {
    mailServiceImpl.sendMail("my@yahoo.com", "my@yahoo.com",
            "Hi look at me!", "Bla bla bla..");
}

和application-context.xml的web.xml

      <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.3</version>
    </dependency>

mailSender总是以null为例。为什么?如何正确发送电子邮件?提前致谢。

1 个答案:

答案 0 :(得分:0)

我怀疑在这种情况下,在JSF bean中你得到了一个注入了MailServiceImpl的实例,它是由CDI容器(或者可能是JSF本身)而不是Spring创建的,因此对象中的字段为空(显然,因为CDI容器对Spring注释一无所知。

如果要在JSF托管bean中注入Spring bean,则必须将以下代码段放在faces-config类中:

<application>
    <el-resolver>
        org.springframework.web.jsf.el.SpringBeanFacesELResolver
    </el-resolver>
</application>