本文介绍了春季:将DAO自动装配到实用程序类中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注释驱动的spring mvc项目,该项目以JBoss Web mvc示例为模板. (Spring,Hibernate,JPA 2.0)

I have an annotation driven spring mvc project templated after the JBoss web mvc sample. (Spring, Hibernate, JPA 2.0)

我有一个实用程序包,我想在其中放置可重用的类以实现明显的实用程序功能.具体来说,我有一个LogonUtilities类,我想在其中查询数据库以获取信息.

I have a utility package where I want to put reusable classes for obviously utility functions.Specifically I have a LogonUtilities class where I want to query a database to get information.

我在那里自动连接DAO,但是当我调试DAO时,它始终为null,并因该异常而失败.

I autowire my DAO there but when I debug the DAO is always null and fails with that exception.

我已经阅读并尝试了许多东西-我知道我可能已经遇到了解决方案-但错过了一些东西,继续尝试其他东西.由于注释对我来说是新的,因此我可能没有使用正确的术语. (我已经使用spring& hibernate多年了-但是使用xml)

I have read and tried many things - I know I've probably come across the solution already - but missed something and moved on and tried something else. I probably am not googling the correct terms since annotations are new to me. (I've worked with spring & hibernate for years - but with xml)

我已将其添加到我的applicationContext.xml

I've added this to my applicationContext.xml

<context:component-scan base-package="util"/>

我认为这是我所需要做的.

which I thought was all I needed to do.

这是我当前在我的LogonUtility类中拥有的-但是它不起作用,keywordDao始终为null.我认为,如果我将DAO连接到applicationContext中的LogonUtility bean(旧方法),我可能会使其工作,但是我认为有一种更好的方法来使用批注.

This is what I currently have in my LogonUtility class - but it doesn't work, keywordDao is always null. I think I could probably get it to work if I wired the DAO to a LogonUtility bean in the applicationContext (the old way) but I would think there's a better way to do it with annotations.

@Service
public class LogonUtilities {

    @Autowired private KeywordDao keywordDao;

我的应用程序不是全新的,此时我可能有十个工作的控制器和十多个工作的DAO,其中包括一个已经执行CRUD操作的关键字控制器和DAO,所以我认为我的设置没有这方面的内容是不正确的.我只想重复使用一些从数据库中提取的代码.

My application isn't brand new, I probably have ten working controllers and over a dozen working DAOs at this point, including a Keyword Controller and DAO that already does CRUD operations, so I don't think my setups with that stuff is incorrect.I just have some code I want to reuse that pulls from a database.

谢谢.

*在我的代码中,它实际上称为"TrainingKeyword"而不是"Keyword"这是nullPointer错误,因为DAO为空

*in my code it's actually called "TrainingKeyword" not "Keyword"This is the nullPointer error because the DAO is null

10:52:07,673 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to /Training/Home: java.lang.NullPointerException

    at util.LogonUtilities.trainingOffices(LogonUtilities.java:59) [classes:]
    at filter.LogonFilter.doFilter(LogonFilter.java:100) [classes:]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-3.1.1.RELEASE.jar:3.1.1.RELEASE]

这是发生错误的代码

//Set TrainingOffices
List<TrainingKeyword> kList1 = keywordDao.getAllTrainingKeywordsByName("Level 200 Training Offices");

推荐答案

我通过听取您的想法并在此问题的帮助下解决了我的问题. JPA不是自动装配

I solved my problem by listening to your ideas and with help from this question.JPA is not Autowiring

基本上,我已经尝试了所有有效的方法-但从未一起尝试过.我一直在不断对代码进行编辑,以使其正常运行,并且始终缺少某些内容或存在某些不良情况.对于任何有类似问题的人,请按以下步骤操作

Basically I had already tried everything that worked - but never all together. I was constantly making edits to my code trying to get it to work and always had either something missing or something bad. For anyone with a similar question here are the steps

在我的LogonFilter中添加了

In my LogonFilter I added

 @Autowired private LogonUtilities lu

在我的LogonUtilites类中,添加了

In my LogonUtilites class I added

@Autowired private TrainingKeywordDao keywordDao;

不需要将LogonUtility bean添加到我的applicationContext中-我将其取出,它仍然可以工作.

There was no need to add the LogonUtility bean to my applicationContext - I took it out and it still works.

感谢您提供的所有帮助-您的想法不断让我重新思考自己在做什么.

Thank you to all that helped - your ideas kept making me rethink what I was doing.

这篇关于春季:将DAO自动装配到实用程序类中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 09:04
查看更多