在Spring Boot应用程序中,我想使用具有相同前缀的@ConfigurationProperties注释来根据配置文件配置我的两个数据源.为什么Spring Boot配置处理器禁止它? gradle报告的错误是:
...
:compileJava ... error: Duplicate `@ConfigurationProperties` deFinition for prefix 'spring.datasource'
笔记:
>“Run As-> Spring Boot App”在STS中运行
>如果没有spring-boot-configuration-processor依赖,gradle构建工作(但警告使用@ConfigurationProperties时,建议在类路径中添加’spring-boot-configuration-processor’以生成配置元数据)
的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")
}
application.properties
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.username = sa
spring.datasource.password = sa
hello.Application
@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();
}
}
提前致谢
application-dev.properties
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.username = sa spring.datasource.password = sa
spring.datasource.url =
appilication-prod.properties
spring.datasource.driverClassName = org.h2.Driver
spring.datasource.username = sa spring.datasource.password = sa
spring.datasource.url =
并且只有一个bean设置数据源.
原文地址:https://www.jb51.cc/spring/432139.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。