Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change a property in spring environment?

Tags:

java

spring

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

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

    public void doSomething(){
        String value = environment.getProperty("myproperty");
        ...
    }
}
like image 346
user1552545 Avatar asked Jan 19 '16 20:01

user1552545


1 Answers

The Environment implementation that you will get by default in the Spring context is actually an instance of StandardEnvironment.

StandardEnvironment implements ConfigurableEnvironment, so if you inject a ConfigurableEnvironment instead of the super interface you will be able to make alterations at runtime.

@Service
public void MyService {
    @Autowired private ConfigurableEnvironment environment;
like image 118
Kong Avatar answered Oct 15 '22 01:10

Kong