02.SpringBoot 之配置文件注入

1. 配置文件

  • SpringBoot 使用全局的配置文件,配置文件名是固定的:application.properties 或者 application.yml

  • 这两个配置文件可以同时存在,如果配置相同的属性,properties 会覆盖 yml

2. yml 语法

2.1 基本语法

  • k: v:表示一对键值对,注意 value 前面必须要有空格

  • 以空格的缩进来控制层级关系;只要是左对齐的一列数据,都是同一个层级的

server:
  port: 8081
  servlet:
    context-path: /demo

2.2 值的写法

2.2.1 字面量:普通的值(数字,字符串,布尔)
  • k: v:字面直接来写

  • "":双引号;不会转义字符串里面的特殊字符;特殊字符会作为本身想表示的意思,如下会输出 zhangsan 换行 lisi

name: "zhangsan \n lisi"
  • '':单引号;会转义特殊字符,特殊字符最终只是一个普通的字符串数据,如下会输出 zhangsan \n lisi
name: 'zhangsan \n lisi'
2.2.2 对象、Map(属性和值)(键值对)
  • k: v:在下一行来写对象的属性和值的关系;注意缩进
people:
	lastName: zhangsan
	age: 20
	
people: {lastName: zhangsan,age: 18}
2.2.3 数组(List、Set)
  • - 值表示数组中的一个元素
pets:
    - cat
    - dog
    - pig

pets: [cat,dog,pig]

3. 配置文件值注入

代码已经上传至 https://github.com/masteryourself-tutorial/tutorial-spring ,详见 tutorial-spring-boot-core/tutorial-spring-boot-config 工程

3.1 配置文件

1. application.properties
person.last-name=张三
person.age=18
person.boss=false
person.birth=2018/12/12
person.maps.k1=v1
person.maps.k2=12
person.lists=lisi,19,wahaha
person.dog.name=wangwang
person.dog.age=3

3.2 代码

1. PersonProperties
  • @ConfigurationProperties:告诉 SpringBoot 将本类中的所有属性和配置文件中相关的配置进行绑定

  • prefix = “person”:配置文件中的前缀

  • 只有这个组件是容器中的组件,才能容器提供的 @ConfigurationProperties 功能,也可以使用 @EnableConfigurationProperties(PersonProperties.class) 导入组件

@Data
@ConfigurationProperties(prefix = "person")
// @Component
public class PersonProperties {

    private String lastName;

    private Integer age;

    private Boolean boss;

    private Date birth;

    private Map<String, Object> maps;

    private List<Object> lists;

    private DogProperties dog;

}
2. DogProperties
@Data
public class DogProperties {

    private String name;

    private Integer age;

}
3. ConfigApplication
  • 这里使用 @EnableConfigurationProperties(PersonProperties.class)PersonProperties 导入到 Spring 容器中
@SpringBootApplication
@EnableConfigurationProperties(PersonProperties.class)
public class ConfigApplication implements EnvironmentAware, ApplicationContextAware {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }

    @Override
    public void setEnvironment(Environment environment) {
        System.out.println("test.name1" + environment.getProperty("test.name1"));
        System.out.println("test.name2" + environment.getProperty("test.name2"));
        System.out.println("test.name3" + environment.getProperty("test.name3"));
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println(applicationContext.getBean(PersonProperties.class));
    }
    
}

3.3 @Value & @ConfigurationProperties 比较

@ConfigurationProperties @Value
功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法) 支持 不支持
SpEL 不支持 支持
JSR303数据校验 支持 不支持
复杂类型封装 支持 不支持
  • 如果只是在某个业务逻辑中需要获取一下配置文件中的某项值,推荐使用 @Value

  • 如果专门编写了一个 JavaBean 来和配置文件进行映射,推荐使用 @ConfigurationProperties

4. @PropertySource & @ImportResource 的区别

4.1 @PropertySource

  • 加载指定的配置文件
4.1.1 配置文件
1. teacher.properties
teacher.name=李四老师
teacher.age=18
4.1.2 代码
2. ConfigApplication
@SpringBootApplication
@EnableConfigurationProperties(PersonProperties.class)
@PropertySource(value = {"classpath:teacher.properties"})
public class ConfigApplication implements EnvironmentAware, ApplicationContextAware {

4.2 @ImportResource

  • 导入 Spring 的配置文件
4.2.1 配置文件
1. teacher.properties
<bean id="springBeanService" class="pers.masteryourself.study.spring.boot.config.service.SpringBeanService"/>
4.2.2 代码
2. ConfigApplication
@SpringBootApplication
@EnableConfigurationProperties(PersonProperties.class)
@PropertySource(value = {"classpath:teacher.properties"})
@ImportResource(value = {"classpath:spring-bean.xml"})
public class ConfigApplication implements EnvironmentAware, ApplicationContextAware {
发布了37 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/masteryourself/article/details/105016435