SpringBoot–基础–01–配置
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/master/springboot-learn/springboot-1
1、SpringBoot使用一个固定文件名全局的配置文件
application.properties
application.yml
- 配置文件的作用
- 修改SpringBoot自动配置的默认值
2、YAML语法
2.1、基本语法
k:(空格)v:
- 表示一对键值对(空格必须有)
2.2、值的写法
2.2.1、字面量:普通的值(数字,字符串,布尔)
k: v:
- 字面直接来写;
2.2.2、双引号:\表示转义字符
name: "111 \n 222"
等价于
name:111 换行 222
2.2.3、单引号:\表示一般字符。
name: '111 \n 222'
等价于
name: 111 \n 222
2.3、对象、Map,键值对写法
user:
name: zhangsan
age: 20
2.4、数组(List、Set)
users:
‐ user1
‐ user2
‐ user3
3、配置文件值注入
3.1、下面的配置是将application.yml中Person 映射到Person类中
3.2、代码
application.yml
person:
lastName: 小苗
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
‐ user1
‐ user2
dog:
name: 黑狗
age: 12
Person
package fei.zhou.springboot1.business.bean;
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:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定,默认从全局配置文件中获取值。
* prefix = "person":读取配置application.yml中,前缀为person的对象的值。
* @Component:把普通pojo实例化到spring容器中
* 要使用@ConfigurationProperties功能,需要将类实例化到容器里面。
*
*/
@Data
@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;
}
Dog
public class Dog {
private String name;
private Integer age;
}
3.3、导入配置文件处理器依赖,使配置文件和代码进行绑定
<!--导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
这样写配置文件的时候就有提示了。
3.5、 测试
package fei.zhou.springboot1;
import fei.zhou.springboot1.business.bean.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Test1 {
@Autowired
Person person;
@Test
void t1() {
System.out.println(person);
}
}
4、@Value的使用
- 给字段设置值
- 和@ConfigurationProperties比较
4.1、形式
-
value=“字面量”
-
value=“${key}”
- 从环境变量、配置文件中获取值
-
value=“#{SpEL}”
- 表达式设置值
4.2、案例1
Person2
package fei.zhou.springboot1.business.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import lombok.Data;
@Component
@Data
public class Person2 {
// 从环境变量、配置文件中获取值
@Value("${person.lastName}")
private String lastName;
// #{SpEL}
@Value("#{2*2}")
private Integer age;
// 字面量
@Value("false")
private Boolean boss;
}
Test1
package fei.zhou.springboot1;
import fei.zhou.springboot1.business.bean.Person;
import fei.zhou.springboot1.business.bean.Person2;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Test1 {
@Autowired
Person person;
@Autowired
Person2 person2;
@Test
void t1() {
System.out.println(person);
}
@Test
void t2() {
System.out.println(person2);
}
}
4.3、案例2
ElConfig
package fei.zhou.springboot1.business.bean.demo1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
@Configuration
@ComponentScan("fei.zhou.springboot1.business.bean.demo1")
//注入配置文件
@PropertySource("classpath:demo1/test.properties")
public class ElConfig {
//常量
@Value("111")
private String normal;
//注入操作系统属性
@Value("#{systemProperties['os.name']}")
private String osName;
//注入表达式结果
@Value("#{ T(java.lang.Math).random() * 100.0 }")
private double randomNumber;
//注入其他bean属性
@Value("#{person3.lastName}")
private String fromAnother;
//注入文件资源
@Value("classpath:demo1/1.txt")
private Resource testFile;
//注入网站资源
@Value("http://www.baidu.com")
private Resource testUrl;
//注入配置文件
@Value("${book.name}")
private String bookName;
@Autowired
private Environment environment; //7
@Bean //7
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer();
}
@Override
public String toString() {
return "ElConfig{" +
"normal='" + normal + '\'' +
", osName='" + osName + '\'' +
", randomNumber=" + randomNumber +
", fromAnother='" + fromAnother + '\'' +
", testFile=" + testFile +
", testUrl=" + testUrl +
", bookName='" + bookName + '\'' +
", environment=" + environment +
'}';
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext(ElConfig.class);
ElConfig elConfig = context.getBean(ElConfig.class);
System.out.println( "------------");
System.out.println( elConfig);
context.close();
}
}
Person3
package fei.zhou.springboot1.business.bean.demo1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Person3 {
@Value("Person3-lastName")
private String lastName;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
1.txt
111111111
test.properties
book.name=spring boot
5、读取配置文件
5.1、 @PropertySource:加载指定的配置文件,在配置类上设置
Person4
package fei.zhou.springboot1.business.bean.demo2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import lombok.Data;
@Component
//组合使用,但是ConfigurationProperties的优先级更高
@PropertySource("classpath:demo2/person4.properties")
@ConfigurationProperties(prefix = "person")
@Data
public class Person4 {
private String lastName;
private Integer age;
private Boolean boss;
}
person4.properties
person.lastName=小明
person.age=12
application.yml
person:
# lastName: 小苗
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
‐ user1
‐ user2
dog:
name: 黑狗
age: 12
5.2、 ImportResource:导入Spring的配置文件,让配置文件里面的内容生效;
Springboot1Application
@SpringBootApplication
@ImportResource(locations = {"classpath:demo3/bean.xml"})
public class Springboot1Application {
public static void main(String[] args) {
SpringApplication.run(Springboot1Application.class, args);
}
}
Dog3
@Data
public class Dog3 {
private String name;
private Integer age;
}
bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dog3" class="fei.zhou.springboot1.business.bean.demo3.Dog3">
<property name="age" value="12"></property>
<property name="name" value="哈巴狗"></property>
</bean>
</beans>
6、配置文件占位符
6.1、随机数:RandomValuePropertySource
- ${random.value}
- ${random.int}
- ${random.long}
- ${random.int(10)}
- ${random.int[1024,65536]}
6.2、属性配置占位符
lastName2: ${person.lastName}2