Spring Boot 学习笔记(三)
@PropertySource()&ImportResource
@PropertySource:加载指定的配置文件;
/**
* ClassName: Person
* Description:
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* date: 2019/11/29 16:17
*@ConfigurationProperties :告诉Spring Boot将本类中的所有属性和配置文件中相关的配置进行绑定
* prefix = "person":配置文件中那个下面的所有属性进行一一映射
*
* 只有这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties功能
* @ConfigurationProperties(prefix = "person") 默认从全局配置文件中获取值
* @since JDK 1.8
*/
@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
@Validated
public class Person {
/**
* <bean class="Person>
* <property name="lastName" value="字面量"/${key}从环境变量、配置文件中获取值/#{SPEL}"></property>
* </bean>
*/
//lastName必须是邮箱格式
// @Email
// @Value("${person.last-name}")
private String lastName;
// @Value("#{11*2}")
private Integer age;
// @Value("true")
private Boolean boss;
private Date birth;
// @Value("${person.maps}")
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@ImportResource: 导入Spring的配置文件,让配置文件里面的内容生效;
Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上
@ImportResource(locations = {"classpath:beans.xml"})
导入Spring的配置文件让其生效
Spring Boot推荐给容器中添加组建的方式:
不来编写Spring配置文件
<?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="helloService" class="com.liu.springboot.service.HelloService"></bean>
</beans>
1.配置类-------Spring的配置文件
2.使用@Bean给容器中添加组件
/**
* @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
*
* 在配置文件中用<bean></bean>标签添加组件
*/
@Configuration
public class MyAppConfig {
/**
* @Bean:将方法的返回值添加到容器中;容器中的这个组件默认的id就是方法名
* @return
*/
@Bean
public HelloService helloService02(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
}
4.配置文件占位符
1.随机数
random.value、{random.int}、${random.long}
random.int(10)、{random.int[1024,65536]}
2.占位符获取之前配置的值,如果没有可以使用:指定的默认值
person.last-name=张三${random.uuid}
person.age=${random.int}
person.birth=2018/10/18
person.boss=true
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello}_dog
person.dog.age=15
5.Profile
1.多Profile文件
我们在主配置文件的编写的时候,文件名可以是 application-{profile}.properties/yml
默认使用application.properties的配置;
2.yml支持对文挡块方式
server:
port: 8081
spring:
profiles:
active: prod
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: prod #指定属于那个环境
3.激活指定的Profile
1.在配置文件中指定spring.profiles.active=dev(profile的名称)
2.命令行:
java -jar spring-boot-02-configs-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
可以直接在测试的时候,配置传入命令行的参数
3.虚拟机参数:
-Dspring.profiles.active=dev
6.配置文件加载位置
springboot启动会扫描以下位置的application.properties或者application.yml文件作为springBoot的默认配置文件
file:./config/
file:./
classpath:/config/
classpath:/
优先级由高到低,高优先级会覆盖低优先级的配置
Spring Boot或从这四个位置加载配置文件;互补配置;
我们还可以通过spring.config.location来改变默认的配置文件的位置
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来制定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --spring.config
.location=D:/application.properties
7.外部配置加载顺序
Spring Boot也可以从以下位置加载配置;优先级从高到低;高优先级的配置覆盖低优先级的配置,所有配置形成互补配置
1.命令行参数
java -jar spring-boot-02-config-02-0.0.1-SNAPSHOT.jar --server.port=8087 --server.context-path=/abc
多个配置用空格分开; --配置项=值
2.来自java:comp/env的JNDI属性
3.Java系统属性(System.getProperties())
4.操作系统环境变量
5.RandomValuePropertySource配置的random.*属性值
都是由jar包外向jar包内进行寻找
优先加载带profile
6.jar保外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
7.jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
再来加载不带profile
8.jar包外部的application.properties或application.yml(不带spring.profile)配置文件
9.jar包内部的application.properties或application.yml(不带spring.profile)配置文件
10.@Configuration注解类上的@PropertySource
11.通过SpringApplication.setDefaultProperties指定的默认属性