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

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

FreeMarker configuration conflict (Spring Boot)

问题

我有一个FreeMarker配置:

  1. @Configuration
  2. public class FreeMarkerConfig {
  3. @Bean
  4. public FreeMarkerConfigurationFactoryBean freeMarkerConfiguration() {
  5. FreeMarkerConfigurationFactoryBean freeMarkerConfigBean = new FreeMarkerConfigurationFactoryBean();
  6. freeMarkerConfigBean.setTemplateLoaderPath("/templates/");
  7. return freeMarkerConfigBean;
  8. }
  9. }

以及build.gradle中的依赖项:

  1. dependencies {
  2. implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  3. implementation 'org.springframework.boot:spring-boot-starter-mail'
  4. implementation 'org.springframework.retry:spring-retry:1.2.5.RELEASE'
  5. compile 'org.freemarker:freemarker:2.3.30'
  6. compileOnly 'org.projectlombok:lombok'
  7. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  8. annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
  9. annotationProcessor 'org.projectlombok:lombok'
  10. testImplementation 'org.springframework.boot:spring-boot-starter-test'
  11. runtimeOnly 'org.postgresql:postgresql'
  12. }

它可以正常工作。但是当我在build.gradle中添加以下依赖项时:

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

我会得到一个错误:

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

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

[更新]

尝试在我的主类中添加exclude

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

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

英文:

I have a FreeMarker configuration:

  1. @Configuration
  2. public class FreeMarkerConfig {
  3. @Bean
  4. public FreeMarkerConfigurationFactoryBean freeMarkerConfiguration() {
  5. FreeMarkerConfigurationFactoryBean freeMarkerConfigBean = new FreeMarkerConfigurationFactoryBean();
  6. freeMarkerConfigBean.setTemplateLoaderPath("/templates/");
  7. return freeMarkerConfigBean;
  8. }
  9. }

And dependencies from build.gradle:

  1. dependencies {
  2. implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
  3. implementation 'org.springframework.boot:spring-boot-starter-mail'
  4. implementation 'org.springframework.retry:spring-retry:1.2.5.RELEASE'
  5. compile 'org.freemarker:freemarker:2.3.30'
  6. compileOnly 'org.projectlombok:lombok'
  7. developmentOnly 'org.springframework.boot:spring-boot-devtools'
  8. annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
  9. annotationProcessor 'org.projectlombok:lombok'
  10. testImplementation 'org.springframework.boot:spring-boot-starter-test'
  11. runtimeOnly 'org.postgresql:postgresql'
  12. }

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:

  1. The bean 'freeMarkerConfiguration', defined in class path resource
  2. [org/springframework/boot/autoconfigure/freemarker/FreeMarkerServletWebConfiguration.class],
  3. could not be registered.
  4. A bean with that name has already been defined in class path resource
  5. [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:

  1. @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

并且使用

  1. implementation 'org.springframework.boot:spring-boot-starter-web'
  2. 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

  1. implementation 'org.springframework.boot:spring-boot-starter-web'
  2. 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:

确定