A, Spring Boot applicaion.properties reading the configuration file and the data file application.yml
Application.properties contents of the file are as follows:
book.name = SpringCloudInAction
book.author = HDN
1, read by @Value comment
1.1, a configuration that it is loaded into Spring Bean Ioc container
/**
* @author HDN
* @date 2019/6/14 14:37
*/
@Component
public class Book {
@Value("${book.name}")
private String name;
@Value("${book.author}")
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
1.2, to obtain the value in the configuration file by implanting Book
/**
* @author HDN
* @date 2019/6/5 16:19
*/
@RestController
@RequestMapping("/controller")
public class HelloController {
@Autowired
Book book;
@RequestMapping("/getProperties")
public String getProperties(){
return "1、通过@Value注解获取配置文件的属性值:"+book.getName()+"----"+book.getAuthor()+"<p>"+
}
}
1.3, the results are as follows:
2, the configuration file to read the contents of the object by injecting Environment
2.1, direct injection Environment object can be obtained in accordance with a value corresponding getProperty (param) Method
param: = foregoing configuration file attributes (key attributes)
/**
* @author HDN
* @date 2019/6/5 16:19
*/
@RestController
@RequestMapping("/controller")
public class HelloController {
@Autowired
Book book;
@Autowired
Environment env;
@RequestMapping("/getProperties")
public String getProperties(){
return "1、通过@Value注解获取配置文件的属性值:"+book.getName()+"----"+book.getAuthor()+"<p>"+
"2、通过Environment注入对象来获取配置文件属性值:"+env.getProperty("book.name")+"----"+env.getProperty("book.author")+"<p>"+
}
}
2.2, the results are as follows:
3, to read the contents of the configuration file by @ConfigurationProperties
3.1, a configuration that it Ioc Bean Spring loaded into the container
/**
* @author HDN
* @date 2019/6/14 15:25
*/
@Component
@ConfigurationProperties(prefix = "book")
public class BookConfigPro {
private String name;
private String author;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
3.2, to obtain the value in the configuration file by implanting BookConfigPro
/**
* @author HDN
* @date 2019/6/5 16:19
*/
@RestController
@RequestMapping("/controller")
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
@Autowired
Book book;
@Autowired
Environment env;
@Autowired
BookConfigPro bookConfigPro;
@RequestMapping("/getProperties")
public String getProperties(){
return "1、通过@Value注解获取配置文件的属性值:"+book.getName()+"----"+book.getAuthor()+"<p>"+
"2、通过Environment注入对象来获取配置文件属性值:"+env.getProperty("book.name")+"----"+env.getProperty("book.author")+"<p>"+
"3、通过@ConfigurationProperties注解获取配置文件属性:"+bookConfigPro.getName()+"----"+bookConfigPro.getAuthor();
}
}
3.3, the results are as follows:
Second, if you have your own defined configuration files, such as config.properties, you want to use @PropertySource (value = "config.properties") annotation to load custom configuration file content.
Two elements worth noting here: First, the file can not be used @PropertySource YAML annotations; second, if from the same profile and configuration application.properties defined preferentially loads the contents of the configuration file application.properties.