Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment Specific application.properties file in Spring Boot application [closed]

In my Spring Boot application, i want to create environment specific properties file. The packaging type of my application in war and i am executing it in embedded tomcat. I use sts and execute the main from sts itself.

  1. Can i have environment specific properties file like application-${env-value}.properties?

In above case, env-value will have values as local/devl/test/prod

  1. Where to set the env-value file? For local, i can set it as the jvm argument through sts

  2. Who reads the application.properties in Spring Boot application.

  3. How to load the environment specific properties file? For ex - if i set the database uid,pwd, schema etc in environment specific property file, in that case will the datasource be able to understand the properties in it?

  4. Can i use application.properties and application-local.properties file at same time?

like image 774
user3534483 Avatar asked Aug 25 '15 05:08

user3534483


People also ask

How do you set environment specific properties in spring Boot?

Environment-Specific Properties File. If we need to target different environments, there's a built-in mechanism for that in 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.

Which property in application properties file will help to run spring Boot application?

Properties files are used to keep 'N' number of properties in a single file to run the application in a different environment. In Spring Boot, properties are kept in the application. properties file under the classpath. Note that in the code shown above the Spring Boot application demoservice starts on the port 9090.

Where are application properties stored in spring Boot?

You will need to add the application. properties file in your classpath. If you are using Maven or Gradle, you can just put the file under src/main/resources . If you are not using Maven or any other build tools, put that under your src folder and you should be fine.

Does environment variable override application properties?

Now, when your Spring Boot application starts, it will be given those environment variables, which will override your application. properties .


1 Answers

Spring Boot already has support for profile based properties.

Simply add an application-[profile].properties file and specify the profiles to use using the spring.profiles.active property.

-Dspring.profiles.active=local 

This will load the application.properties and the application-local.properties with the latter overriding properties from the first.

like image 50
M. Deinum Avatar answered Oct 02 '22 17:10

M. Deinum