SpringBoot配置相关

1.配置文件注入

  在SpringBoot里可以通过四个注解进行配置文件的注入,分别是:

  **@ConfigurationProperties  @Value**  @PropertySource@ImportResource

  1.@ConfigurationProperties 使用方式

 居中比如我的配置文件application.yml里面是这种

person:
     lastName: hello
     age: 18
     boss: false
     birth: 2017/12/12
     maps: {k1: v1,k2: 12}
     lists:
      - lisi
      - zhaoliu
     dog:
       name: 小狗
       age: 12

 JavaBean

/**
    - 将配置文件中配置的每一个属性的值,映射到这个组件中
    - @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;prefix = "person":指定 配置文件中哪个前缀下面的所有属性进行 一一 映射
        - 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能,
        - 使其成为组件可以通过下面的两种中的任何一种方式
            1.@Component //如果这里添加了注解那么在自动配置类的时候就不用添加    
            2.@EnableConfigurationProperties(Person.class)注解.
     */
    @Component
    @ConfigurationProperties(prefix = "person")
    public class Person {
private String lastName;
        private Integer age;
        private Boolean boss;
        private Date birth;
private Map<String,Object> maps;
        private List<Object> lists;
        private Dog dog;
    }
 2.@Value 使用方式

 @Value 只能从application.yml中读取

@Component
    @ConfigurationProperties(prefix = "person")
    @Validated
    public class Person {
//lastName必须是邮箱格式
        @Email
        @Value("${person.last-name}")  
        private String lastName;
        @Value("#{11*2}")
        private Integer age;
        @Value("true")
        private Boolean boss;
 3.@ConfigurationProperties 和 @Value 取值比较和使用场景


springboot 注入自己 springboot 注入配置_java

 如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value; 如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties;

 4.@PropertySource 使用方式

 @PropertySource:加载指定的配置文件(非application.yml);必须要加 @Component,让其spring进行管理 需要配合@ConfigurationProperties(prefix = "") 指定prefix 绑定到JavaBean中

* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
     *  @ConfigurationProperties(prefix = "person”) 默认从全局配置文件中获取值 即默认只能从 application.properties 或者 application.yml 中获取;
     *
     */
    @Component
    @ConfigurationProperties(prefix = "person")
    @PropertySource(value = {"classpath:person.properties"})
    @Data
    public class PersonConfig {
private String lastName;
        private String firstName;
}

 person.properties 文件

person.lastName=johnny
    persong.firstName=candy
 5.@ImportResource

导入Spring的配置文件 xml 格式的 放在 主运行类上,Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别

 想让Spring的配置文件生效,加载进来;@ImportResource标注在一个配置类上

@ImportResource(locations = {"classpath:bean.xml"})
    @SpringBootApplication
    public class SpringBoot01HelloworldQuickApplication {
    }
bena.xml
<?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.atguigu.springboot.service.HelloService"></bean>
    </beans>

---

2.配置文件处理器

 作用: 配置文件处理器的主要作用是为了 在编写配置文件的时候有提示功能

 方式1 在pom.xml文件里引入以下依赖:
<--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
 方式2 在IDEA创建Spring-Boot项目的时候 勾选


springboot 注入自己 springboot 注入配置_配置文件_02