This entry shows where to put the application.properties to be automatically picked up by spring-boot, and how to access the properties defined in the file.
According to reference manual on spring’s official website, the “application.properties” will be added to “Environment” automatically if you put it under the right folder.
”
SpringApplication
will load properties from application.properties
files in the following locations and add them to the Spring Environment
:
- A
/config
subdirectory of the current directory. - The current directory
- A classpath
/config
package - The classpath root
”
This entry shows an explicit way to load the properties file, by using “PropertySource” annotation.
To achieve the same goal, we can simple create a “config” folder and place “application.properties” under the folder. Spring context will load those settings into Environment.
As an experiment, I tried to create properties in the following 3 locations:
- /config under “src/main/java”, which will create “/config” in the classpath.
- root directory, which will create a properties file under the current working directory.
- command line argument.
The following screenshot shows the folder structure, and the running result of the property precedence.
The following screenshot shows the command line properties defined in Eclipse.
1) /config/application.properties
disc.title=Rattle and Hum (/config) disc.artist=U2 (/config) disc.price=$20.1 (/config)
2) (current working directory)/application.properties
disc.title=Rattle and Hum (current working directory) disc.artist=U2 (current working directory) #disc.price=$20 (current working directory)
3) AppConfig.java, which does not use PropertySource.
package hello; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.env.Environment; @Configuration public class AppConfig { @Autowired Environment env; @Bean public BlankDisc disc(){ BlankDisc disc = new BlankDisc(); disc.setArtist(env.getProperty("disc.artist")); disc.setTitle(env.getProperty("disc.title")); disc.setPrice(env.getProperty("disc.price")); return disc; } @Bean public static PropertySourcesPlaceholderConfigurer properties(){ PropertySourcesPlaceholderConfigurer conf = new PropertySourcesPlaceholderConfigurer(); return conf; } }
4) MainApp.java
package hello; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.core.env.Environment; @Import(AppConfig.class) public class Application { private static Logger logger = LogManager.getLogger(Application.class); @Autowired private Environment env; public static void main(String[] args) throws Exception { ApplicationContext ctx = SpringApplication.run(Application.class); BlankDisc disc = ctx.getBean(BlankDisc.class); logger.info(disc); } }
5) BlankDisc.java, the model that takes values from properties file.
package hello; public class BlankDisc { private String title; private String artist; private String price; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } @Override public String toString(){ return "{title="+getTitle()+", artist="+getArtist()+", " +getPrice()+ "}"; } }
6) Property file can be loaded via classpath instead of the file system path.
Put “application.properties” under /src/main/resources, and reference the property file in configuration class via @PropertySource(“classpath:application.properties”)
Reference:
1. http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html
Under section 69.2.
2. http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Under section 24.3 for “Application property files”.