I've created a simple repo here that demonstrates my issue, just in case it helps anyone: https://github.com/kevinm416/spring-boot-config. I've tried various iterations of different Spring annotations I saw on different tutorials, but so far nothing has worked.
I have a very standard spring boot application:
@SpringBootApplication
@EnableConfigurationProperties({BaseConfig.class, FeedsConfig.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
And a configuration class:
@Component
@ConfigurationProperties(prefix = "feeds")
public class FeedsConfig {
public List<String> urls;
public String abc;
}
I've tried both application.properties and application.yml individually and at the same time, with contents like:
feeds:
abc: def
urls:
- url1
- url2
- url3
base: level
When I @Autowire the config class into a controller, the values from the application config are not populated.
@RestController
public class HelloController {
@Autowired
private FeedsConfig feedsConfig;
@Autowired
private Environment environment;
@RequestMapping("/hello")
private String hello() {
return "Hi " + feedsConfig.abc + ", " + feedsConfig.urls;
}
}
I can see the values I want in the Environment under propertySources though! Why aren't my config classes being filled out?
I changed FeedsConfig to this and it's working! Thank you Nicholas!
@ConstructorBinding
@ConfigurationProperties(prefix = "feeds")
public class FeedsConfig {
public FeedsConfig(List<String> urls, String abc) {
this.urls = urls;
this.abc = abc;
}
public List<String> urls;
public String abc;
}
