1

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);
    }
}
7
  • Add @ConfigurationProperties to your Application class. Commented Jan 30, 2015 at 10:13
  • since I put the properties into application.properties which are picked up by Spring by default, that should not be necessary, should it? Commented Jan 30, 2015 at 10:16
  • If you are putting the properties in application.properties, there are chances that it is overwritten. Please refer to the following documentation which gives details about the order in which property values are resolved. docs.spring.io/spring-boot/docs/current/reference/html/… Commented Jan 30, 2015 at 10:25
  • I think I've read that site before. It seems to me that the application.properties are read and not overwritten, since the logging statements and several DB-specific properties in there are used by Spring. None of the other alternatives in the Property-Reading-Hierarchy on that site are in effect in this app. Commented Jan 30, 2015 at 10:29
  • Could you please let me the location of application.properties file in your project? Commented Jan 30, 2015 at 10:31

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

what do you mean by afterwards, I need to run some logging in main that takes variables from application.properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.