Reputation: 1803
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);
}
}
Upvotes: 1
Views: 947
Reputation: 124441
The problem is your main
you aren't using Spring Boot you are just creating a new instance.
public static void main(String[] args) {
new Application().run();
}
Now in your run
method you are trying to use properties that haven't been initialized as it is just a new instance instead of a Spring configured instance.
You will need to start your application in the main method and afterwards you can configure the ProxyAuthenticator
.
Upvotes: 2