Spring Boot (3) Configurations
This section describes the configurations in Spring Boot.
1. Get configuration by ConfigurableApplicationContext object
application.properties
# Common properties
server.port = 8090
server.context-path=/properties
server.session.timeout=1800
package com.my.study.properties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Main {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);
// Get properties from ConfigurableApplicationContext
String serverPort = context.getEnvironment().getProperty("server.port");
System.out.println("Server port: " + serverPort);
}
}
2. Get configuration by @Value annotation
application.properties
# Customized properties
user1.name=sunny-dev
user1.age=32-dev
package com.my.study.properties.module;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class User1 {
// Use @Value annotation to inject properties, default to use application.properties
@Value("${user1.name}")
private String name;
@Value("${user1.age}")
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
3. Get configuration by @PropertySource
If you add another properties files, then you need to use @PropertySource to specify the location of this file, then use @Value to inject properties.
customized.properties
# Customized properties
user2.name=Tom
user2.age=33
package com.my.study.properties.module;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
// Specify properties file to use
@PropertySource("classpath:customized.properties")
public class User2 {
// Use @Value annotation to inject properties
@Value("${user2.name}")
private String name;
@Value("${user2.age}")
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
4. Create a configuration class with annotation @org.springframework.context.annotation.Configuration
We use this configuration class to load all properties files at one place, it's convenient for properties management.
package com.my.study.properties.config;
import org.springframework.context.annotation.PropertySource;
@org.springframework.context.annotation.Configuration
@PropertySource("classpath:customized2.properties")
@PropertySource("classpath:random.properties")
public class Configuration {
}
5. Get configuration by
@ConfigurationProperties
customized2.properties
# Customized properties
user3.name=Xiaoming
user3.age=30
package com.my.study.properties.module;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
// No need to specify properties file here, because Configuration.java contains this clarification.
// Use @ConfigurationProperties to inject user3.name and user3.age to this class
@ConfigurationProperties(prefix = "user3")
public class User3 {
// No need to add @Value annotation here
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
6. Switch multiple environments
application-dev.properties
# Customized properties
user1.name=sunny-dev
user1.age=32-dev
application-test.properties
# Customized properties
user1.name=sunny-test
user1.age=32-test
application-pro.properties
# Customized properties
user1.name=sunny-pro
user1.age=32-pro
application.properties
# Specify deploy mode 'dev', then spring boot will use configuration file application-dev.properties
# Then no need to configure application-xxx.properties in Configuration.java manually.
spring.profiles.active=dev
7.
Random properties
random.properties
# Random value
com.test.value=${random.value}
# Random int value
com.test.number=${random.int}
# Random long value
com.test.bignumber=${random.long}
# Random int value in [0, 10)
com.test.random1=${random.int(10)}
# Random int value in [10, 20]
com.test.random2=${random.int[10,20]}
8. Testing Controller
package com.my.study.properties.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.my.study.properties.module.RandomVal;
import com.my.study.properties.module.User1;
import com.my.study.properties.module.User2;
import com.my.study.properties.module.User3;
@RestController
public class TestController {
@Autowired
private User1 user1;
@Autowired
private User2 user2;
@Autowired
private User3 user3;
@Autowired
private RandomVal randomVal;
// Get properties from Environment object
@Autowired
private Environment env;
@RequestMapping("/user1")
public ResponseEntity<User1> showUser() {
return new ResponseEntity<>(user1, HttpStatus.OK);
}
@RequestMapping("/user2")
public ResponseEntity<User2> showUser2() {
return new ResponseEntity<>(user2, HttpStatus.OK);
}
@RequestMapping("/port")
public String getServerPort() {
return env.getProperty("server.port");
}
@RequestMapping("/user3")
public ResponseEntity<User3> showUser3() {
return new ResponseEntity<>(user3, HttpStatus.OK);
}
@RequestMapping("/random")
public ResponseEntity<RandomVal> showRandomVals() {
return new ResponseEntity<>(randomVal, HttpStatus.OK);
}
}
9. Code structure
application.properties
# Common properties
server.port = 8090
server.context-path=/properties
server.session.timeout=1800
For all common properties, please refer to:
https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/html/common-application-properties.html
For all Spring Boot guide, please refer to:
https://docs.spring.io/spring-boot/docs/1.5.8.RELEASE/reference/html/