I'm using Spring Boot to start an embedded Tomcat with my application in it. This Tomcat-Server is behind a firewall and for all connections to the outside world, I need to use a proxy with authentication.
For this reason I configured an Authenticator right before I start up the Spring Application in my main method.
Everything works fine with fixed Username/Password, but I would like to read these from a properties file, therefore I tried to inject these via
@Value("${proxy.user}")
private String proxyUser;
@Value("${proxy.password}")
private String proxyPassword;
But these always evaluate to null. I guess that's because the ApplicationContext and therefore the Spring Application does not exist, when the class with the main method is instantiated...
Is there a best practice for these kind of situation? Do I just read the properties in the "old-fashioned" way?
Class looks something like this:
@Configuration
@EnableAutoConfiguration
public class Application {
@Value("${proxy.user}")
private String proxyUser;
@Value("${proxy.password}")
private String proxyPassword;
public static void main(String[] args) {
new Application().run();
}
private void run() {
ProxyAuthenticator proxyAuth = new ProxyAuthenticator(proxyUser, proxyPassword);
Authenticator.setDefault(proxyAuth);
// Spring Context starten
ConfigurableApplicationContext context = SpringApplication.run(Application.class);
}
}
@ConfigurationPropertiesto yourApplicationclass.