Environment与ConfigurableEnvironment
Environment
Spring Environment接口是Spring框架中用于获取应用程序配置信息的核心接口,常用的方法包括:
- getProperty(String key):获取指定属性的值,如果属性不存在则返回null。
示例代码:
@Autowired
private Environment env;String value = env.getProperty("spring.application.name");
- getRequiredProperty(String key):获取指定属性的值,如果属性不存在则抛出IllegalStateException异常。
示例代码:
@Autowired
private Environment env;String value = env.getRequiredProperty("my.required.property");
- containsProperty(String key):判断指定属性是否存在。
示例代码:
@Autowired
private Environment env;boolean exists = env.containsProperty("my.property");
- getPropertySources():获取所有的属性源。
示例代码:
@Autowired
private Environment env;MutablePropertySources sources = ((AbstractEnvironment) env).getPropertySources();
- getPropertySources(String name):根据名称获取指定的属性源。
示例代码:
@Autowired
private Environment env;PropertySource<?> source = env.getPropertySources().get("my.property.source");
- getActiveProfiles():获取当前激活的profile。
示例代码:
@Autowired
private Environment env;String[] profiles = env.getActiveProfiles();
- getDefaultProfiles():获取默认的profile。
示例代码:
@Autowired
private Environment env;String[] profiles = env.getDefaultProfiles();
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):设置默认的环境配置文件。
示例代码
- getPropertySources()方法
该方法返回一个PropertySources对象,该对象包含了应用程序的所有配置信息。可以通过该方法获取应用程序的所有配置信息。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
PropertySources propertySources = environment.getPropertySources();
- setPropertySources(PropertySources propertySources)方法
该方法用于设置应用程序的配置信息。可以通过该方法将自定义的配置信息添加到应用程序中。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(new MapPropertySource("customProperties", Collections.singletonMap("custom.property", "customValue")));
environment.setPropertySources(propertySources);
- setProperty(String key, String value)方法
该方法用于设置应用程序的某个配置属性的值。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setProperty("my.property", "myValue");
- getProperty(String key)方法
该方法用于获取应用程序的某个配置属性的值。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
String value = environment.getProperty("my.property");
- getRequiredProperty(String key)方法
该方法用于获取应用程序的某个配置属性的值,如果该属性不存在,则抛出异常。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
String value = environment.getRequiredProperty("my.property");
- containsProperty(String key)方法
该方法用于判断应用程序是否包含某个配置属性。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
boolean contains = environment.containsProperty("my.property");
- getActiveProfiles()方法
该方法返回当前激活的所有配置文件的名称。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
String[] activeProfiles = environment.getActiveProfiles();
- addActiveProfile(String profile)方法
该方法用于添加一个激活的配置文件。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
environment.addActiveProfile("dev");
- getDefaultProfiles()方法
该方法返回默认的配置文件的名称。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
String[] defaultProfiles = environment.getDefaultProfiles();
- setDefaultProfiles(String… profiles)方法
该方法用于设置默认的配置文件的名称。
示例代码:
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setDefaultProfiles("dev", "test");