Spring Boot之配置

Updated on with 0 views and 0 comments

配置文件

SpringBoot 使用一个全局的配置文件,配置文件名是固定的;用来修改SpringBoot自动配置的默认值

application.properties

跟 yml 文件类似,二者选择其一使用

person.age=20
#map的配置形式,每个键值对分开配置
person.map.k1=v1
person.map.k2=v2
#list的配置形式,每个元素用逗号隔开
person.list=a,b,c
#对象的配置形式,对象中的每个属性分开赋值
person.dog.name=dog
person.dog.age=10
application.yml

YAML(YAML Ain't Markup Language)

标记语言:

以前的配置文件大多都是 XML 文件

YAML:以数据为中心,比 JSON、XML 等更适合做配置文件

server:
  port: 8081
基本语法

k: v:表示一对键值对(值前面必须有一个空格),以空格的缩进来控制层级关系,只要是左对齐的一列数据都是同一个层级的

server:
  port: 8081
  path: /hello

属性和值也是大小写敏感的

值的写法
  • 字面量:普通的值(数字,字符串,布尔):k: v,字符串默认不加单引号或者双引号

    • 双引号:不会转义字符串中的特殊字符,特殊字符会作为本身想表达的意思,eg:name: "zhangsan \n lisl",这里面的\n 会表示成换行
    • 单引号:会转义特殊字符,特殊字符最终只是一个普通的字符串数据,上面所说的那一句会原样输出
  • 对象、Map(属性和值)

    • k: v:还是键值对形式,在下一行来写对象和值的关系
    <!-- 常规写法 -->
    friends:
        lastName: zhangsan
        age: 20
    <!-- 行内写法 -->
    friends: {lastName: zhangsan,age: 18}
    
  • 数组(List、set):用'- '值表示数组中的一个元素

    <!-- 常规写法 -->
    pets:
        - cat
        - dog
        - pig
    <!-- 行内写法 -->
    pets: [cat,dog,pig]
    
导入配置文件处理器
<!-- 导入配置文件处理器,配置文件进行绑定会有提示 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
获取值

方式一:

/*
 * 将配置文件中配置的每一个属性的值,映射到这个组件中
 * @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 Map<String, Object> map;
    private List<Object> list;
    private Dog dog;
}

方式二:

@Value:在 XML 中取值可以使用下面的三种方式去取值,用这个注解就与在 XML 中一样,可以获取配置文件中的值

/*
 * <bean class = "Person">
 *      <property name = "lastName" value = "字面量/${key}从环境变量、配置文件中取值、#{SpEL}"></property>
 * </bean>
 */
@Value("${person.last-name}")
private String lastName;

| | @ConfigurationProperties | @Value |
| - | - | - |
| 功能 | 批量注入配置文件中的属性 | 一个一个指定 |
| 松散绑定 | - 可以表示接下来的一个字母是大写,_ 表示小写 | 不支持 |
| SpEL | 不支持 | 支持 |
| JSR303 数据校验 | 支持 | 不支持 |
| 复杂类型封装 | 支持 | 不支持 |

JSR303 校验:

@Validated  //类上加Validated注解

@Email      //属性上可以加上校验注解,这个是代表注入的值必须是email格式的

总结:如果说我们只是在某个业务逻辑中需要获取一下配置文件中的你某个值,使用 @value 注解,如果说我们专门编写了一个 JavaBean 来和配置文件进行映射,就使用 @ConfigurationProperties

配置文件占位符

1.随机数

random.value,{random,int},${random.long}、random.int(10),{random.int[102,65536]

2.占位符获取之前配置的值,如果没有,可以指定默认值

${person.hello:hello}:冒号后面的为指定的默认值

person.age=20
person.last-name=ruohui${random.uuid}
person.map.k1=v1
person.list=a,b,c
person.dog.name=${person.hello:hello} ${person.last-name}'s dog
person.dog.age=${random.int}
Profile多环境支持

Profile:切换环境使用,测试环境用测试环境,生产环境用生成环境

1.多 Profile 文件

文件名可以是:application-{profile}.properties/yml

默认使用 application.properties

2.yml 支持多文档块的方式

---:是文档分块标志,上面的为一个文档,下面的为一个文档

server:
  port: 8082
spring:
  profiles:
    active: dev
---
server:
  port: 8083
spring:
  profiles: dev
---
server:
  port: 8084
spring:
  profiles: prod
3.激活指定 profile

(1)在配置文件中指定 spring.profiles.active=dev,就可以激活 application-dev.properties

(2)命令行:--spring.profiles.active=dev

(3)虚拟机参数:-Dpring.profiles.active=dev

配置文件加载位置

Spring boot 启动会扫描以下位置的 application.properties 或者 application.yml 文件作为 SpringBoot 的默认配置文件

  • -file:./config/:当前目录下的 config 文件夹,该文件夹跟 src 文件夹是同级的关系
  • -file:./:当前目录下,跟 pom 文件是同一级
  • classpath:/config/:resources 下的 config 文件夹
  • classpath:/:resources 文件夹下

以上优先级从高到低,所有位置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容,我们也可以通过 spring.config.location 改变默认配置

项目打包好之后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件形成互补配置

java -jar jar包名 --spring.config.location=配置文件完整路径

外部配置文件加载顺序

SpringBoot 也可以从以下位置加载配置; 优先级从高到低;高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置

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 指定的默认属性

所有支持的配置加载来源;

参考官方文档

@PropertySource & @ImportResource

@PropertySource

@PropertySource:读取指定配置文件,这个注解要跟 @ConfigurationProperties 一起使用

@Component
@EnableConfigurationProperties(Person.class)
@ConfigurationProperties(prefix = "person")
@PropertySource("classpath:person.properties")
public class Person {
}
@ImportResource

导入 Spring 的配置文件,让配置文件里面的内容生效

Spring Boot 里面没有 Spring 的配置文件,自己编写的配置文件,也不能自动识别

想让 Spring 的配置文件生效,加载进来,就需要使用这个注解,标注在一个配置类上,可以是主程序

@ImportResource(locations = {"classpath:bean.xml"})

原先是使用 XML 配置文件的方式添加组件,SpringBoot 不推荐这种方式

SpringBoot 推荐给容器添加组件的方式:推荐使用注解的方式

@Configuration   //标明这是一个配置类
public class MyAppConfig {
    @Bean   //将方法的返回值添加到容器中,容器中的这个组件默认的id就是方法名
    public HelloService helloService() {
        return new HelloService();
    }
}

自动配置原理

SpringBoot 启动的时候加载主配置类,开启自动配置功能 @EnableAutoConfiguration

@EnableAutoConfiguration 作用:

  • 利用EnableAutoConfigurationImportSelector 给容器中导入一些组件
  • 可以查看 selectImports()方法的内容
  • List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);:获取候选的配置
    //扫描所有jar包类路径下的META-INF/spring.factories,把扫描到的文件包装成properties对象,从properties中获取到EnableAutoConfiguration.class(类名对应的值)的值,然后把他们添加到容器中
    List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
    

将类路径下的 META-INF/spring.factories 里面配置的所有 EnableAutoConfiguration 的值加入到了容器中 ,每一个 XXXAutoConfiguration 类都是容器中的一个组件,都加入到容器中,用他们来做自动配置

以 HttpEncodingAutoConfiguration(Http 编码自动配置)为例
@Configuration(   //表示这是个配置类,以前编写的配置文件一样,也可以给容器中添加组件
    proxyBeanMethods = false
)
@EnableConfigurationProperties({HttpProperties.class})   //启动指定类的configurationProperties功能,将配置文件中对应的值和HttpEncodingAutoConfiguration绑定起来;并把HttpEncodingProperties加入到IOC容器中
@ConditionalOnWebApplication(   //spring底层@Conditional注解,根据不同的条件,如果满足指定的条件,整个配置类里面的配置就好生效 ---> 判断是否web应用,是则生效
    type = Type.SERVLET
)
@ConditionalOnClass({CharacterEncodingFilter.class})   //判断当前当前项目有没有CharacterEncodingFilter这个类
@ConditionalOnProperty(   //判断配置文件中是否存在某个配置 --> spring.http.encoding.enabled;matchIfMissing表示如果不存在也认识生效
                        //即使配置文件中不配置spring.http.encoding.enabled=true,也是默认生效的
    prefix = "spring.http.encoding",
    value = {"enabled"},
    matchIfMissing = true
)
public class HttpEncodingAutoConfiguration {
    //已经和springboot的配置文件映射了
    private final Encoding properties;

    //只有一个有参构造器的情况下,参数的值会直接从容器中拿
    public HttpEncodingAutoConfiguration(HttpProperties properties) {
        this.properties = properties.getEncoding();
    }


    @Bean    //给容器中添加一个组件,这个组件的某些值需要从properties中获取
    @ConditionalOnMissingBean
    public CharacterEncodingFilter characterEncodingFilter() {
        CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
        filter.setEncoding(this.properties.getCharset().name());
        filter.setForceRequestEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.REQUEST));
        filter.setForceResponseEncoding(this.properties.shouldForce(org.springframework.boot.autoconfigure.http.HttpProperties.Encoding.Type.RESPONSE));
        return filter;
    }
}

