英文:
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
找到的解决方案:
-
在属性中设置(bean 名称应相同):
spring.main.allow-bean-definition-overriding=true
-
删除 FreeMarker 自定义配置,使用默认配置。在属性中设置:
spring.freemarker.template-loader-path
并且使用
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
代替 spring-data-rest
和 org.freemarker:freemarker
。
英文:
Found solutions:
-
Set in properties (beans names should be the same):
spring.main.allow-bean-definition-overriding=true
-
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论