Spring Boot.配置一
一.配置文件
1.Spring Boot应用一个全局配置文件,配置文件名是固定的,可以使用下面这两种文件:
1.application.properties
2.application.yml(YAML语法)
2.配置文件的作用:修改Spring Boot自动配置的默认值。
3.配置文件可以放在以下几个位置
注:如果这几个位置都有配置文件就安优先级别进行读取“图片标识1的”为JAR内最优先级别,如果1里面和234都有的属性,只取1里面的,如果1里面没有234里面有那就形成互补配置。
4.自定义位置(必须打包成jar在运行时指定)
代码:
java -jar jar包名 --spring.config.location=配置文件位置
注:JAR外优先JAR内的配置,它也会跟项目里的配置文件形成互补配置
5.直接放在跟jar同级目录运行时就会读取这个配置
二.使用@ConfigurationProperties把配置文件里的值映射到组件中
1.JAVA编写
目录结构:
Person.java:
package cn.luoruiyuan.test.bean;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="person")//全局配置文件
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
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> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person [lastName=" + lastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="
+ maps + ", lists=" + lists + ", dog=" + dog + "]";
}
}
Dog.java
package cn.luoruiyuan.test.bean;
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;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
}
2.配置文件编写,下面两个配置文件用一个就行
目录结构:
application.properties中的配置
#配置person的值
person.lastName=张三
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=dog
person.dog.age=2
或者application.yml中的配置
#配置person的值
person:
lastName: 张三
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 8
3.pom.xml添加依赖
<!-- 导入配置文件处理器,配置文件进绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
4.单元测试
目录结构:
JAVA代码
package cn.luoruiyuan.test;
import org.junit.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;
import cn.luoruiyuan.test.bean.Person;
/**
*
* Spring Boot 单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入容器
*
* @SpringBootTest:表是是一个单元测试
* @RunWith:表示用SpringRunner来跑这个测试
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootTestApplicationTests {
@Autowired
Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
5.我用的是application.yml输出结果
6.ConfigurationProperties优点
1)批量注入
2)松散绑定
配置文件中写法不用完全一致如下写法都可以
person.lastName
person.last-name
person.last_name
PERSON_FIRST_NAME
3)支持校验
三.使用@Value把配置文件里的值映射到组件中
1.上面除了Person.java其它的文件内容没有修改
Person.java:
package cn.luoruiyuan.test.bean;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Person {
@Value("${person.lastName}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
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> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person [lastName=" + lastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="
+ maps + ", lists=" + lists + ", dog=" + dog + "]";
}
}
2.输出结果:
3.Value优点
1)支持SpEL表达示(#{11*2})
4.Value缺点
1)不支持复杂类型(如:${person.maps})
四.使用@PropertySource+@ConfigurationProperties把指定配置文件里的值映射到组件中
PropertySource可以加载指定的配置文件(写一个数组加载多个配置文件)
1.Person.java:
package cn.luoruiyuan.test.bean;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value={"classpath:person.properties"})//获取指定配置文件
@ConfigurationProperties(prefix="person")//全局配置文件
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
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> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person [lastName=" + lastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="
+ maps + ", lists=" + lists + ", dog=" + dog + "]";
}
}
2.person.properties(application.yml和application.properties不能写配置信息不然会读全局的)
#配置person的值
person.lastName=11111111111
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=v2
person.lists=a,b,c
person.dog.name=dog
person.dog.age=2
3.输出结果
五.配置文件中使用站位符
1.使用随机数
${random.value}
${random.int}
${random.long}
${random.int(10)}
${random.int[1024,65536]}
2.使用配置中的值,如果没有可以设置默认值
${person.aaa:aaa}
3.输出
赞(1)
赏