I am trying to use spring 4 too load properties and use them in the classes, but it I can't get it to work. This is not an MVC appication and the reported duplicatet does not give me any clue on my issue. Here is what I got in my test project:
The main class:
public class Main {
public static void main(String...args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
Tester t = new Tester();
t.testBean();
}
}
ApplicationConfig Class:
@Configuration
@ComponentScan(basePackages={"nl.foo.propertiestest"})
@PropertySource("application.properties")
public class ApplicationConfig {
//Spring framework will know what to do with this class
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
The Tester Ckass:
public class Tester {
@Autowired
Environment env;
@Bean
public void testBean() {
System.out.println("testBean start");
String a = env.getProperty("test");
System.out.println("property = " + a);
System.out.println("testBean e");
}
}
The application.properties:
test=hello tester
The application gives a null pointer exception on tring a = env.getProperty("test");
but it seems to load the properties, as it gives an error when the properties file is not in the class path.
How do I get the property retrieved?