Reputation: 2421
Hello i'm currently going through getting values on .properties
files. i have encountered a problem. I am using Spring Boot. See my sample source below;
Browser.java
@Controller
public class Browser {
@Autowired
private BrowserConfiguration conf;
public Browser(){
System.out.println("I am initializing...");
System.out.println("Reading configuration files..."+conf);
System.out.println("Starting selected browser...");
System.out.println("Waiting for command execution...");
}
}
BrowserConfiguration.java
@Configuration
@PropertySource("file:./properties/test-config.properties")
public class BrowserConfiguration {
@Value( "${browser.target}" )
private String browser;
public String getBrowser() {
return browser;
}
public void setBrowser(String browser) {
this.browser = browser;
}
@Override
public String toString(){
return "Target Browser: "+getBrowser();
}
}
Demo1Application.java
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private Browser browser;
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setBannerMode(Mode.OFF);
app.run(args);
}
@Override
public void run(String... arg0) throws Exception {
// TODO Auto-generated method stub
browser.runTest("test1");
}
}
test-config.properties
browser.target=ie
Console Logs
2017-04-29 23:51:10.537 INFO 6912 --- [ main] com.example.Demo1Application : Starting Demo1Application on Mikram-PC with PID 6912 (C:\Users\Mikram\sts-workspace\demo-1\target\classes started by Mikram in C:\Users\Mikram\sts-workspace\demo-1)
2017-04-29 23:51:10.540 INFO 6912 --- [ main] com.example.Demo1Application : No active profile set, falling back to default profiles: default
2017-04-29 23:51:10.576 INFO 6912 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@67b467e9: startup date [Sat Apr 29 23:51:10 SGT 2017]; root of context hierarchy
I am initializing...
Reading configuration files...null
Starting selected browser...
Waiting for command execution...
2017-04-29 23:51:11.051 INFO 6912 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
I am search now!
2017-04-29 23:51:11.062 INFO 6912 --- [ main] com.example.Demo1Application : Started Demo1Application in 0.722 seconds (JVM running for 1.02)
2017-04-29 23:51:11.063 INFO 6912 --- [ Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@67b467e9: startup date [Sat Apr 29 23:51:10 SGT 2017]; root of context hierarchy
2017-04-29 23:51:11.064 INFO 6912 --- [ Thread-2] o.s.j.e.a.AnnotationMBeanExporter : Unregistering JMX-exposed beans on shutdown
Problem:
I can't seem to connect the BrowserConfiguration inside Browser class. Did i miss something? please do point me to the right direction. im am at a lost right now.
Upvotes: 2
Views: 3098
Reputation: 798
You're using field injection, but access the variable inside the constructor. Because the constructor is called when the object is created Spring didn't have a chance yet to set the variable. In this case you have to use constructor based injection:
@Autowired
public Browser(BrowserConfiguration conf) {
this.conf = conf;
//Use the variable here
}
Upvotes: 3