目录
- 1.yaml语法
- 值的写法:
- 2.配置文件注入
- 1.@ConfigurationProperties注解注入
- 2.@Value注解注入
- 3.@PropertySource
- 3.配置类注入
1.yaml语法
k:(空格)v:表示一对键值对(空格必须有);
以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的
例如:
server: port: 8080 path: /hello
值的写法:
字面量:普通的值(数字,字符串,布尔)
k: v:字面直接来写;
字符串默认不用加上单引号或者双引号;
"":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思
name: "zhangsan \n lisi":输出;zhangsan 换行 lisi
'':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据
name: ‘zhangsan \n lisi’:输出;zhangsan \n lisi
对象、Map(属性和值)(键值对):
k: v:在下一行来写对象的属性和值的关系;注意缩进
对象还是k: v的方式
friends: lastName: zhangsan age: 20
行内写法:
friends: {lastName: zhangsan,age: 18}
数组(List,set):
用 - 值表示数字当中的一个元素
pets: - cat - dog - pig
行内写法:
pets: [cat,dog,pig]
2.配置文件注入
1.@ConfigurationProperties注解注入
我们尝试把配置文件当中的值注入到bean里面:
实体类:
package com.qcby.domain;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PrjavascriptopertySource;
import org.springframework.stereotype.Component;
import Javax.annotation.PostConstruct;
import java.util.*;
/**
*把配置文件里的每一个值映射到这组件中
*@ConfigurationProperties:告诉Spring把这个类中的所有属性和配置文件中的配置绑定
*perfix="person" 配置文件中person的所有属性进行一一映射
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Mwww.devze.comap<String,Object> ma编程ps;
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<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getBirthString() {
return birthString;
}
public void setBirthString(String birthString) {
this.birthString = birthString;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
配置文件:
person:
lastName: 232222
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
name: 小狗
age: 12
在Controller中返回这个对象:
@Autowired
private Person person;
@GetMapping("/person")
@ResponseBody
public Person selectPerson(){
return person;
}
然后运行启动类

2.@Value注解注入
package com.qcby.domain;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
//直接赋值
@Value("Hello")
private String lastName;
@Value("18")
private Integer age;
@Value("false")
private Boolean boss;
//Date类型用Value需要转换
@Value("2017/12/12")
private String birthString;
private Date birth;
//要手动初始化(创建对象)
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
// 使用 @PostConstruct 在 Bean 初始化后处理复杂类型
@PostConstruct
public void init() {
//处理 Date 类型
try {
this.birth = new Date(birthString);
} catch (Exception e) {
this.birth = new Date(); // 默认当前时间
}
// 初始化 Map
this.maps = new HashMap<>();
maps.put("k1", "v1");
maps.put("k2", "v2");
//初始化 List
this.lists = Arrays.asList("lisi", "zhaoliu");
//创建 Dog 对象
this.dog = new Dog();
dog.secsltatName("小狗112312");
dog.setAge(12);
}
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<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getBirthString() {
return birthString;
}
public void setBirthString(String birthString) {
this.birthString = birthString;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
@Value后面的括号里的值可以直接对这些属性赋值:
1513008000000 是一个时间戳,表示从 1970年1月1日 00:00:00 UTC 到指定时间的毫秒数
value也可以使用占位符来获取配置文件里面的值:
package com.qcby.domain;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;
@Component
public class Person {
@Value("${person.lastName:Hello1231}")
private String lastName;
@Value("${person.age:183}")
private Integer age;
@Value("${person.boss:true}")
private Boolean boss;
@Value("${person.birth:2016/12/31}")
//Date类型用Value需要转换
private String birthString;
private Date birth;
//要手动初始化(创建对象)
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
// 使用 @PostConstruct 在 Bean 初始化后处理复杂类型
@PostConstruct
public void init() {
//处理 Date 类型
try {
this.birth = new Date(birthString);
} catch (Exception e) {
this.birth = new Date(); // 默认当前时间
}
// 初始化 Map
this.maps = new HashMap<>();
maps.put("k1", "v1");
maps.put("k2", "v2");
//初始化 List
this.lists = Arrays.asList("lisi", "zhaoliu");
//创建 Dog 对象
this.dog = new Dog();
dog.setName("小狗112312");
dog.setAge(12);
}
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;
}
pucsltablic List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getBirthString() {
return birthString;
}
public void setBirthString(String birthString) {
this.birthString = birthString;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}

区别:

3.@PropertySource
@PropertySource可以加载指定的配置文件
package com.qcby.domain;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.*;
@PropertySource("classpath:person.properties")
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private String birthString;
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<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getBirthString() {
return birthString;
}
public void setBirthString(String birthString) {
this.birthString = birthString;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
想让person.properties生效的话,要先把application.yaml文件中有关person的配置去掉,因为application.yaml读取优先级更高,高优先级的配置会覆盖低优先级的配置
person.lastName=1222222222222222222222222222
person.age=${random.int(10)}
person.boss=true
person.birthString=2017/12/10
person.birth=2017/12/10
person.maps.k1=v1
person.maps.k2=v2
person.lists[0]=lisi
person.lists[1]=zhaoliu
person.dog.name=大狗
person.dog.age=12

3.配置类注入
想让Spring的配置文件生效,我们可以在配置类上使用@ImportResource注解
package com.qcby.config;
import com.qcby.domain.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//指明是一个配置类,代替之前的Spring配置文件
@Configuration
public class MyConfiguration {
@Bean
public HelloService helloService(){
System.out.println("配置类给容器添加组件了.......");
return new HelloService();
}
}

到此这篇关于SpringBoot配置文件注入值的简单实现的文章就介绍到这了,更多相关Springboot配置文件值注入内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
加载中,请稍侯......
精彩评论