0

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?

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
K.K
  • 311
  • 1
  • 7
  • 17
  • 2
    Your `SpringMain` class is not being created by Spring, so it has no way of injecting your dependency. – MD Sayem Ahmed Dec 08 '14 at 14:04
  • So configure `SpringMain` as just another Spring bean, then use `ctx.getBean()` to retrieve it. – Marko Topolnik Dec 08 '14 at 14:05
  • Oh, how to solve this then? – K.K Dec 08 '14 at 14:06
  • Add below lin in your spring xml to define your class as java bean :- And modify your main method as below :- ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); SpringMain springMain = ctx.getBean("springMain"); springMain.test(); ctx.close(); – Panther Dec 08 '14 at 14:18

0 Answers0