java - spring freemarker ConversionNotSupportedException

标签 java spring jboss classloader freemarker

我正在尝试在 JBoss 5.1 Web 应用程序服务器上的 Web 应用程序中使用 Freemarker 作为模板电子邮件。

邮件上下文-xml:

<bean id="freemarkerConfiguration"
    class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
    <property name="templateLoaderPath" value="/WEB-INF/templates" />
</bean>


<bean id="registrationMailService" class="com.epam.darts.webapp.utils.RegistrationMailService">
    <property name="configuration" ref="freemarkerConfiguration" />
    <property name="mailSender" ref="mailSender" />
</bean>

RegistrationMailService.java:

public class RegistrationMailService {

    private JavaMailSender mailSender;
    private Configuration configuration;

    public void sendConfirmationEmail(final User user) {
        MimeMessage message = this.mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message);
        try {
            helper.setFrom(user.getLogin());

            helper.setTo(user.getLogin());
            helper.setSubject(user.getLogin());

            Map<String, Object> model = new HashMap<String, Object>();
            model.put("user", user);

            String sendText = FreeMarkerTemplateUtils.processTemplateIntoString(
                        this.configuration.getTemplate("regisstration_mail.html"), model);
            helper.setText(sendText, true);
            this.mailSender.send(helper.getMimeMessage());
        }
        catch(MessagingException e) {
            e.printStackTrace();
           }
    }

    public void setMailSender(final JavaMailSender mailSender) {
        this.mailSender = mailSender;
    }
    public void setConfiguration(final Configuration configuration) {
        this.configuration = configuration;
    }
}

freemarker 的 Maven 依赖性:

    <dependency>
        <groupId>freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.9</version>
    </dependency>

第一次用,效果很好。但是如果我尝试重新部署我的应用程序(mvn clean package jboss:hard-deploy),我会收到错误

 18:17:46,839 ERROR [ContextLoader] Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationMailService' defined in
ServletContext resource [/WEB-INF/spring/spring-mail.xml]: Initialization of bean failed; nested exception is org.springfram
ework.beans.ConversionNotSupportedException: Failed to convert property value of type 'freemarker.template.Configuration' to
 required type 'freemarker.template.Configuration' for property 'configuration'; nested exception is java.lang.IllegalStateE
xception: Cannot convert value of type [freemarker.template.Configuration] to required type [freemarker.template.Configurati
on] for property 'configuration': no matching editors or conversion strategy found

可能问题出在类加载器中,但我不知道如何修复它。

最佳答案

您的类路径中似乎有两份 freemarker 的 JAR 文件副本。您需要找到它们,并将范围缩小到一个。

关于java - spring freemarker ConversionNotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6456890/

相关文章:

java - 收到 404 running spring boot

maven - JBoss 7.1.1.WildFly 最终迁移指南

java - java.1.8 中是否有像 scala 中那样在 Map 中传递/存储函数引用?

Java - 字 rune 件的文件处理

java - 如何使用 spring mvc 使用子字符串从数据库中剪切内容?

java - JBOSS 服务器停留在启动状态

java - JConsole 远程连接到 JBoss EAP

java - 用 Iterable.forEach 替换 foreach 循环纯粹是为了美观吗?

java - 如何解决有关需要从特定类扩展的类的 `unchecked` Java 警告?

java - Interface Annotation 不接受 application.properties 值