自由标记配置冲突(Spring Boot)

huangapple go评论109阅读模式
英文:

FreeMarker configuration conflict (Spring Boot)

问题

我有一个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不起作用。
我该怎么办?

[更新]

尝试在我的主类中添加exclude

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

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

英文:

I have a FreeMarker configuration:

@Configuration
public class FreeMarkerConfig {

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

}

And dependencies from 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'

}

It works okay. But when I set following dependency in build.gradle:

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

I'll get an error:

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]

Seems like spring-data-rest provides configured FreeMarker too and conflict happens.
@Primary and @Qualifier don't work.
What can I do?

[UPDATE]

Tried to add exclude to my main class:

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

Now application can start, but FreeMarker doesn't work.

答案1

得分: 2

找到的解决方案:

  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

英文:

Found solutions:

  1. Set in properties (beans names should be the same): spring.main.allow-bean-definition-overriding=true

  2. Delete FreeMarker custom config and use default one. Set in properties: spring.freemarker.template-loader-path

And use

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

Instead of spring-data-rest and org.freemarker:freemarker

huangapple
  • 本文由 发表于 2020年10月13日 21:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/64336394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定