• Environment与ConfigurableEnvironment


    Environment

    Spring Environment接口是Spring框架中用于获取应用程序配置信息的核心接口,常用的方法包括:

    1. getProperty(String key):获取指定属性的值,如果属性不存在则返回null。

    示例代码:

    @Autowired
    private Environment env;
    
    String value = env.getProperty("spring.application.name");
    
    • 1
    • 2
    • 3
    • 4
    1. getRequiredProperty(String key):获取指定属性的值,如果属性不存在则抛出IllegalStateException异常。

    示例代码:

    @Autowired
    private Environment env;
    
    String value = env.getRequiredProperty("my.required.property");
    
    • 1
    • 2
    • 3
    • 4
    1. containsProperty(String key):判断指定属性是否存在。

    示例代码:

    @Autowired
    private Environment env;
    
    boolean exists = env.containsProperty("my.property");
    
    • 1
    • 2
    • 3
    • 4
    1. getPropertySources():获取所有的属性源。

    示例代码:

    @Autowired
    private Environment env;
    
    MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
    
    • 1
    • 2
    • 3
    • 4
    1. getPropertySources(String name):根据名称获取指定的属性源。

    示例代码:

    @Autowired
    private Environment env;
    
    PropertySource<?> source = env.getPropertySources().get("my.property.source");
    
    • 1
    • 2
    • 3
    • 4
    1. getActiveProfiles():获取当前激活的profile。

    示例代码:

    @Autowired
    private Environment env;
    
    String[] profiles = env.getActiveProfiles();
    
    • 1
    • 2
    • 3
    • 4
    1. getDefaultProfiles():获取默认的profile。

    示例代码:

    @Autowired
    private Environment env;
    
    String[] profiles = env.getDefaultProfiles();
    
    • 1
    • 2
    • 3
    • 4

    ConfigurableEnvironment

    ConfigurableEnvironment是Spring框架中的一个接口,它是Environment接口的子接口,用于表示应用程序的环境配置信息。ConfigurableEnvironment提供了一些方法,可以用来获取和设置应用程序的环境配置信息,包括系统属性、环境变量、配置文件等。


    ConfigurableEnvironment接口中的一些常用方法包括:

    1. getPropertySources():获取应用程序的所有属性源,包括系统属性、环境变量、配置文件等。
    
    2. setPropertySources(PropertySources propertySources):设置应用程序的属性源。
    
    3. setProperty(String key, String value):设置指定属性的值。
    
    4. getProperty(String key):获取指定属性的值。
    
    5. getRequiredProperty(String key):获取指定属性的值,如果属性不存在则抛出异常。
    
    6. containsProperty(String key):判断指定属性是否存在。
    
    7. getActiveProfiles():获取当前激活的环境配置文件。
    
    8. addActiveProfile(String profile):添加一个激活的环境配置文件。
    
    9. getDefaultProfiles():获取默认的环境配置文件。
    
    10. setDefaultProfiles(String... profiles):设置默认的环境配置文件。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    示例代码

    1. getPropertySources()方法

    该方法返回一个PropertySources对象,该对象包含了应用程序的所有配置信息。可以通过该方法获取应用程序的所有配置信息。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    PropertySources propertySources = environment.getPropertySources();
    
    • 1
    • 2
    1. setPropertySources(PropertySources propertySources)方法

    该方法用于设置应用程序的配置信息。可以通过该方法将自定义的配置信息添加到应用程序中。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MapPropertySource("customProperties", Collections.singletonMap("custom.property", "customValue")));
    environment.setPropertySources(propertySources);
    
    • 1
    • 2
    • 3
    • 4
    1. setProperty(String key, String value)方法

    该方法用于设置应用程序的某个配置属性的值。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    environment.setProperty("my.property", "myValue");
    
    • 1
    • 2
    1. getProperty(String key)方法

    该方法用于获取应用程序的某个配置属性的值。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    String value = environment.getProperty("my.property");
    
    • 1
    • 2
    1. getRequiredProperty(String key)方法

    该方法用于获取应用程序的某个配置属性的值,如果该属性不存在,则抛出异常。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    String value = environment.getRequiredProperty("my.property");
    
    • 1
    • 2
    1. containsProperty(String key)方法

    该方法用于判断应用程序是否包含某个配置属性。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    boolean contains = environment.containsProperty("my.property");
    
    • 1
    • 2
    1. getActiveProfiles()方法

    该方法返回当前激活的所有配置文件的名称。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    String[] activeProfiles = environment.getActiveProfiles();
    
    • 1
    • 2
    1. addActiveProfile(String profile)方法

    该方法用于添加一个激活的配置文件。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    environment.addActiveProfile("dev");
    
    • 1
    • 2
    1. getDefaultProfiles()方法

    该方法返回默认的配置文件的名称。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    String[] defaultProfiles = environment.getDefaultProfiles();
    
    • 1
    • 2
    1. setDefaultProfiles(String… profiles)方法

    该方法用于设置默认的配置文件的名称。

    示例代码:

    ConfigurableEnvironment environment = new StandardEnvironment();
    environment.setDefaultProfiles("dev", "test");
    
    • 1
    • 2
  • 相关阅读:
    TPM零知识学习二—— 相关链接和页面
    Qt中使用QNetworkAccessManager类发送https请求时状态码返回0
    leetcode498. 对角线遍历
    C++面试知识点总结
    R语言ggplot2可视化:使用patchwork包将两个ggplot2可视化结果横向构成新的结果可视化组合图(使用|符号)
    Vue或React项目配置@路径别名及智能提示方案
    解决ElementUI表格el-table-column添加fixed底部被遮挡的方法汇总
    swig是怎么通过python调用dll的
    Apache Spark 的基本概念
    LeetCode_快速幂_递归_中等_50.Pow(x, n)
  • 原文地址:https://blog.csdn.net/weixin_42594143/article/details/134075145