文章目录
一、application.yml
1.简介
spring boot使用一个全局的配置文件,配置文件名是固定的:application.properties或application.yml,放在resources下面或者类路径/config下。配置文件的作用是用来修改spring boot自动配置的默认值。
application.yml配置例子:
server:
port: 8081
2.yml配置文件值获取
打开我们之前通过快速向导创建的项目,注意在选在starter时要选择测试相关内容。
- 创建实体类:
在com.wang下创建entity包,再在entity下创建实体类Person和Dog:
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 +
'}';
}
}
- 在pom中添加依赖:
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
- 配置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
- 单元测试:
在test/java/com.wang下有一个测试类,代码:
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);
}
}
然后运行测试方法,成功了:
3.配置文件占位符
application.properties文件:
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
person类不变:
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
...
}
测试一下:
二、@PropertySource和@ImportResource
1.@PropertySource
加载指定的配置文件,需要与@ConfigurationProperties配合使用(@ConfigurationProperties默认是对全局配置文件生效)。
注意此注解只对.properties文件生效。
-
在resources下面创建文件person.properties文件。
-
再修改Person类代码:
@PropertySource(value={
"classpath:person.yml"})
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
...
}
2.@ImportResource
导入spring的配置文件,让配置文件中的内容生效。
具体用法是,首先在resources下创建一个spring配置文件beans.xml,然后在Application类中使用注解:
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推荐的给容器添加组件的方式:全注解
创建配置类,代码如下:
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();
}
}
测试一下:
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"));
}
}
三、多环境支持Profile
我们在主配置文件编写时可以命名为application-XXX.properties/yml,在多个配置文件中进行不同的配置。如果不指定配置文件,默认生效的是application.properties/yml。
如果我们要指定使用某个配置文件,在该配置文件中添加:
spring.profiles.active=dev
更高级的用法请上网查阅资料。
四、配置文件的加载
1.内部配置
按优先级从高到低:
- file:./config/ 放在当前文件根目录的config文件夹下
- file:./ 放在当前文件的根目录下
- classpath:/config/ 放在resources下的config文件夹中
- classpath:/ 放在resources下
所有的配置文件都会被加载,高优先级的配置文件会覆盖低优先级配置文件的内容,互补配置。
还可以通过配置spring.config.location来改变默认配置:
项目打包好以后,可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置。指定配置文件和默认加载的这些配置文件共同起作用形成互补配置。
2.外部配置
按优先级从高到低:
- 命令行参数
- 来自java∶comp/env的JNDI属性
- Java系统属性(System.getProperties())
- 操作系统环境变量
- RandomValuePropertySource配置的random.*属性值
- jar包外部的application-{profile}.properties或appiation.yml带spring.profle配置文件
- jar包内部的application-{profle}.properties或appiation.yml带spring,profle)配置文件
- jar包外部的application.properties或application.yml(不带spring.profle配置文件
- jar包内部的application.properties或application.yml不带spring.profile)配置文件
所有的配置文件都会被加载,高优先级的配置文件会覆盖低优先级配置文件的内容,互补配置。