• 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
  • 相关阅读:
    【Python】不是内部或外部命令,cmd指令报错,path环境配置
    第 42 章 RTC—实时时钟
    看三年的CRUD程序员如何解决数据库死锁的
    MindSpore:强化学习基础-蒙特卡洛(Monte Carlo)
    MySQL内部函数介绍
    mongo启动客户端失效
    Golang 实现 Redis(11): RDB 文件格式
    《C++ Primer》第5章 语句
    创建个人中心页面(下)
    8种超简单的Golang生成随机字符串方式
  • 原文地址:https://blog.csdn.net/weixin_42594143/article/details/134075145