Spring JavaMailSenderImpl抛出TypeMismatchException

时间:2012-10-16 10:37:09

标签: spring javamail

我在我的应用程序中使用JavaMailSenderImpl在Spring框架中配置了JavaMail。实际上,我尝试从数据库加载邮件属性,并在spring config.xml文件中进行了一些更改。 但我得到了错误

  

“bean的初始化失败;嵌套异常是   org.springframework.beans.TypeMismatchException:无法转换   [com.core.springexamples.UCMSMailImpl]类型的属性值   属性的必需类型[org.springframework.mail.MailSender]   'mailSender';嵌套异常是java.lang.IllegalArgumentException:   无法将[com.core.springexamples.UCMSMailImpl]类型的值转换为   属性的必需类型[org.springframework.mail.MailSender]   'mailSender':找不到匹配的编辑或转换策略“

在我的应用程序中实现的任何更改,都在下面提到。

第1步:

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

第2步: -

<bean id="mailSender" class="com.core.springexamples.UCMSMailImpl" scope="prototype" init-method="configuredProperties">
    <property name="javaMailImpl" ref="javaMailImpl"></property>
</bean>

com.core.springexamples.UCMSMailImpl : -

public class UCMSMailImpl {
    private JavaMailSenderImpl javaMailImpl;
    private ConfigDAO configDAO;

    public  void configuredProperties(){
        System.out.println("UCMSMailImpl::configuredProperties");
        Properties props=new Properties();
    String[] mildata=configDAO.getMailingPropData();
        props.put("mail.smtp.auth", mildata[0]);
        props.put("mail.smtp.starttls.enable", mildata[2]);
        props.put("mail.smtp.host", mildata[3]);
        props.put("mail.smtp.port", mildata[4]);
        props.put("mail.smtp.host", mildata[5]);
        props.put("username", mildata[6]);
        props.put("password",mildata[7]);
        getJavaMailImpl().setJavaMailProperties(props);

    }
    public JavaMailSenderImpl getJavaMailImpl() {
        return javaMailImpl;
    }
    public void setJavaMailImpl(JavaMailSenderImpl javaMailImpl) {
        this.javaMailImpl = javaMailImpl;
    }
    public void setConfigDAO(ConfigDAO configDAO){
    this.configDAO=configDAO;
    }
    public ConfigDAO getConfigDAO(){
    return configDAO;
    }

第3步: - 我正在尝试使用UCMSMailImpl java类从MailSender.send发送邮件。我提到了UCMSMailImpl bean。

<bean id="sendMail" class="com.core.springexamples.JavaMailing">
    <property name="mailSender" ref="mailSender"></property>

</bean>


public class JavaMailing {
    private MailSender mailSender;


    public void sendMail(String from,String to,String text,String subject){
        SimpleMailMessage message = new SimpleMailMessage();
                message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);   

    }

    /**
     * @return the mailSender
     */
    public MailSender getMailSender() {
        return mailSender;
    }

    /**
     * @param mailSender the mailSender to set
     */
    public void setMailSender(MailSender mailSender) {
        this.mailSender = mailSender;
    }

第4步: - 我尝试测试sendMail bean

ApplicationContext context =new ClassPathXmlApplicationContext("applicationContext-mail.xml");
    JavaMailing m=(JavaMailing)context.getBean("sendMail");
   m.sendMail("john.ch@gmail.com", "john.c@gmail.com", "TEST MAIL", "TEST MAIL");

但我得到的异常是 TypeMismatchException:无法将[com.core.springexamples.UCMSMailImpl]类型的属性值转换为属性所需的类型[org.springframework.mail.MailSender]

请帮帮我。

1 个答案:

答案 0 :(得分:1)

如果没有实现接口,则无法为接口分配类。 UCMSMailImpl未实施MailSender。保持原样,并改变您的UCMSMailImpl:

public class UCMSMailImpl implements MailSender {

    private JavaMailSenderImpl javaMailImpl;
    private ConfigDAO configDAO;

    // do your property initialization
    // ...

    // implement interface methods

    void send(SimpleMailMessage simpleMessage) throws MailException {
        this.javaMailImpl.send(simpleMessage);
    }

    void send(SimpleMailMessage[] simpleMessages) throws MailException {
        this.javaMailImpl.send(simpleMEssages);
    }

}

如果您无法更改UCMSMailImpl,请将其扩展:

public class MyUCMSMailImpl extends UCMSMailImpl implements MailSender {

    void send(SimpleMailMessage simpleMessage) throws MailException {
        this.getgetJavaMailImpl().send(simpleMessage);
    }

    void send(SimpleMailMessage[] simpleMessages) throws MailException {
        this.getgetJavaMailImpl().send(simpleMEssages);
    }

}

并更改您的配置:

<bean id="mailSender" class="your.package.MyUCMSMailImpl" scope="prototype" init-method="configuredProperties">
    <property name="javaMailImpl" ref="javaMailImpl"></property>
</bean>