My spring.xml file is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<mvc:annotation-driven/>
<context:component-scan base-package="com.journaldev" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean name="authorizationAspect" class="com.journaldev.spring.aspect.EmployeeAnnotationAspect" />
<!-- Spring AOP XML Configuration -->
<aop:config proxy-target-class="false">
<aop:aspect id="Id" order="1" ref="authorizationAspect">
<aop:pointcut id="secureMethodPointcut"
expression="@annotation(com.journaldev.spring.aspect.Loggable)"/>
<aop:before pointcut-ref="secureMethodPointcut" method="myAdvice"/>
</aop:aspect>
</aop:config>
</beans>
My employee class is basic file which is:
@Controller
public class Employee {
private String name;
public String getName() {
return name;
}
@Loggable
public void setName(String nm) {
this.name=nm;
}
public void throwException(){
throw new RuntimeException("Dummy Exception");
}
}
And my main class is:
public class SpringMain {
@Autowired
Employee employee;
public void test() {
employee.setName("Pankaj");
System.out.println(employee.getName());
}
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
(new SpringMain()).test();
ctx.close();
}
}
I am getting NullPointerException
in main class while accessing employee. I am very naive to all this. Can anyone of you help me to figure out the issue?