SpringBoot 属性配置解析

属性配置介绍

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

  1. Devtools全局配置
  2. 测试环境@TestPropertySource注解
  3. 测试环境properties属性
  4. 命令行参数
  5. SPRING_APPLICATION_JSON属性
  6. ServletConfig初始化参数
  7. ServletContext初始化参数
  8. JNDI属性
  9. JAVA系统属性
  10. 操作系统环境变量
  11. RandomValuePropertySource随机值属性
  12. jar包外的application-{profile}.properties
  13. jar包内的application-{profile}.properties
  14. jar包外的application.properties
  15. jar包内的application.properties
  16. @PropertySource绑定配置
  17. 默认属性

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.properties
  • application-default.properties

激活profile

  • spring.profiles.active=xx
  • spring.profiles.activedefault互斥(即配置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处理逻辑?
  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过多种方式配置多个不同的环境,比如properties文件、yaml文件、系统环境变量等。在多环境配置中,一个常见的需求是根据不同的环境来加载不同的配置文件,Spring Boot提供了一种很方便的方式来实现这个需求,即使用Spring Boot的Profile功能。 通过使用Profile,我们可以在不同的环境中加载不同的配置文件。具体实现方式如下: 1. 在application.properties文件中配置profiles.active属性,如:`spring.profiles.active=dev`。 2. 在resources目录下创建不同的配置文件,如application-dev.properties、application-prod.properties等。 3. 在配置文件中定义对应的配置项,如数据库连接、缓存配置等。 在程序运行时,Spring Boot会根据配置的profiles.active属性来决定加载哪个配置文件中的配置项。比如,当profiles.active=dev时,Spring Boot会加载application-dev.properties文件中的配置项。 而在实际项目中,我们可能需要根据不同的环境来动态解析配置文件,并将其配置Spring Boot的Environment中,这时候可以使用Spring Boot提供的PropertySource来实现。具体实现方式如下: 1. 创建一个PropertiesLoaderUtils类,用于加载properties文件。 ```java public class PropertiesLoaderUtils { private static final String DEFAULT_PROPERTIES = "application.properties"; public static Properties loadProperties(String... locations) throws IOException { Properties properties = new Properties(); for (String location : locations) { try (InputStream in = getResourceAsStream(location)) { if (in == null) { continue; } properties.load(in); } } return properties; } private static InputStream getResourceAsStream(String location) { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(location); if (in == null) { in = PropertiesLoaderUtils.class.getClassLoader().getResourceAsStream(location); } if (in == null) { in = ClassLoader.getSystemClassLoader().getResourceAsStream(location); } return in; } public static Properties loadDefaultProperties() throws IOException { return loadProperties(DEFAULT_PROPERTIES); } } ``` 2. 在程序启动时,读取配置文件并解析成PropertySource,然后将其添加到Spring Boot的Environment中。 ```java @SpringBootApplication public class Application { public static void main(String[] args) throws IOException { SpringApplication app = new SpringApplication(Application.class); Environment env = app.run(args).getEnvironment(); Properties properties = loadProperties(env.getActiveProfiles()); PropertySource<?> propertySource = new PropertiesPropertySource("customProperties", properties); ((ConfigurableEnvironment) env).getPropertySources().addFirst(propertySource); } private static Properties loadProperties(String[] activeProfiles) throws IOException { String[] locations = new String[activeProfiles.length + 1]; System.arraycopy(activeProfiles, 0, locations, 0, activeProfiles.length); locations[activeProfiles.length] = "application.properties"; // 默认配置文件 return PropertiesLoaderUtils.loadProperties(locations); } } ``` 在上面的代码中,我们首先获取了Spring Boot的Environment对象,并根据当前的Active Profiles加载对应的配置文件。然后,将配置文件解析成PropertySource,并将其添加到Environment中,这样就可以在程序中通过Environment获取到配置项了。 需要注意的是,如果多个配置文件中存在同名的配置项,后加载的配置项会覆盖之前的配置项。因此,在编写配置文件时,应该避免定义同名的配置项,以免造成不必要的麻烦。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值