I'm trying to use Freemarker for template emails in my web-app on JBoss 5.1 web-app server.
mail-context-xml:
<bean id="freemarkerConfiguration"
class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="templateLoaderPath" value="/WEB-INF/templates" />
</bean>
开发者_如何学Go<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>
First time it works good. But if I try to redeploy my app (mvn clean package jboss:hard-deploy) I got error
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
May be problem is in class-loader, but i dont know how fix it.
It looks like you have two copies of freemarker's JAR file in your classpath. You need to find them, and narrow it down to one.
精彩评论