Article Directory
一 、 application.yml
1 Introduction
Spring boot uses a global configuration file, the configuration file name is fixed: application.properties or application.yml, placed under resources or under the class path /config. The function of the configuration file is to modify the default value of the spring boot automatic configuration.
Application.yml configuration example:
server:
port: 8081
2. yml configuration file value acquisition
Open the project we created through the quick wizard before, and pay attention to the test related content when choosing the starter.
- Create an entity class:
Create the entity package under com.wang, and then create entity classes Person and Dog under entity:
package com.wang.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将全局配置文件中配置的每一个属性的值映射到这个组件中,注意是全局配置文件
* @ConfigurationProperties告诉spring boot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix属性表明配置文件中哪个下面的所有的属性进行一一映射
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> map;
private List<Object> list;
private Dog dog;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
public List<Object> getList() {
return list;
}
public void setList(List<Object> list) {
this.list = list;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Person(String name, Integer age, Boolean boss, Date birth, Map<String, Object> map, List<Object> list, Dog dog) {
this.name = name;
this.age = age;
this.boss = boss;
this.birth = birth;
this.map = map;
this.list = list;
this.dog = dog;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", map=" + map +
", list=" + list +
", \ndog=" + dog +
'}';
}
}
package com.wang.entity;
public class Dog {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Dog() {
}
public Dog(String name, Integer age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- Add dependency in pom:
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- Place application.yml:
person:
name: Tracy
age: 25
boss: false
birth: 1996/1/1
map: {
k1: v1,k2: v2}
list:
- l1
- l2
dog:
name: 团团
age: 1
- unit test:
There is a test class under test/java/com.wang, the code:
package com.wang;
import com.wang.entity.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class Boot02ApplicationTests {
@Autowired
Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
Then run the test method and it succeeded:
3. Configuration file placeholder
application.properties file:
person.name=Tracy
#使用随机数
person.age=${
random.int}
person.boss=false
person.birth=1996/1/1
person.map.k1=v1
person.map.k2=v2
person.list=l1,l2
#引用上面的值
person.dog.name=${
person.name}的狗:团团
#也可以这么写,如果person.name取不出值,就赋值为hello
#person.dog.name=${
person.name:hello}的狗:团团
person.dog.age=1
The person class is unchanged:
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
...
}
have a test:
Two, @PropertySource and @ImportResource
1.@PropertySource
To load the specified configuration file, it needs to be used in conjunction with @ConfigurationProperties (@ConfigurationProperties is effective for the global configuration file by default).
Note that this annotation is only valid for the .properties file.
-
Create a file person.properties file under resources.
-
Then modify the Person class code:
@PropertySource(value={
"classpath:person.yml"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
...
}
2.@ImportResource
Import the spring configuration file to make the content in the configuration file effective.
The specific usage is to first create a spring configuration file beans.xml under resources, and then use annotations in the Application class:
package com.wang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@ImportResource(locations={
"classpath:beans.xml"})
@SpringBootApplication
public class Boot02Application {
public static void main(String[] args) {
SpringApplication.run(Boot02Application.class, args);
}
}
3. SpringBoot recommends the way to add components to the container: full annotation
Create a configuration class, the code is as follows:
package com.wang.config;
import com.wang.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class Config01 {
// 将方法的返回值添加到容器中,默认id就是方法名
@Bean
public Person helloPerson(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new Person();
}
}
have a test:
package com.wang;
import com.wang.entity.Person;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class Boot02ApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
void contextLoads() {
System.out.println(person);
}
@Test
void testHelloPerson(){
System.out.println(ioc.containsBean("helloService"));
}
}
3. Multi-environment support Profile
When we write the main configuration file, we can name it application-XXX.properties/yml, and perform different configurations in multiple configuration files. If you do not specify a configuration file, application.properties/yml will take effect by default.
If we want to specify the use of a configuration file, add in the configuration file:
spring.profiles.active=dev
For more advanced usage, please check the information online.
Fourth, the loading of the configuration file
1. Internal placement
From high to low priority:
- file:./config/ is placed in the config folder of the root directory of the current file
- file:./ placed in the root directory of the current file
- classpath:/config/ is placed in the config folder under resources
- classpath:/ placed under resources
All configuration files will be loaded, and the high-priority configuration file will overwrite the content of the low-priority configuration file to complement the configuration.
You can also change the default configuration by configuring spring.config.location:
After the project is packaged, you can use command line parameters to specify the new location of the configuration file when you start the project. The specified configuration file and the configuration files loaded by default work together to form a complementary configuration.
2. External placement
From high to low priority:
- Command line parameters
- JNDI attributes from java:comp/env
- Java system properties (System.getProperties())
- Operating system environment variables
- Random.* property value configured by RandomValuePropertySource
- Application-{profile}.properties or appiation.yml outside the jar package with spring.profle configuration file
- Application-{profle}.properties or appiation.yml in the jar package with spring, profle) configuration files
- Application.properties or application.yml outside the jar package (without spring.profle configuration file
- Application.properties or application.yml inside the jar package without spring.profile) configuration file
All configuration files will be loaded, and the high-priority configuration file will overwrite the content of the low-priority configuration file to complement the configuration.