Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load property file based on spring profiles

How to create project architecture to support multiple envionment. Each environment will have different datasource from different property file like(dev-propertfile,test-propertyFil,Production-propertyfile) with help of spring's

org.springframework.core.env.Environment;
like image 496
Santoshkumar Kalasa Avatar asked Dec 21 '16 12:12

Santoshkumar Kalasa


People also ask

How does spring load properties file?

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.

How do I get property properties from spring boot?

Another way to read application properties in Spring Boot app is to use the @ConfigurationProperties annotation. To do that we will need to create a Plain Old Java Object where each class field matches the name of the key in a property file.

Are Spring Boot supports different properties based on the spring active profile?

Spring Boot supports different properties based on the Spring active profile. For example, we can keep two separate files for development and production to run the Spring Boot application.

How to create profile based properties file in Spring Boot?

To make profile based properties file, you need to add "-profilename" at the end of the file as application-error.properties. And also this file must be present in the classpath ("src/main/resources"). flag spring.profiles.active will take profiles names comma-separated if you have many.

How to load properties file from external location in Spring Boot?

Loading the properties file from an external location can be done in multiple ways but you should decide which one is suitable for your scenario. 2. Loading from application.properties By default, Spring boot will load all the properties from the application.properties file which is located at " src/main/resources ".

How to set property values in spring reading properties file?

In Spring reading properties file and setting property values can be done using- Suppose you have your Database properties in a properties file called db.properties residing under config folder under resources folder.

How do I register a properties file in spring?

Register a Properties File via Annotations Spring 3.1 also introduces the new @PropertySource annotation as a convenient mechanism for adding property sources to the environment. We can use this annotation in conjunction with the @Configuration annotation:


1 Answers

I'll give step by step procedure for Spring boot applications.

  1. Inside /src/main/resources/application.properties mention spring.profiles.active=dev (or Prod)
  2. Create /src/main/resources/application-dev.properties and give your custom dev configurations here.
  3. Create /src/main/resources/application-prod.properties and give your custom prod configurations here.

Run.

like image 138
USM Avatar answered Oct 09 '22 02:10

USM