I'm trying to inject a bean in a simple java project but I'm getting a NullPointerException for ProductInformationServiceImpl.
These are the classes I'm using:
AppConfig.java (Spring configuration class)
package com.test.testing.config;
@Configuration
@ComponentScan(basePackages = "com.test")
public class AppConfig {
}
TestService.java interface:
package com.test.testing.service;
public interface TestService {
String doIt();
}
TestService.java implementation:
package com.test.testing.service;
@Service
public class TestServiceImpl implements TestService {
public String doIt() {
return "you did it!";
}
}
Main class:
package com.test.testing;
public class App {
public static void main( String[] args ){
Test w = new Test();
System.out.println(w.getTestService().doIt());
}
}
class Test {
@Autowired
private TestService testServiceImpl;
public TestService getTestService() {
return testServiceImpl;
}
public void setTestService(TestService testServiceImpl) {
this.testServiceImpl = testServiceImpl;
}
}
I did some tests retrieving the bean using:
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
TestService app = (TestService)context.getBean("testServiceImpl");
But that is something I'd like to avoid.
context
. – JensApplicationContext
nothing will happen (just like now). You need a spring container. – M. Deinum