基础

配置文件

SpringBoot默认使用一个全局的配置文件,application*.yaml,这个配置文件的名称是固定的。

  • application.properties
    • 语法:key = value
  • application.yaml
    • 语法:key: value

配置文件的作用:修改SpringBoot的默认值,其实很多配置SpringBoot都已经配置好了,偶尔我们需要
修改一些内容就可以通过这个配置。例如:服务器端口。

yaml语言以数据作为中心 数据:对象、map、list、key/value

基础语法学习

  • 语法要求很严格
  • 缩进严格要求
  • 大小写严格要求

字符串说明:
双引号转义字符会生效
单引号转义支付不会生效

对象、map:

  1. key:
  2. v1: 1
  3. v2: 2

例子

  1. student:
  2. name: zhangsan
  3. age: 3
  4. happy: false

缩减为行内写法:就和JSON配置一样!

  1. student:{name: coding,age: 3}

list 、数组:

  1. key:
  2. - v1
  3. - v2
  4. - v3

例子

  1. pets:
  2. - dog
  3. - cat
  4. - pig

缩减为行内写法:就和JSON配置一样

  1. pets: {dog,cat,pig}

在SpringBoot中,yaml配置和properties 共同使用,yaml配置些复杂的配置,properties配置些简单的配置
properties的优先级大于yaml配置!

注入配置文件

传统的配置文件:set、get
我们可以使用 pojo学习下配置类的写法!

重点注意:

@ConfiguratrionProperties 作用: 1、绑定配置文件中的对象 2、将对象的属性值和配置文件的值一一对应,然后注入 3、这是一种批量注入的方式,更加快捷和方便,推荐使用 思想: 我们编写的配置文件如果存在值,就使用配置文件中的值 如果不存在,就使用默认值或者null

加载指定的配置文件

@PropertySource() 加载指定的配置文件

  1. @PropertySource(value="classpath:person.properties")
  2. public class Person{
  3. @Value("${name}")
  4. private String name ;
  5. }
  1. //person.properties 文件
  2. name=zhangsan

yaml 配置文件可以编写一些随机数!占位符

  1. person:
  2. name: zhangsan${random.int}
  3. age: ${random.int}
  4. dog:
  5. name: ${person.age:ohter}_旺财 //如果person.age有值则显示,如果不存在则显示other
  6. age: 2

回复传统过的Propertis配置!

1、properties 一定要注意乱码问题!
2、测试properties配置!
image.png

对比区别

properties和yaml的区别:

@Value @ConfigurationProperties
功能 一个个指定 批量注入
松散绑定 不支持 支持
SpEL 支持 不支持,自己的占位符
JSR303数据校验 不支持 支持
复杂类型封装 不支持 支持

JSR303数据校验

验证我们的输入格式是否正确

  1. @Validated
  2. public class Person{
  3. @Email
  4. private String Email; //如果邮箱不是Email类型的就会报错
  5. }

类似的 可以有如下校验
image.png
在SpringBoot底层所有配置类都是用的@ConfigurationProperties

最佳实践

1、假设配置类十分简单,@Value,如果只需要获取个别值,@Value 依旧最高效
2、配置繁琐,使用@ConfigurationProperties

多环境配置

什么是多环境配置

真实开发的时候配置文件一定不止一个;
application-dev.properties
application-test.properties
application-pro.properties

传统的方式

image.png

yaml配置

yaml不需要多个配置文件,只需要多文档块;

  1. server:
  2. port: 8080
  3. spring:
  4. profiles:
  5. active: prod # 启用生产环境
  6. # 使用 ---分割文档块
  7. ---
  8. server:
  9. port: 8081
  10. spring:
  11. profiles:
  12. active: dev
  13. ---
  14. server:
  15. port: 8082
  16. spring:
  17. profiles:
  18. active: prod

如果yaml和propertis 都配置了,那么优先级还是properties;

自动配置原理

我们可以配置的文件内容十分之多。到底是怎么配置的呢?

原理

例子:HttpEncodingAutoConfiguration

  1. //Configuration 表示就是一个配置文件
  2. @Configuration(proxyBeanMethods = false)
  3. //指定配置文件,HttpProperties 对应我们编写的配置文件,假设配置文件中有就用配置文件的,
  4. //没有就用默认值
  5. @EnableConfigurationProperties(HttpProperties.class)
  6. //@ConditionalOnXXX Spring底层注解;条件判断注解,通过条件来判断这个类是否生效!详见下图
  7. @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
  8. // 如果存在CharacterEncodingFilter这个类,这个配置类才生效
  9. @ConditionalOnClass(CharacterEncodingFilter.class)
  10. @ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
  11. public class HttpEncodingAutoConfiguration {
  12. //注入绑定HttpProperties 配置文件
  13. private final HttpProperties.Encoding properties;
  14. public HttpEncodingAutoConfiguration(HttpProperties properties) {
  15. this.properties = properties.getEncoding();
  16. }
  17. //注入对应的Bean
  18. //如果自己配置了配置文件,就会注入到SpringBoot,自动帮我们配置到bean中。
  19. //注册bean的时候,springboot 自动帮我们关联了HttpProperties
  20. //我们编写的配置就可以直接生效了。
  21. @Bean
  22. @ConditionalOnMissingBean
  23. public CharacterEncodingFilter characterEncodingFilter() {
  24. CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
  25. filter.setEncoding(this.properties.getCharset().name());
  26. filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
  27. filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
  28. return filter;
  29. }
  30. @Bean
  31. public LocaleCharsetMappingsCustomizer localeCharsetMappingsCustomizer() {
  32. return new LocaleCharsetMappingsCustomizer(this.properties);
  33. }
  34. private static class LocaleCharsetMappingsCustomizer
  35. implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>, Ordered {
  36. private final HttpProperties.Encoding properties;
  37. LocaleCharsetMappingsCustomizer(HttpProperties.Encoding properties) {
  38. this.properties = properties;
  39. }
  40. @Override
  41. public void customize(ConfigurableServletWebServerFactory factory) {
  42. if (this.properties.getMapping() != null) {
  43. factory.setLocaleCharsetMappings(this.properties.getMapping());
  44. }
  45. }
  46. @Override
  47. public int getOrder() {
  48. return 0;
  49. }
  50. }
  51. }

image.png

精髓

1、SpringBoot 启动会加载大量的自动配置类!spring.factories
2、我们就需要判断我们的类是否存在在这里面,如果不存在我们需要手动导入,如果存在导入启动器即可。
3、我们的配置文件之所以可以自动配置生效:

  • xxxxAutoConfiguration:自动配置类,根据条件@ConditionXXX 判断是否生;如果生效则成功注入bean
  • xxxxProperties:封装配置文件中的相关属性;

4、给容器中自动配置类配置属性的时候,会通过xxxProperties 类来获取某用户配置文件中的属性,如果没有则使用默认的,如果有则使用自动配置 !

我们怎么知道哪些类加载了呢?

debug =true

虽然说启动的时候,加载了大量的类,但是不一定剩下,因为条件不满足。
**