IOC课程整理-19 Spring Environment 抽象

1. 理解 Spring Environment 抽象

2. Spring Environment 接口使用场景

3. Environment 占位符处理

4. 理解条件配置 Spring Profiles

5. Spring 4 重构 @Profile

6. 依赖注入 Environment

7. 依赖查找 Environment

8. 依赖注入 @Value

9. Spring 类型转换在 Environment 中的运用

10. Spring 类型转换在 @Value 中的运用

11. Spring 配置属性源 PropertySource

12. Spring 內建的配置属性源

13. 基于注解扩展 Spring 配置属性源

14. 基于 API 扩展 Spring 配置属性源

15. 课外资料

16. 面试题精选

简单介绍 Spring Environment 接口?

如何控制 PropertySource 的优先级?

在Spring中,PropertySource是用于加载和读取属性配置的机制。当存在多个PropertySource时,可以通过优先级来控制它们的加载顺序和覆盖关系。下面介绍几种常见的控制PropertySource优先级的方式:

1. 使用@Order注解: 在Spring中,可以使用@Order注解为PropertySources进行排序。@Order注解的值越小,优先级越高,即优先级越高的PropertySource先被加载。

```
@Configuration
@PropertySources({
    @PropertySource("classpath:config1.properties"),
    @PropertySource("classpath:config2.properties")
})
public class AppConfig {

    @Bean
    @Order(1) // 指定优先级,数字越小优先级越高
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}
```

2. 使用spring.profiles.active属性:在Spring Boot中,可以通过设置`spring.profiles.active`属性来决定哪个PropertySource会被加载。根据active profile的不同,Spring会加载相应的PropertySource。

```
@Configuration
@PropertySource(value = "classpath:config1.properties", ignoreResourceNotFound = true)
@PropertySource(value = "classpath:config2.properties", ignoreResourceNotFound = true)
public class AppConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setIgnoreUnresolvablePlaceholders(true);
        return configurer;
    }
}
```

通过设置`spring.profiles.active`属性为相应的配置文件名,控制只加载对应的PropertySource。

3. 使用spring.config.name和spring.config.location属性:在Spring Boot中,可以通过设置`spring.config.name`和`spring.config.location`属性来指定加载的配置文件名和位置。根据配置文件的顺序,后面的配置文件会覆盖前面的。

```
java -jar myproject.jar --spring.config.name=config1,config2
java -jar myproject.jar --spring.config.location=classpath:/default/,classpath:/custom/
```

以上是几种常见的控制PropertySource优先级的方式。根据具体的需求和场景,选择合适的方式来管理和控制属性配置的加载顺序。

当存在多个PropertySource时,它们的加载顺序会影响属性的优先级和覆盖关系。除了前面提到的方式,还可以使用以下方法控制PropertySource的优先级:

4. 使用@PropertySource注解的顺序:在使用@PropertySource注解加载属性配置文件时,可以通过调整注解的顺序来控制加载顺序和优先级。

```java
@Configuration
@PropertySource("classpath:config1.properties") // 优先级低
@PropertySource("classpath:config2.properties") // 优先级高
public class AppConfig {
    // ...
}
```

上述示例中,config2.properties的优先级更高,它的配置会覆盖config1.properties中相同的属性值。

5. 使用Environment的getPropertySources方法:通过Environment对象可以获取到加载的PropertySource,并且可以通过添加和移除PropertySource来调整它们的优先级。

```java
@Autowired
private Environment environment;

@PostConstruct
public void modifyPropertySourcePriority() {
    MutablePropertySources propertySources = ((AbstractEnvironment) environment).getPropertySources();
    // 调整PropertySource的优先级
    propertySources.addFirst(new ResourcePropertySource("classpath:config2.properties"));
}
```

上述示例中,通过调用addFirst方法将config2.properties的PropertySource添加到第一个位置,从而提高了它的优先级。

通过以上方法,可以灵活地控制和调整PropertySource的加载顺序,从而控制属性的优先级和覆盖关系。根据具体的需求和场景,选择合适的方式来管理和调整PropertySource的优先级。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值