I'm playing around with spring-boot auto wiring to learn more about it and I'm not understanding something. In the code below the autowiring for the startup class works, but the one in the highier level class does not. The result is the "SpringApplication.run" will print out the properties correctly, but "demo.print()" will not. Can someone help me understand why and how I would get it to work?
package demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
@SpringBootApplication
@EnableConfigurationProperties(EmailProperties.class)
public class DemoApplication {
@Autowired
private EmailProperties properties;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
DemoApplication demo = new DemoApplication();
demo.print();
}
private void print() {
System.out.println("Partner support: " + this.properties.getPartnerSupport());
}
DemoApplication(){
}
@Service
static class Startup implements CommandLineRunner {
@Autowired
private EmailProperties properties;
@Override
public void run(String... strings) throws Exception {
System.out.println("-----------------------------------------");
System.out.println("Client support: " + this.properties.getClientSupport());
System.out.println("Partner support: " + this.properties.getPartnerSupport());
System.out.println("No reply to: " + this.properties.getNoReplyTo());
System.out.println("Support reply to: " + this.properties.getSupportReplyTo());
System.out.println("OpS notification: " + this.properties.getOpsNotification());
System.out.println("-----------------------------------------");
}
}
}
I can post the other parts of the demo code if needed. Thanks.