0

I have a running Spring Boot project. I want to read some environment specific properties from an external properties file.

I mentioned config files names and locations while starting the server as follows:

java -jar AllergiesConditions.jar --spring.config.name=application,metadata --spring.config.location=classpath:/,/APPS/SpringBoot/

The property files loads successfully(because i tried to log one of the external key values inside datasource bean and It printed successfully) But when i try to access a value using @Value annotation - It returns null.

My test Class is as follows:

@Component
public class testclass {
    private Logger logger = LogManager.getLogger(testcla.class);

    @Value("${sso.server}")
    public String sso;

    public void test(){
        logger.info("sso url is: "+sso); //This sso is logged as null
        otherStuff();
    }
}

This test() function is called when a particular API is hit after server is running.

The external config file - metadata.properties contains this variable:

sso.server=1234test

Edit: As suggested in this apparently duplicate question I also tried adding @PropertySource(name = "general-properties", value = { "classpath:path to your app.properties"}) in main Application configuration class and It loaded the files, but still I get null value itself.

Can someone please help in what's going wrong here?? Does the testclass need some specific annotation OR it needs to be a bean or something??

Thanks in Advance :)

Naveen Kumar
  • 384
  • 2
  • 11
  • 31
  • https://stackoverflow.com/questions/44179065/spring-boot-set-value-from-an-external-properties-file – Mahendra Kapadne Jan 15 '18 at 12:51
  • Possible duplicate of [Spring Boot - set value from an external properties file](https://stackoverflow.com/questions/44179065/spring-boot-set-value-from-an-external-properties-file) – Mahendra Kapadne Jan 15 '18 at 12:52
  • I tried the solution suggested in above post, Still I couldn't succeed!! – Naveen Kumar Jan 15 '18 at 14:15
  • 1
    It cannot be `null`...If the `${sso.server}` cannot be resolved your application will break at startup. Which means you aren't using a managed instance but creating instances of `testclass` yourself. – M. Deinum Jan 15 '18 at 14:48
  • 1
    @M.Deinum Yes, I think You gave some great clue. Inside one of my controllers I am just calling `testclass obj = new testclass(); obj.test();`. If that is wrong way, please tell me how can i call test function of my testclass inside my controller. As you may expect, If I use `@Value` annotation inside controller itself and log it in controller itself, It works!! – Naveen Kumar Jan 16 '18 at 05:43
  • Instead of creating a new instance `@Autowire` the instance in the classes you need it. – M. Deinum Jan 16 '18 at 06:43
  • @M.Deinum Thanks for your inputs. It worked when i autowired `testclass` in my controller. Can you please elaborate a little on- why it worked on using a managed instance but didn't work by creating instances of `testclass` on my own? – Naveen Kumar Jan 17 '18 at 13:00

1 Answers1

1

Thanks to M.Deinum for great input and saving my time

Just posting his comment as answer

Factually ${sso.server} cannot be null. If ${sso.server} couldn't be resolved, my application will break at startup itself.

So the obvious problem was that I was creating a new instance of testclass in my controller using

testclass obj = new testclass(); obj.test();

Rather I should be using spring managed instance by autowiring testclass in my controller.

Naveen Kumar
  • 384
  • 2
  • 11
  • 31