0

So I have followed almost all tutorials/examples and stack overflow questions but I still can't seem to get my application.properties file to be able to fill in values in an ExternalConfig class I have created.

My application properties class file is located in src/main/resources and looks like this

app.developerName = "f21ad267-e241-4ed0-8943-721fa90bcf3a"
spring.boot.config.developerName = "f21ad267-e241-4ed0-8943-721fa90bcf3a"
server.port=9000
Access-Control-Allow-Origin: *

My purpose built External Config class looks like this

@ConfigurationProperties(prefix="spring.boot.config")
@Component
public class ExternalConfig {

private String developerName;

public String getDeveloperName() {
    return developerName;
}

public void setDeveloperName(String developerName) {
    this.developerName = developerName;
}   
}

Finally my Springboot main class looks like this

@EnableConfigurationProperties(ExternalConfig.class)
@SpringBootApplication
public class SpringBootMain implements CommandLineRunner {

@Autowired
ExternalConfig externalConfig;

@Bean
ResourceConfig resourceConfig() {
    return new ResourceConfig().registerClasses(ExternalConfig.class, Version1Api.class, Paypal.class);
}

public static void main(String[] args) {
    SpringApplication.run(SpringBootMain.class);
}

@Override
public void run(String... args) throws Exception {
    // TODO Auto-generated method stub
    System.out.println(externalConfig.getDeveloperName());

}  
}

Every time I run the code or debug it, the ExternalClass and it's variables are always null.

I am running out of ideas and methods to try do this an alternative way. Am I missing something in my pom.xml?

3
  • When I try your provided example code, it works. What is your Spring Boot version? And is your application.properties file called application.properties exactly? Commented Jan 13, 2020 at 19:53
  • Check out the following links: baeldung.com/spring-value-annotation a guide that describes how you should define your "@Configuration" class and use "@Value". The corresponding github link to the code: github.com/eugenp/tutorials/tree/master/spring-boot-properties Commented Jan 13, 2020 at 19:58
  • You are aware that you have multiple instances of the ExternalConfig? You either do @Component with @ConfigurationProperties or @EnableConfigurationProperties with @ConfigurationProperties. You also have a fresh instance in JAX-RS which isn't coupled to the instance in Spring. Commented Jan 14, 2020 at 6:56

2 Answers 2

1

These 2 changes may help:

  1. Remove the blank spaces next to '=' symbol in the application.properties file
  2. Remove the word 'prefix=' from the @ConfigurationProperties annotation.

More information:

A very good article with working code on extracting the application properties.

https://mkyong.com/spring-boot/spring-boot-configurationproperties-example/

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

1 Comment

“Remove the blank spaces next to '=' symbol in the application.properties file” ← There is literally no chance of that making a difference. The properties file format explicitly allows whitespace around the = or : separator: “Any white space after the key is skipped; if the first non-whitespace character after the key is '=' or ':', then it is ignored and any white space characters after it are also skipped.”
0

I think you should define the bean differently

@Configuration
@ConfigurationProperties(prefix="spring.boot.config")
public class ExternalConfig {

1 Comment

@ConfigurationProperties works with an @Component as well.

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.