Environment in Spring Boot

Environment in Spring Boot

In Spring Boot, Environment is an important component used to manage the configuration of the application. It is an interface that provides methods to access application configuration properties. In this article, we'll dig into the Environment in Spring Boot, including how it works and how to use it.

insert image description here

The principle of Environment

In Spring Boot, Environment is an interface for managing configuration properties. It provides many methods to get configuration properties, such as getProperty(), getPropertySources() and getActiveProfiles(), etc. When the application starts, Spring Boot automatically creates an Environment instance and uses it to load the application's configuration.

Environment consists of multiple PropertySources. Each PropertySource contains a set of property key-value pairs, and the property values ​​can be obtained through the getProperty() method. When an application needs to get a configuration property, Spring Boot iterates through the PropertySource list and looks for a matching property.

In Spring Boot, there are many classes that implement the PropertySource interface, such as MapPropertySource, SystemEnvironmentPropertySource, and CommandLinePropertySource. These classes can be used to load different configuration sources, such as command-line parameters, system environment variables, and configuration files.

How to use Environment

In Spring Boot, we can use Environment to get the configuration properties of the application. Here are some commonly used methods:

@Configuration
public class MyConfig {
    
    

    @Autowired
    private Environment env;

    @Bean
    public MyBean myBean() {
    
    
        String property = env.getProperty("my.property");
        return new MyBean(property);
    }
}

In the above code, we use the @Autowired annotation to inject the Environment into the MyConfig class. Then, in the myBean() method, we use the getProperty() method to get the value of the my.property property and pass it to the MyBean's constructor.

In addition to the getProperty() method, Environment provides many other useful methods. For example, we can use the getActiveProfiles() method to get the currently active configuration files, use the getPropertySources() method to get all PropertySources, and use the containsProperty() method to check whether a property exists, etc.

Advanced Usage of Environment

In addition to basic usage, Environment also provides some advanced usage, such as adding and removing PropertySource.

Add PropertySource

In some cases, we may need to add a custom PropertySource. For example, we can load configuration properties from the database and add them to the Environment. Here is an example:

@Configuration
public class MyConfig {
    
    

    @Autowired
    private Environment env;

    @Bean
    public PropertySource<?> myPropertySource() {
    
    
        Map<String, Object> properties = new HashMap<>();
        properties.put("my.custom.property", "foo");
        return new MapPropertySource("myPropertySource", properties);
    }

    @Bean
    public MyBean myBean() {
    
    
        String property = env.getProperty("my.custom.property");
        return new MyBean(property);
    }
}

In the code above, we first define a PropertySource called myPropertySource that contains a property called my.custom.property. Then, in the myBean() method, we use the getProperty() method to get the value of the my.custom.property property and pass it to the MyBean's constructor.

deletePropertySource

If we need to delete a PropertySource, we can use the remove() method provided by the MutablePropertySources interface. For example, the following code demonstrates how to delete a PropertySource named myPropertySource:

@Configuration
public class MyConfig {
    
    

    @Autowired
    private ConfigurableEnvironment env;

    @Bean
    public PropertySource<?> myPropertySource() {
    
    
        Map<String, Object> properties = new HashMap<>();
        properties.put("my.custom.property", "foo");
        return new MapPropertySource("myPropertySource", properties);
    }

    @PostConstruct
    public void removeMyPropertySource() {
    
    
        MutablePropertySources propertySources = env.getPropertySources();
        propertySources.remove("myPropertySource");
    }
}

In the code above, we first define a PropertySource named myPropertySource. Then, in the removeMyPropertySource() method, we use the ConfigurableEnvironment interface to obtain the MutablePropertySources and remove myPropertySource using the remove() method.

Summarize

In this article, we took an in-depth look at the Environment in Spring Boot, including how it works and how to use it. Environment is an interface for managing configuration properties, which is automatically created when the application starts and consists of multiple PropertySources. We can use Environment to get the configuration properties of the application, and use the methods it provides to get the active configuration file, all PropertySources, check the existence of properties, etc. In addition to basic usage, Environment also provides some advanced usage, such as adding and removing custom PropertySource.

By understanding the principle and usage of Environment, we can better manage the configuration of the application and improve the maintainability and scalability of the code. Therefore, when writing Spring Boot applications, we should make full use of Environment and use its advanced usage according to actual needs.

Finally, the sample code is attached for readers' reference and study.

@Configuration
public class MyConfig {
    
    

    @Autowired
    private Environment env;

    @Bean
    public MyBean myBean() {
    
    
        String property = env.getProperty("my.property");
        return new MyBean(property);
    }

    @Bean
    public PropertySource<?> myPropertySource() {
    
    
        Map<String, Object> properties = new HashMap<>();
        properties.put("my.custom.property", "foo");
        return new MapPropertySource("myPropertySource", properties);
    }

    @PostConstruct
    public void removeMyPropertySource() {
    
    
        MutablePropertySources propertySources = ((ConfigurableEnvironment) env).getPropertySources();
        propertySources.remove("myPropertySource");
    }
}

Guess you like

Origin blog.csdn.net/it_xushixiong/article/details/131455051