根据当前不同条件判断,决定这个配置类是否生效,一旦这个配置类生效;这个配置类就会给容器中添加各种组件,这些组件的属性是从对应的 properties 类中获取的,这些类中的每一个属性又是和配置文件绑定的

所有在配置文件中能配置的属性都是在 xxxProperties 类中封装着,配置文件能配置什么就可以参照某个功能对应的这个属性类

@ConfigurationProperties(  //从配置文件中获取指定的值和bean属性进行绑定
    prefix = "spring.http"
)
public class HttpProperties {

}

精髓:

1.SpringBoot 启动会加载大量的自动配置类

2.我们需要的功能有没有 SpringBoot 默认写好的自动配置类

3.我们再来看这个自动配置类到底配置了哪些组件(只要我们要用的组件有,我们就不需要配置了)

4.给容器中自动配置类添加组件时,会从 properties 类中获取某些属性,我们就可以在配置文件中指定这些属性的值

xxxxAutoConfigurartion:自动配置类;

给容器中添加组件

xxxxProperties:封装配置文件中相关属性;

@Conditional 派生注解(Spring 注解版原生的@Conditional 作用)

作用:必须是@Conditional 指定的条件成立,才给容器中添加组件,配置配里面的所有内容才生效;

@Conditional 扩展注解作用(判断是否满足当前指定条件)
@ConditionalOnJava系统的 Java 版本是否符合要求
@ConditionalOnBean容器中存在指定 Bean;
@ConditionalOnMissingBean容器中不存在指定 Bean;
@ConditionalOnExpression满足 SpEL 表达式指定
@ConditionalOnClass系统中有指定的类
@ConditionalOnMissingClass系统中没有指定的类
@ConditionalOnSingleCandidate容器中只有一个指定的 Bean,或者这个 Bean 是首选 Bean
@ConditionalOnProperty系统中指定的属性是否有指定的值
@ConditionalOnResource类路径下是否存在指定资源文件
@ConditionalOnWebApplication当前是 Web 环境
@ConditionalOnNotWebApplication当前不是 Web 环境
@ConditionalOnJndiJNDI 存在指定项

自动配置类必须在一定的条件下才能生效;

我们怎么知道哪些自动配置类生效;

== 可以通过在配置文件中启用 debug=true 属性;来让控制台打印自动配置报告 ==,这样我们就可以很方便的知道哪些自动配置类生效;

=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:(自动配置类启用的)
-----------------

   DispatcherServletAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition)


Negative matches:(没有启动,没有匹配成功的自动配置类)
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

   AopAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)