Java Spring自动连接中的NullpointerException,即使bean定义是在dispatcher-servlet.xml中编写的

Java Spring自动连接中的NullpointerException,即使bean定义是在dispatcher-servlet.xml中编写的,java,spring,dependency-injection,javabeans,autowired,Java,Spring,Dependency Injection,Javabeans,Autowired,我阅读了有关自动布线的概念,并试图在我的项目中使用它。我想要的是只为一个特定类创建一个实例,该实例可以与所有具有自动关联的类一起使用 我在dispatcher-servlet.xml中定义了一个bean 问题是我能够在第一个类中使用它,但在第二个类中它给了我NullPointerException。同样的事情也发生在另一个bean上。我做错了吗。这不是自动布线的目的吗。请解释一下。。我正在学习春天,不想学习错误的概念 完整的dispatcher-servlet.xml isI无法真正复制粘贴。。

我阅读了有关自动布线的概念,并试图在我的项目中使用它。我想要的是只为一个特定类创建一个实例,该实例可以与所有具有自动关联的类一起使用

我在dispatcher-servlet.xml中定义了一个bean

问题是我能够在第一个类中使用它,但在第二个类中它给了我NullPointerException。同样的事情也发生在另一个bean上。我做错了吗。这不是自动布线的目的吗。请解释一下。。我正在学习春天,不想学习错误的概念

完整的dispatcher-servlet.xml isI无法真正复制粘贴。。因此,可能会出现语法错误。如果有,请忽略:

<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>

谢谢

Spring对收集对象的处理很奇怪。它假定您需要许多bean,这些bean都实现了一些公共接口。自动关联集合需要使用其名称

此外: -使用泛型。 -使用Spring引导,而不是手动配置过时的版本。
-使用构造函数注入而不是字段注入,使代码更易于测试和维护

这两个类在哪些包中?您是如何配置的?是的,我已经配置了xml文件。它一直在工作,直到我只有一个类可以使用bean/list。在第二种用法中,只有我得到了例外。这两个类都在不同的包中。您能显示完整的xml文件吗。。我将添加一个问题。@King A和B类属于您的工作代码吗?如果是这样的话,他们在哪里配置了SpringBean?但是modifyService bean也出现了这种情况。@King啊,那么看起来您有多个问题。不过,如果你正在学习,我建议你使用Boot,这样你就不必处理所有这些细节。我做这个项目就是为了学习这个。我真的不能放弃。我想了解自动布线。它是如何工作的,只有我在创造一些东西,它才能发生。如果它对bean有效,我将整理列表。请帮忙。@King这是一个人工的难题,在现代系统中已经不存在了。如果你真的想了解它,那么你可能会得到一些解释,因为现在是美国的一天,但是这个多上下文配置问题现在已经过时了,我建议不要花时间在过时的配置方法的细节上,除非你真的不得不处理一个旧系统。ohk。。所以你建议用弹簧靴?
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**
}
<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>