【问题标题】:Error in Spring bean injection : leads to a NullPointerExceptionSpring bean 注入错误:导致 NullPointerException
【发布时间】:2015-03-09 11:58:17
【问题描述】:

我对 Spring 框架、bean 注入等完全陌生,并且正在从事一个由许多子项目组织的项目。

在包含所有实体、DAO、DS 的 commons 子项目中,我有一个实现 IMyDS 并包含其 EntityManager 和 DAO 的 MyDS 类:

@PersistenceContext(unitName="myPersistenceUnit")
private EntityManager entityManager;

@Autowired
@Qualifier("myDAO")
private IMyDAO mainDao;

然后,我尝试从我的项目的 Web 部分调用这个类,如下所示:

@Autowired
private IMyDS myDS; 

// then I try to call a function of IMyDS, and get an error at this line :

protected ActionForward executeAction(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ReefPresentationException {
    myDS.callFunction(form);
}

但它不起作用,给我一个 NullPointerException。到目前为止,我已经猜到 bean 没有正确注入,所以我尝试在我的 application-context-spring.xml 文件中添加一些信息:

<bean id="myDS" class="com.my.project.service.IMyDS" />

我得到了这个错误:

org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/config/application-context-spring.xml] 中创建名称为“myDS”的 bean 时出错:bean 的实例化失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 bean 类 [com.my.project.service.IMyDS]:指定的类是一个接口

所以我尝试改为声明类:

<bean id="myDS" class="com.my.project.service.internal.MyDS" />

org.springframework.beans.factory.BeanCreationException:在 ServletContext 资源 [/WEB-INF/config/application-context-spring.xml] 中创建名称为“myDS”的 bean 时出错:bean 的实例化失败;嵌套异常是 java.lang.ExceptionInInitializerError

所以我真的不知道现在出了什么问题......

感谢您的帮助

【问题讨论】:

  • 向我们展示您的MyDS 类。Spring 告诉它无法初始化该类。

标签: java spring hibernate spring-mvc


【解决方案1】:

错误说明了一切。您已将接口 IMyDS 定义为 bean,而 Spring 无法实例化该接口。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 2015-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多