EnvironmentAware;获取运行环境中变量;使用:加载配置文件

Interface EnvironmentAware

  • org.springframework.context.EnvironmentAware

    • setEnvironment(Environment environment)
  • 任何实现这个接口的bean将可以获得其运行的环境

setEnvironment(Environment environment)

  • 设置此组件运行的环境。

使用场景

  1. 加载配置文件
    场景一@configration的配置类读取properties中的值
@Configuration
 public class MyConfig implements EnvironmentAware {
    private static Environment env;
    
    @Override
    public void setEnvironment(Environment environment) {
        env=environment;
    }
    
    // ... 
    // todo : 读取env中的值 用来配置其他对象
 }

发布了8 篇原创文章 · 获赞 1 · 访问量 135

猜你喜欢

转载自blog.csdn.net/C_Wangjq/article/details/105377193