简体   繁体   中英

Spring boot how to pick externalized spring properties file

I have this configurations which needs to be used for a spring boot application.

server.port=8085
server.servlet.context-path=/authserver
#data source
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=<url>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

By default spring-boot picks up up the application.properties file located in src/main/resources/

I want to alter this path and direct spring boot to different application.properties file

I can achieve this using

java -jar app.jar --spring.config.location=classpath:/another-location.properties  

Is there any any alternative solution I can achieve this without passing args through command line?

I was using this

   @PropertySource("file:C:\Users\test\.test\test.properties")
    @ConfigurationProperties(prefix = "spring")
    public class Configuration {

        private String ddlAuto;

        private String url;

        private String username;

        private String password;

        private String driverClassName;
    }

in my Main class

@SpringBootApplication
@EnableConfigurationProperties(Configuration.class)
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

There after I tried executing the app commenting out all datasource properties in application.properties under src/main/resources/ But it keeps giving me the error mentioned bellow and application fails to start

I was referring this tutorial : https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/

but as it's mentioned I get this error when i start the spring boot application

***************************
APPLICATION FAILED TO START
***************************

Description:

Binding to target org.springframework.boot.context.properties.bind.BindException: 

Any help on this would be appreciated

The recommended way to have externalized properties is to use the spring.config.location system property, by starting your application like so:

java -jar -Dspring.config.location=/path/to/my/file.properties app.jar

The reason for this is that you don't add coupling between your code and your filesystem hierarchy.

Before Spring Boot 2.0 this property is additive, meaning that it will complement the default locations. After Spring Boot 2.0, spring.config.location replaces the default locations (eg classpath src/main/resources/application.properties). To keep the additive behaviour after 2.0, use spring.config.additional-location instead.

Please see here for official documentation on this matter.

I am able to make it work properly on Spring Boot 2.1.2.RELEASE. This is what I have done: I have a test.properties in my /tmp folder with the following content:

test.myprop=hello

I also have the usual property file in the resources folder:

myprop=world

I have created a class for the custom property file:

@Configuration
@PropertySource("file:/tmp/test.properties")
@ConfigurationProperties(prefix = "test")
public class TestConfig {

    private String myprop;

    public String getMyprop() {
        return myprop;
    }

    public void setMyprop(String myprop) {
        this.myprop = myprop;
    }
}

And then in my main class I have enabled to configuration properties:

@EnableConfigurationProperties(TestConfig.class)
@SpringBootApplication
public class MyApp {

    public static void main(String[] args) {
        SpringApplication.run(MyApp.class, args);
    }

}

Now I have this test controller:

@RestController
public class TestController {

    @Value("${test.myprop}")
    private String externalisedProp;
    @Value("${myprop}")
    private String prop;

    @GetMapping("test")
    public void test() {
        System.out.println("externalised: " + externalisedProp);
        System.out.println("not externalised" + prop);
    }
}

Which, once called, is properly printing:

externalised: hello
not externalised: world

My TestConfig class is in the same package as the MyApp main class. What I have done is very similar, almost identical, to your solution, are you sure your path is correct? Also, I can see that the content of your property file is not matching what you have in your config class, the prefix is different. Maybe that is the problem?

Edit: I have tried to remove the @Configuration annotation from my property class (which you do not have as well) and it is not able to pick up the externalised properties anymore. The error is different though but you should try to add it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM