At last episode, we have a peek at the spring boot project. We knew the spring boot philosophy “convention over configuration”, but we still need to grasp the spring application configuration, remember some important and common configs, in case of changing the default value at some time.
First of all, application.properties is located in src/main/java/resources by default.
define customized value
application.properties
net.flywithfan.helloMsg = "hello, I am from China."
To inject value by using Value annotation
@RestController
public class HelloController {
@Value("${net.flywithfan.helloMsg}")
private String helloMsg;
@RequestMapping("/hello")
public String hello() {
return helloMsg;
}
}
Open url http://localhost:8080/hello, displays:
Hello, I am from China.
define configuration bean
sometimes, we have a lot properties that ought to be injected, so it’s convenient to define a bean for this specific task. To do that, use ConfigurationProperties annotation,
application.properties
net.flywithfan.helloMsg=Hello, I am from China.
net.flywithfan.owner=Luhao Yang
net.flywithfan.slogan=Stay hungry Stay foolish
GlobalConfig.java
@Component
@ConfigurationProperties(prefix="net.flywithfan")
public class GlobalConfig {
private String type;
private String owner;
private String slogan;
//getters and setters ...
@Override
public String toString() {
return "GlobalConfig{" +
"type='" + type + '\'' +
", owner='" + owner + '\'' +
", slogan='" + slogan + '\'' +
'}';
}
}
HelloController.java
@Autowired
private GlobalConfig gc;
@RequestMapping("/info")
public String info() {
return gc.toString();
}
Open url http://localhost:8080/info, displays:
GlobalConfig{type=’Personal Blog’, owner=’Luhao Yang’, slogan=’Stay hungry Stay foolish’}
Configuring Random Values
The RandomValuePropertySource is useful for injecting random values.
It can produce integers, longs, uuids, or strings, as shown in the following example:
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.uuid=${random.uuid}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
Placeholders in Properties
You can refer back to previously defined values.
net.flywithfan.desc=${net.flywithfan.owner}'s ${net.flywithfan.type}
Accessing Command Line Properties
By default, SpringApplication converts any command line option arguments (that is, arguments starting with –, such as –server.port=9000) to a property and adds them to the Spring Environment. As mentioned previously, command line properties always take precedence over other property sources. SEE HERE.
you can disable them by using SpringApplication.setAddCommandLineProperties(false)
Application Property Files Priorities
SpringApplication loads properties from application.properties files in the following locations and adds them to the Spring Environment:
- A /config subdirectory of the current directory
- The current directory
- A classpath /config package
- The classpath root
Hence, the property in application.properties under config/ would override those in resources folder.
For example, net.flywithfan.helloMsg=I am number one in sequence.
would lead to an output I am number one in sequence. instead of
Hello, I am from China. with url http://localhost:8080/hello
Profile-specific Properties
In addition to application.properties files, profile-specific properties can also be defined by using the following naming convention: application-{profile}.properties. The Environment has a set of default profiles (by default, [default]) that are used if no active profiles are set. In other words, if no profiles are explicitly activated, then properties from application-default.properties are loaded.
i.e
- application-dev.properties for development config
- application-pro.properties for production config
To get there, we should specify the spring.profiles.active property like this:
spring.profiles.active=dev
Alternatively, it can come with the boot parameters as well.
java -jar xxx.jar --spring.profiles.active=dev
Using YAML Instead of Properties
YAML is a superset of JSON and, as such, is a convenient format for specifying hierarchical configuration data.
If you use “Starters”, SnakeYAML is automatically provided by spring-boot-starter.
application.yaml
net:
flywithfan:
admin-name: yangluhao
enable-password: true
tags:
- Java
- Spring MVC
- Spring Boot
msg: 你好
AdminConfig.java
@Component
@ConfigurationProperties(prefix="net.flywithfan")
public class AdminConfig {
private String adminName;
private boolean enablePasseword;
private List<String> tags;
private String msg;
//getters and setters ...
@Override
public String toString() {
return "AdminConfig{" +
"adminName='" + adminName + '\'' +
", enablePasseword=" + enablePasseword +
", tags=" + tags +
", msg='" + msg + '\'' +
'}';
}
}
HelloController.java
@RequestMapping("/admin")
public String admin(){
return ac.toString();
}
open http://localhost:8080/admin, output:
AdminConfig{adminName=’yangluhao’, enablePasseword=false, tags=[Java, Spring MVC, Spring Boot], msg=’你好’}
As you can see, yaml is very easy to write and read, and also support chinese correctly. I tried using chinese in application.properties but it turned out a mess.
It is said that changing setting like this should solve this problem. Unfortunately it didn’t work out for me.
So I’d better use yaml, lol…
@ConfigurationProperties vs. @Value
The @Value annotation is a core container feature, and it does not provide the same features as type-safe configuration properties. The following table summarizes the features that are supported by @ConfigurationProperties and @Value: