属性配置介绍
spring官方提供的17中属性配置的方式

- Devtools全局配置
 - 测试环境@TestPropertySource注解
 - 测试环境properties属性
 - 命令行参数
 - SPRING_APPLICATION_JSON属性
 - ServletConfig初始化参数
 - ServletContext初始化参数
 - JNDI属性
 - JAVA系统属性
 - 操作系统环境变量
 - RandomValuePropertySource随机值属性
 - jar包外的application-{profile}.properties
 - jar包内的application-{profile}.properties
 - jar包外的application.properties
 - jar包内的application.properties
 - @PropertySource绑定配置
 - 默认属性
 
Spring Aware介绍
Aware介绍
- Spring框架优点:Bean感知不到容器的存在
 - 使用场景:需要使用Spring容器的功能资源
 - 引入缺点:Bean和容器强耦合
 
常用Aware

Aware调用


自定义实现Aware

import org.springframework.beans.factory.Aware;
public interface MyAware extends Aware {
    void setFlag(Flag flag);
} 
@Component
public class MyAwareProcessor implements BeanPostProcessor {
    private final ConfigurableApplicationContext configurableApplicationContext;
    public MyAwareProcessor(ConfigurableApplicationContext configurableApplicationContext) {
        this.configurableApplicationContext = configurableApplicationContext;
    }
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof Aware) {
            if (bean instanceof MyAware) {
                ((MyAware) bean).setFlag((Flag) configurableApplicationContext.getBean("flag"));
            }
        }
        return bean;
    }
} 
Environment解析

getOrCreateEnvironment
- 添加servletConfigInitParams属性集
 - 添加servletContextInitParams属性集
 - 添力Jndi属性集
 - 添加systemProperties属性集
 - 添加systemEnvironment属性集
 
configureEnvironment
- 添加defaultProperties厲性集
 - 添加commandLineArgs属性集
 
listener.environmentPrepared
- 添加spring_application_json属性集
 - 添加vcap属性集
 - 添加random属性集
 - 添加application-profile.(properties | yml)属性集
 
ConfigurationPropertySources.attach
- 添加configurationProperties属性集
 
ConfigurationClassParser
- 添加@PropertySources属性集
 
Spring profile介绍
将不同的参数配置绑定在不同的环境
默认使用
application.propertiesapplication-default.properties
激活profile
spring.profiles.active=xxspring.profiles.active与default互斥(即配置active后application-default.properties会失效)
Spring profile原理解析
源码流程解析

处理入口

initializeProfiles逻辑

profiles处理

profile处理逻辑

Load逻辑

addLoadedPropertySources

面试题
- SpringBoot属性配置方式有哪些?
 - 介绍下Spring Aware的作用及常见的有哪些?
 - Spring Aware注入原理?
 - 动手写一个Spring Aware?
 - Environment对象是如何加载属性集的?
 - 部分属性集如spring_application_json何时被加载的?
 - 介绍下Spring Profile?常用配置方式?
 - Spring Profile配置方式有哪些注意事项,为什么?
 - Spring Profile处理逻辑?
 



















