首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FreeMarker配置冲突(Spring )

FreeMarker配置冲突(Spring )
EN

Stack Overflow用户
提问于 2020-10-13 13:44:05
回答 1查看 1.3K关注 0票数 1

我有一个FreeMarker配置:

代码语言:javascript
复制
@Configuration
public class FreeMarkerConfig {

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

}

和来自build.gradle的依赖关系

代码语言:javascript
复制
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'

我会得到一个错误:

代码语言:javascript
复制
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,而且冲突也会发生。@主限定符和@限定符无效。我能做什么?

更新

尝试将exclude添加到我的主类:

代码语言:javascript
复制
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration.class})

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-13 15:36:23

找到解决方案:

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

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

和使用

代码语言:javascript
复制
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'

而不是spring-data-restorg.freemarker:freemarker

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64336394

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档