FreeMarker配置冲突(春季启动)

时间:2020-10-13 13:44:05

标签: java spring spring-boot freemarker

我有一个FreeMarker配置:

@Configuration
public class FreeMarkerConfig {

    @Bean
    public FreeMarkerConfigurationFactoryBean freeMarkerConfiguration() {
        FreeMarkerConfigurationFactoryBean freeMarkerConfigBean = new FreeMarkerConfigurationFactoryBean();
        freeMarkerConfigBean.setTemplateLoaderPath("/templates/");
        return freeMarkerConfigBean;
    }

}

还有build.gradle的依赖项:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-mail'
    implementation 'org.springframework.retry:spring-retry:1.2.5.RELEASE'
    compile 'org.freemarker:freemarker:2.3.30'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    runtimeOnly 'org.postgresql:postgresql'

}

行得通。但是当我在build.gradle中设置以下依赖项时:

implementation 'org.springframework.boot:spring-boot-starter-data-rest'

我会得到一个错误:

The bean 'freeMarkerConfiguration', defined in class path resource 
[org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.class], 
could not be registered. 
A bean with that name has already been defined in class path resource 
[com/lab/myservice/config/FreeMarkerConfig.class]

似乎spring-data-rest也提供了已配置的FreeMarker,并且会发生冲突。 @Primary和@Qualifier不起作用。 我该怎么办?

[UPDATE]

试图将exclude添加到我的主班:

@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration.class})

现在可以启动应用程序,但是FreeMarker不起作用。

1 个答案:

答案 0 :(得分:0)

找到的解决方案:

  1. 设置属性(bean名称应相同):spring.main.allow-bean-definition-overriding=true

  2. 删除FreeMarker自定义配置并使用默认设置。设置属性:spring.freemarker.template-loader-path

并使用

implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'

代替spring-data-restorg.freemarker:freemarker