注入配置文件属性值
properties语法
键(key)=值(value)
1
   | spring.thymeleaf.cache = false
   | 
 
yaml(yml)语法
大小写敏感
key:value
使用空格缩进表示层级
不允许tab键缩进
普通数据
1 2 3 4
   | student:  name: fengyue  age: 20  height: 40
   | 
 
or
1
   | student: {name: fengyue,age: 20}
  | 
 
数组
1 2 3
   | friend:  - zhangsan  - lisi
   | 
 
列表
1 2 3 4 5 6 7
   | friend:  -    name: lisi    age: 20  -     name: zhangsan    age: 20
   | 
 
注入
@Value
value注解可不写set方法,
不支持松散绑定和JSR303数据验证
不提供复杂数据类型
1 2 3 4 5 6 7 8
   | @Component public class Constomer {     @Value("${student.name}")     private String name;     @Value("${student.age}")     private int age; }
 
   | 
 
@ConfigruationProperties(prefix=””)
将properties或者yml配置文件转化为bean来使用的
prefix中写入在配置文件中对应的类名
和@Value不同要些set方法
@ConfigurationProperties会自动匹配对应对应的数据
1 2 3 4 5 6 7
   | @Component @ConfigurationProperties(prefix="constomer") public class Constomer {     private String name;     private int age; }
 
   | 
 
获取
@Componet
在类上使用@Componet表示该类是SpringBoot的组件,将此类标记为Spring容器中的一个Bean
spring的组件扫描就会自动发现它,并且会将其初始化为spring应用上下文中的bean
@Autowired
@Autowired可以标注在属性上、方法上和构造器上,来完成自动装配,依赖注入
自定义配置类
@Configuration和@Bean
@Bean表示标记为spring容器中的一个bean
@Configuration,表示定义一个配置类,有@Componet的功能
springboot会自动扫描配置类,可替代spring中的xml配置文件
1 2 3 4 5 6 7
   | @Configuration public class AutoProper {     @Bean     public String name(){         return "fengyue";     } }
   | 
 
自定义配置文件
properties的配置方法
springboot会自动使用名称为application的配置文件,当要使用其他名称的配置文件时就要手动配置
@PropertySource()引入自定义配置文件,指定配置文件的位置和名字
@EnableConfigurationProperties(Role.class)表示开启对应配置类的属性注入功能其余功能如下
1、让使用了 @ConfigurationProperties 注解的类生效
2、将该类注入到 IOC 容器中,交由 IOC 容器进行管理
注:在下面的例子中体系不出@EnableConfigurationProperties的作用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
   | @Configuration @PropertySource("classpath:mytest.properties") @EnableConfigurationProperties(Role.class) @ConfigurationProperties(prefix = "role") public class Role {     private String name;     private int age;
      @Override     public String toString() {         return "Role{" +                 "name='" + name + '\'' +                 ", age=" + age +                 '}';     }
      public String getName() {         return name;     }
      public void setName(String name) {         this.name = name;     }
      public int getAge() {         return age;     }
      public void setAge(int age) {         this.age = age;     } }
 
   | 
 
yaml的配置方法
yaml的配置需要新建工厂转换类PropertySourceFactory
1 2 3 4 5 6
   | public class MyPropertySourceFactory implements PropertySourceFactory {     @Override     public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {         return new YamlPropertySourceLoader().load(name,resource.getResource()).get(0);     } }
  | 
 
接着就是和properties类似的操作
在@ProperySource中需要指定工厂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
   | @Configuration @PropertySource(value="classpath:mytest.yaml",factory = MyPropertySourceFactory.class) @EnableConfigurationProperties(Role.class) @ConfigurationProperties(prefix = "role") public class Role {     private String name;     private int age;     @Override     public String toString() {         return "Role{" +                 "name='" + name + '\'' +                 ", age=" + age +                 '}';     }
      public String getName() {         return name;     }
      public void setName(String name) {         this.name = name;     }
      public int getAge() {         return age;     }
      public void setAge(int age) {         this.age = age;     } }
   | 
 
@Value中的EL表达式
#{}被包裹的数据可以参与运算
1 2
   | @Value("#{${user.height} + 10}") private int h;
  | 
 
多环境配置
当前使用的时test配置
—该分割线用来分开配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
   | server:   servlet:     context-path: /index spring:   profiles:     active: test ---
  server:   port: 8081   servlet:     context-path: /dev spring:   profiles: dev    --- server:   port: 8081   servlet:     context-path: /test spring:   profiles: test
   | 
 
配置文件之间的优先级
1为最高优先级,优先级依次减低
1.在当前项目目录下的config文件
2.在当前项目目录下
3.在classpath根路径的config目录下
4.在classpath根路径下