I've seen all the questions about spring autowired configurations being null and I can't seem to figure it out. So I condensed it into a small problem in the hopes someone can help.
Spring application Hello.java
package hello;
@SpringBootApplication
public class Hello {
private static final Logger log = LoggerFactory.getLogger(Hello.class);
@Autowired
private AppConfig appConfig;
public void start() {
log.info("Start");
log.info("AppConfig: {}", appConfig);
}
public static void main(String args[]) {
SpringApplication.run(Hello.class, args).close();
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
Hello hello = new Hello();
hello.start();
};
}
}
AppConfig.java
package hello;
@Configuration
public class AppConfig {
@Value("${string.test:bob}")
private String commandLineArg;
public String getCommandLineArg() {
return commandLineArg;
}
}
Running this works! However it logs null
for the appConfig
variable. From my perspective, it should have been automatically instantiated and filled in. What's going on?