Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot Configuration Processor, Duplicate @ConfigurationProperties definition for prefix

In a Spring Boot application, I want to use @ConfigurationProperties annotation with the same prefix to configure my two data sources depending to the profile. Why is it forbidden by Spring Boot Configuration Processor ? The error reported by gradle is:

...
:compileJava ... error: Duplicate `@ConfigurationProperties` definition for prefix 'spring.datasource'

Notes:

  • "Run As->Spring Boot App" works in STS
  • Without spring-boot-configuration-processor dependency, gradle build works (but the warning When using @ConfigurationProperties it is recommended to add 'spring-boot-configuration-processor' to your classpath to generate configuration metadata appears)

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();
    }

}

Thanks in advance

like image 392
cmaulini Avatar asked Aug 18 '17 14:08

cmaulini


People also ask

What is the use of @configuration in spring boot?

One of the most important annotations in spring is @Configuration annotation which indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application. This annotation is part of the spring core framework.

How do you define properties in spring boot?

We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name. For example, if we define a “staging” environment, that means we'll have to define a staging profile and then application-staging. properties.

What is @configurationproperty?

@ConfigurationProperties allows to map the entire Properties and Yaml files into an object easily. It also allows to validate properties with JSR-303 bean validation. By default, the annotation reads from the application. properties file.

What are the ways in which spring boot can read configuration?

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.


1 Answers

I think you are confused on how this works. The code should mostly stay the same. The properties change when you define which profile to load at start up.

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=

And only one bean setup the datasource.

like image 98
ndrone Avatar answered Oct 02 '22 19:10

ndrone