I'm searching for a way to set system variables (e.g. -Djavax.net.ssl.keyStore) from Spring Cloud Config Server, because I'm facing exactly this behaviour (https://stackoverflow.com/a/30199253/1406669) in a mutual ssl environment.
There are various ways to set it statically (https://stackoverflow.com/a/36895827/1406669 || https://gist.github.com/unamanic/a7eb0c17b78fb03617cc955b06285b1d).
The thing I don't like about it, is setting it statically (once set on startup and never refreshed) and the need to define the keys in a static way. In this way I would have to redeploy the apps when there would be the need to introduce a new system variable. This is a the thing I try to avoid.
Does anybody got an idea?
Asked
Active
Viewed 1,228 times
1
1 Answers
1
The refresh event works via an ApplicationChangeEvent, which your app can listen for as well by implementing an ApplicationListener.
@Component
public class DynamicSystemProperties implements ApplicationListener<EnvironmentChangeEvent>{
private final Environment env;
@Autowired
public DynamicSystemProperties(Environment env) {
this.env = env;
}
@Override
public void onApplicationEvent(EnvironmentChangeEvent environmentChangeEvent) {
if(env.containsProperty("system.javax.net.ssl.keyStore")) {
String keystore = env.getProperty("system.javax.net.ssl.keyStore");
System.out.println("system.javax.net.ssl.keyStore - " + keystore);
System.getProperties().setProperty("javax.net.ssl.keyStore", keystore);
}
}
}

Unamanic
- 126
- 7
-
1Could also work with http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/env/EnvironmentPostProcessor.html – El Gohr Dec 04 '16 at 15:59
-
@ElGohr That is true, however this method also gives you access to the properties that changed in the EnvironmentChangeEvent (which is empty on first run). If you needed to execute something only when a property actually changed this may be cleaner. – Unamanic Dec 04 '16 at 16:09