How to change a property in a spring environment?

I am using spring Environment Bean in my application to get application configuration properties. I want to change the value of a property in spring Environment from java code without restarting the application server. How can i do this?

@Service
public void MyService {
    @Autowired
    private Environment environment;

    public void doSomething(){
        String value = environment.getProperty("myproperty");
        ...
    }
}
+4
source share
1 answer

The Environmentdefault implementation in the Spring context is actually an instance of StandardEnvironment .

StandardEnvironmentimplements ConfigurableEnvironment , so if you add ConfigurableEnvironmenta super-interface instead, you can make changes at runtime.

@Service
public void MyService {
    @Autowired private ConfigurableEnvironment environment;
+4
source

Source: https://habr.com/ru/post/1625024/


All Articles