简体   繁体   中英

Spring-boot: Configure FreeMarkerConfigurationFactoryBean in web application

I'm trying to use Freemarker for e-mail templating in a web application.

I have declared a FreeMarkerConfigurationFactoryBean as follow:

@Bean
public FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean(EmailTemplateService templateService) {
    FreeMarkerConfigurationFactoryBean configurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
    configurationFactoryBean.setPreTemplateLoaders(templateService);
    return configurationFactoryBean;
}

When running my JUnit everything is working well, but when running in my webapp my bean is "overriden" by the spring boot FreeMarkerAutoConfiguration.

I have tried to:

  1. remove the spring-boot-starter-freemarker from my gradle file
  2. @EnableAutoConfiguration(exclude = {FreeMarkerAutoConfigurationk.class})
  3. spring.freemarker.enabled=false

But without success. Any idea?

Thanks guys.

As your application is a Web application, it's Spring Boot's FreeMarkerWebConfiguration that you're interested in. It doesn't use a FreeMarkerConfigurationFactoryBean but a FreeMarkerConfigurer . You should create a FreeMarkerConfigurer bean and configure it as required. For example:

@Bean
public FreeMarkerConfigurer freeMarkerConfigurer(EmailTemplateService templateService) {
    FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
    configurer.setPreTemplateLoaders(templateService);
    return configurer;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM