1

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

“bean 初始化失败;嵌套异常是 org.springframework.beans.TypeMismatchException:无法将类型 [com.core.springexamples.UCMSMailImpl] 的属性值转换为属性“mailSender”所需的类型 [org.springframework.mail.MailSender] ; 嵌套异常是 java.lang.IllegalArgumentException:无法将类型 [com.core.springexamples.UCMSMailImpl] 的值转换为属性“mailSender”所需的类型 [org.springframework.mail.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: Failed to convert type of property value of type [com.core.springexamples.UCMSMailImpl] to required type [org.springframework.mail.MailSender] for property

请帮我。

4

1 回答 1

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>
于 2012-10-16T11:09:19.540 回答