我正在尝试使用Freemarker在我的JBoss 5.1网络应用服务器上的网络应用中发送模板电子邮件。
邮件的上下文的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;
}
}
maven depandency for freemarker:
<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
问题在于类加载器,但我不知道如何解决它。
答案 0 :(得分:1)
您的类路径中有两个freemarker的JAR文件副本。你需要找到它们,并将其缩小到一个。