There are some similar questions, I checked them but could not find a solution to my problem. I am trying very basic spring application but getting error.
here is my appcontext.xml
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.spring.springlegacy"></context:component-scan>
and my component
@Component("springService")
public class SpringServiceImpl implements SpringService{
@Override
public void giveService() {
System.err.println("service given");
}
}
and my wired variable
public class Printer {
@Autowired
private SpringService springService;
public void print(){
springService.giveService();
}
}
and inside main method
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
Printer printer = new Printer();
printer.print();
and the error is
Exception in thread "main" java.lang.NullPointerException
at com.spring.springlegacy.Printer.print(Printer.java:10)
at com.spring.springlegacy.Main.main(Main.java:13)
What is missing here ?
PS: Since I am not using new keyword for spring managed objects, I believe this question is different from others.