gpt4 book ai didi

java - Spring bean注入(inject)错误: leads to a NullPointerException

转载 作者:行者123 更新时间:2023-12-01 11:48:53 25 4
gpt4 key购买 nike

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

在包含所有实体、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 未正确注入(inject),因此我尝试在我的 application-context-spring.xml 文件中添加一些信息:

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

我收到此错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDS' defined in ServletContext resource [/WEB-INF/config/application-context-spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.my.project.service.IMyDS]: Specified class is an interface

所以我尝试声明该类:

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

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myDS' defined in ServletContext resource [/WEB-INF/config/application-context-spring.xml]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

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

感谢您的帮助

最佳答案

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

关于java - Spring bean注入(inject)错误: leads to a NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28941467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com