首页 文章

Spring bean注入错误:导致NullPointerException

提问于
浏览
0

我是Spring框架,bean注入等的新手,并且在许多子项目中组织了一个关于它的项目 .

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

@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

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

谢谢你的帮助

1 回答

  • 1

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

相关问题