1. The configuration file uses the configuration information written in yaml in the previous section :
server: port: 8081 path: /hello person: name: zhangsan age: 20 boss: false birth: 2017/11/12 #map writing 1: inline maps1: {k1: v1, k2: v2} #map writing 2 maps2: k3: v3 k4: v4 #Array writing 1 lists1: - l1 - l2 #Array writing 2: inline lists2: [l3, l4] # object dog: name: puppy age: 2
2. Write javaBeans
/** * Map each property value of the configuration in the configuration file to the component * @ConfigurationProperties: Tell SpringBoot to bind all properties in this class to the related configuration in the configuration file. * prefix: All properties under the properties specified by prefix in the configuration file correspond to the component properties one-to-one. * * @ConfigurationProperties: get values from global configuration file by default * * Only if this component is a component in the container, the container can provide the @ConfigurationProperties function. */ @Component @ConfigurationProperties(prefix = "person") public class Person implements Serializable { private String name; private Integer age; private Boolean boss; private Date birth; private Map<String, Object> maps; private List<Object> lists; private Dog dog; //Omit getter and setter }You can import the configuration file processor, and then you will be prompted when configuring properties
<!--Configuration file handler--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
3. Annotation
3.1、@ConfigurationProperties
prefix: All properties under the properties specified by prefix in the configuration file correspond to the component properties one-to-one.
Get the value from the global configuration file by default
3.2. Modify the default file encoding format of the properties of idea
3.3, the difference between @Value and @ConfigurationProperties to get the value
@ConfigurationProperties | @Value | |
Function | batch injection into the configuration file | specify one by one |
loosely bound | Support loose binding | Loose binding is not supported |
Game | not support | support |
JSR303 data verification | Support, @Validated, @Email, etc. | not support |
complex type encapsulation | support | not support |
If you only need a property in the configuration file, it is recommended to use @Value .
If you need to map to properties in a configuration class, @ConfigurationProperties is recommended .
3.4. Configuration file injection value verification:
@Component @ConfigurationProperties(prefix = "person") @Validated public class Person implements Serializable { @Email private String name; private Integer age; private Boolean boss; private Date birth; private Map<String, Object> maps; private List<Object> lists; private Dog dog; //Omit getter and setter }
3.5、@PropertySource
properties files other than application.properties
This annotation needs to be used with @ConfigurationProperties
The prefix used (if any) needs to be specified in @ConfigurationProperties
You need to specify the loaded properties file in @PropertySource
#employee.properties employee.name=lisi employee.age=30 employee.birth=2017/11/11 employee.boss=false
@Component @PropertySource(value = {"classpath:employee.properties"},encoding = "UTF-8") @ConfigurationProperties(prefix = "employee") public class Employee implements Serializable { private String name; private Integer age; private Date birth; private Boolean boss; //Omit getter and setter }
3.6、@ImportResource
There is no Spring configuration file in SpringBoot. Even if the configuration file in xml format in the Spring project is used, SpringBoot cannot automatically recognize it. If you want the Spring configuration file to take effect, you need to use @ImportResource to mark it on a configuration class. It is recommended to mark it in the main configuration. class.
<!-- beans.xml --> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="helloService" class="org.com.cay.spring.boot.service.HelloService"></bean> </beans>
@ImportResource(locations = {"classpath:beans.xml"})
3.7、@Bean
/** * @Configuration: Indicates that the current class is an annotation class, which is used to replace the original Spring xml configuration file */ @Configuration public class MyConfig { //Add the return value of the method to the container, the default id of this component in the container is the method name @Bean public HelloService helloService(){ System.out.println("Add component: helloService"); return new HelloService(); } }
=====================Make an advertisement, welcome to pay attention =====================
QQ: |
412425870 |
WeChat public account: Cay Classroom |
|
csdn blog: |
http://blog.csdn.net/caychen |
Code cloud: |
https://gitee.com/caychen/ |
github: |
https://github.com/caychen |
Click on the group number or scan the QR code to join the QQ group: |
|
Click on the group number or scan the QR code to join the QQ group: |