简体   繁体   中英

NullpointerException in Spring autowiring even though bean definition is written in dispatcher-servlet.xml

I read about autowiring concept and trying to use it in my project. What I want is only one instance to be created for a particular class and that can be used with all the classes with autowiring.

I defined a bean in dispatcher-servlet.xml

<bean id="modifyService"
  class="com.xyz.service.ModifyPreferencesService"/>

No scope is defined, so it will be singleton. Now I am using it in two classes.

class ABCEvaluationService{
@Autowired
ArrayList listIncident;
//This class is instantiating IncidentFactory with new keyword in a method
**//Methods using listIncident - getting empty list here**
}

class IncidentFactory{
**@Autowired
List listIncident
@Autowired
ModifyPreferencesService modifyService;**

//This class creates Incident Objects and add it to the listIncident.
**//Uses modifyService class objects - but I am getting null here**
}

Issue is I am able to use it in first class but in the second class it gives me NullPointerException. This same thing is happening with other bean. Am I doing this wrong. Is this not the purpose of autowiring. Please explain.. I am learning Spring and don't want to learn wrong concept.

Complete dispatcher-servlet.xml is(I cant really copy paste.. so there can by syntax error. Please ignore if any):

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
    <context:component-scan base-package="mypack" />        
  <mvc:annotation-driven/>
  <mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/ABCReportForm"/>
        <bean class="com.xyz.interceptor.ValueStreamInterceptor" />
    </mvc:interceptor>
  </mvc:interceptors>
  <mvc:default-servlet-handler/>

  <mvc:resources location="/resources/" mapping="/resources/**"/>

  <bean id="validator"
  class="com.xyz.validator.ABCValidator"/>
  <bean id="modifyService"
  class="com.xyz.service.ModifyPreferencesService"/>

  <bean id="abcService"
  class="com.xyz.service.ABCEvaluationService"/>
  <bean id="listIncident"
  class="java.utilArrayList"/>

  //Code for InternalResourceViewer
  </beans>

Thanks

Spring has quirky handling of collection objects. It presumes that you're asking for a number of beans that all implement some common interface. Autowiring a collection requires using its name.

Additionally: - Use generics. - Use Spring Boot instead of manual configuration (with outdated versions). - Use constructor injection instead of field injection to make your code easier to test and maintain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM