弹簧启动配置处理器,前缀的重复@ConfigurationProperties定义
2022-09-04 23:28:02
在 Spring Boot 应用程序中,我想使用具有相同前缀的注释来配置我的两个数据源,具体取决于配置文件。为什么它被弹簧启动配置处理器禁止?gradle 报告的错误是:@ConfigurationProperties
...
:compileJava ... error: Duplicate `@ConfigurationProperties` definition for prefix 'spring.datasource'
笔记:
- “以>启动应用”在 STS 中工作正常
- 如果没有 spring-boot-configuration-processor 依赖性,gradle build 可以正常工作(但会出现警告)
When using @ConfigurationProperties it is recommended to add 'spring-boot-configuration-processor' to your classpath to generate configuration metadata
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.6.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
maven { url "https://repository.jboss.org/nexus/content/repositories/releases" }
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile 'org.springframework.boot:spring-boot-configuration-processor:1.5.4.RELEASE'
compile("com.h2database:h2")
}
应用程序.属性
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.username = sa
spring.datasource.password = sa
你好。应用
@SpringBootApplication
public class Application {
public static void main(final String[] args) {
final SpringApplication app = new SpringApplication(Application.class);
app.setAdditionalProfiles("prod");
app.run();
}
@Bean
@Profile("dev")
@ConfigurationProperties("spring.datasource")
public DataSource dataSourceDev() {
return DataSourceBuilder
.create()
.url(generateDevUrl())
.build();
}
@Bean
@Profile("prod")
@ConfigurationProperties("spring.datasource")
public DataSource dataSourceProd() {
return DataSourceBuilder
.create()
.url(generateProdUrl())
.build();
}
}
提前致谢