SpringBoot:2.配置
Table of Contents
yaml
全称:YAML Ain't Markup Language
主要是以数据为中⼼,⽐json、xml等更适合做配置⽂件;
yaml:
server:
port: 8081
xml:
<server>
<port>8081</port>
</server>
yaml语法
基本语法
k:(空格)v:表⽰⼀对键值对(空格必须有),以空格的缩进来控制层级关系,只要是左对齐的⼀列数据,都是同⼀个层级的,属性和值也是⼤⼩写敏感
server:
context-path: /hello
port: 8082
值的写法
字⾯量:普通的值(数字,字符串,布尔),字符串默认不⽤加上单引号或者双引号
"":双引号;不会转义字符串⾥⾯的特殊字符;特殊字符会作为本⾝想表⽰的意思
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]
配置⽂件值注⼊
@ConfigurationProperties
1.bean定义
/**
* 将配置⽂件中配置的每⼀个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置⽂件中相关的配置进⾏绑定;
*      prefix = "person":配置⽂件中哪个下⾯的所有属性进⾏⼀⼀映射
* 只有这个组件是容器中的组件,才能容器提供的 @ConfigurationProperties 功能;
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
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;
}
杆的组词100个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;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
2.配置⽂件中的配置
person:
lastName: hello
age: 18
properties是什么文件
boss: true
birth: 2017/12/12
maps: {k1: v1,k2: v2}
lists:
- lisi
- zhaoliu
dog:
name: ⼩狗
age: 12
3.jar包导⼊
<!--导⼊配置⽂件处理器,配置⽂件进⾏绑定就会有提⽰-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>    <optional>true</optional>
</dependency>
4.SpringBoot单元测试执⾏&结果
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringbootConfigApplicationTests {
@Autowired
private Person person;
@Test
public void contextLoads() {
System.out.println("======="+person);
}
}
@ImportResource
1.bean定义
public class Student {
private String name;
private Integer age;
linuxshell编程实验public String getName() {
return name;
}
public void setName(String name) {
this.name = name;两张表关联查询sql语句
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.spring的xml配置⽂件定义
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans /schema/beans/spring-beans.xsd">
<bean id="student" class="springbootconfig.bean.Student">
<property name="name" value="tony"></property>
<property name="age" value="12"></property>
</bean>
</beans>
3.配置启动导⼊spring的配置⽂件
@SpringBootApplication
@ImportResource("l")
public class MySpringbootConfigApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringbootConfigApplication.class, args);
}
}
4.执⾏测试&结果
@RunWith(SpringRunner.class)
@SpringBootTest
public class MySpringbootConfigApplicationTests {
@Autowired
private Student student;
@Test
public void contextLoads() {
System.out.println("======="+ student);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。