Spring Framework Autowired Null Pointer Exception

前端 未结 3 1845
既然无缘
既然无缘 2021-01-24 23:42

I\'m learning Spring and I\'m having some problems trying to set up a relatively basic Spring project. I\'m creating an application to simply read from a database, but I\'m havi

相关标签:
3条回答
  • 2021-01-25 00:01

    You need to create the application context no matter if you are using xml or annotation based configuration. If your application is a web application, see this post Loading context in Spring using web.xml to load the application context

    Once you have the application you can get the bean using context.getBean() method

    Also, spring container does not manage the objects you create using new operator. In your example you need to autowire the GetCustomerEvent bean

    @Autowired GetCustomerEvent getCustomerEvent;
    //and call
    getCustomerEvent.getCustomers();
    
    0 讨论(0)
  • 2021-01-25 00:14

    Problem is with below line

    GetCustomerEvent ev = new GetCustomerEvent();
    

    You manually created instance using "new". Spring does not have idea about this object. See Why is my Spring @Autowired field null? for details.

    0 讨论(0)
  • 2021-01-25 00:16

    You are not initializing the Spring Container.

    You need to create your context in order for it to work.

    0 讨论(0)
提交回复
热议问题