我在包中有一个bean - data.dao
@Component("questionDao")
@Transactional
public class QuestionDao extends AbstractDao<Question, Long>{
}
(所有方法都在AbstractDao中实现) 我正在为这个bean编写testClass,我使用这样的自动装配:
@Autowired
@Qualifier(value="questionDao")
private QuestionDao questionDao;
这种方式没有任何问题!
现在我需要为QuestionDao bean添加非常具体的方法,所以我稍微改了一下:
@Component("questionDao")
@Transactional
public class QuestionDao extends AbstractDao<Question, Long>{
public void doSomething(){
}
}
一旦我修改我的bean(正文不为空),自动装配由于某种原因不起作用...&#34;没有找到依赖的data.dao.QuestionDao类型的限定bean。 (我使用的是Spring 4.x) 任何想法都可能出错(在场景1中它的功能就像魅力一样,但是当我触摸QuestionDao类时它就无法自动装配)
这是我的AbstractDao课程
public class AbstractDao<T, ID extends Serializable> implements Dao<T, ID> {
@Autowired
protected SessionFactory sessionFactory;
private Class<T> persistentClass;
private Transaction transaction;
@SuppressWarnings("unchecked")
public AbstractDao() {
this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
protected Session getSession() {
return sessionFactory.getCurrentSession();
}
public Class<T> getPersistentClass() {
return persistentClass;
}
@SuppressWarnings("unchecked")
@Override
public T findById(ID id) {
transaction = getSession().beginTransaction();
T object = (T) getSession().load(this.getPersistentClass(), id);
transaction.commit();
return object;
}
@Override
public List<T> findAll() {
transaction = getSession().beginTransaction();
List<T> list = this.findByCriteria();
transaction.commit();
return list;
}
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(Criterion... criterion) {
Criteria crit = this.getSession().createCriteria(this.getPersistentClass());
for (Criterion c : criterion) {
crit.add(c);
}
return (List<T>) crit.list();
}
@Override
public T create(T entity) {
transaction = getSession().beginTransaction();
getSession().saveOrUpdate(entity);
transaction.commit();
return entity;
}
@Override
public void delete(T entity) {
transaction = getSession().beginTransaction();
getSession().delete(entity);
transaction.commit();
}
@Override
public void deleteTable() {
transaction = getSession().beginTransaction();
String deleteTable = new String ("delete " + this.getPersistentClass().getName());
Query query = getSession().createQuery(deleteTable);
query.executeUpdate();
transaction.commit();
}
}
这是sessionFactory
的配置<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL9Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>sk.uniza.fri.cuka.data.dao</value>
<value>sk.uniza.fri.cuka.data.entity</value>
</list>
</property>
</bean>
Full stackTrace:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sk.uniza.fri.cuka.test.tests.AnswerDaoTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private sk.uniza.fri.cuka.data.dao.QuestionDao sk.uniza.fri.cuka.test.tests.AnswerDaoTest.questionDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [sk.uniza.fri.cuka.data.dao.QuestionDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=questionDao)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:212)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:200)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:259)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:261)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:219)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private sk.uniza.fri.cuka.data.dao.QuestionDao sk.uniza.fri.cuka.test.tests.AnswerDaoTest.questionDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [sk.uniza.fri.cuka.data.dao.QuestionDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=questionDao)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [sk.uniza.fri.cuka.data.dao.QuestionDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=questionDao)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 28 more
编辑: 问题解决了:我刚刚删除了&#34; @Transactional&#34;它的工作原理。我还重构了一下我的QuestionDao,所以它实现了Peter Jurkovic所建议的界面。
答案 0 :(得分:2)
Dao界面存在问题。从QuestionDao
中提取界面,它应该有效。
public interface QuestionDao extends Dao<Question, Long>{
public void doSomething();
}
创建QuestionDaoImpl
@Transactional
@Repository("questionDao")
public class QuestionDaoImpl extends AbstractDao<Question, Long> implements QuestionDao{
public void doSomething(){
}
}
答案 1 :(得分:0)
嗯,现在通过编辑,我想知道是否可能是由于您的抽象方法中的@Autowired
无法自动连接。你在堆栈跟踪中有更多信息吗?
我尝试用public class AbstractDao<T, ID extends Serializable> implements Dao<T, ID> {
protected SessionFactory sessionFactory;
@Autowired
public AbstractDao(SessionFactory sessionFactory){
this.sessionFactory= sessionFactory;
}
///
}
setter或构造函数替换它。
类似的东西:
@Component
@Transactional
@Qualifier("questionDao")
public class QuestionDao extends AbstractDao<Question, Long>{
public void doSomething(){
}
}
然后你还需要在QuestionDao中创建一个构造函数
----没有解决问题。
试试这个:
<!-- Main Toolbar -->
<paper-toolbar id="mainToolbar" class="medium-tall">
<!-- Application name -->
<div class="app-name">Website</div>
<span class="space"></span>
<!-- Toolbar icons -->
<paper-icon-button icon="refresh"></paper-icon-button>
<paper-icon-button icon="search"></paper-icon-button>
<!-- Application tabs -->
<paper-tabs class="bottom" attr-for-selected="data-route" selected="[[route]]">
<paper-tab link>
<a data-route="home" href="{{baseUrl}}">
<iron-icon icon="home"></iron-icon>
<span>Home</span>
</a>
</paper-tab>
<paper-tab link>
<a data-route="artist" href="{{baseUrl}}artist">
<iron-icon icon="home"></iron-icon>
<span>Artist</span>
</a>
</paper-tab>
<paper-tab link>
<a data-route="teacher" href="{{baseUrl}}teacher">
<iron-icon icon="home"></iron-icon>
<span>Teacher</span>
</a>
</paper-tab>
<paper-tab link>
<a data-route="research" href="{{baseUrl}}research">
<iron-icon icon="home"></iron-icon>
<span>Research</span>
</a>
</paper-tab>
<paper-tab link>
<a data-route="contact" href="{{baseUrl}}contact">
<iron-icon icon="mail"></iron-icon>
<span>Contact</span>
</a>
</paper-tab>
</paper-tabs>
</paper-toolbar>
<!-- Main Content -->
<div class="content">
<iron-pages attr-for-selected="data-route" selected="{{route}}">
<section data-route="home">
<paper-material elevation="1">
<my-greeting></my-greeting>
<p class="subhead">You now have:</p>
<my-list></my-list>
<p>Looking for more Web App layouts? Check out our <a href="https://github.com/PolymerElements/app-layout-templates">layouts</a> collection. You can also <a href="http://polymerelements.github.io/app-layout-templates/">preview</a> them live.</p>
</paper-material>
</section>
<section data-route="artist">
<paper-material elevation="1">
<h2 class="page-title">Users</h2>
<p>This is the users section</p>
<a href$="{{baseUrl}}users/Addy">Addy</a><br>
<a href$="{{baseUrl}}users/Rob">Rob</a><br>
<a href$="{{baseUrl}}users/Chuck">Chuck</a><br>
<a href$="{{baseUrl}}users/Sam">Sam</a>
</paper-material>
</section>
<section data-route="teacher">
<paper-material elevation="1">
<h2 class="page-title">User: {{params.name}}</h2>
<div>This is {{params.name}}'s section</div>
</paper-material>
</section>
<section data-route="contact">
<paper-material elevation="1">
<h2 class="page-title">Contact</h2>
<p>This is the contact section</p>
</paper-material>
</section>
</iron-pages>
</div>
答案 2 :(得分:0)
此异常是由组件中的包扫描创建的,并添加此xml配置文件<jpa:repositories base-package="sk.uniza.fri.cuka.data.dao"/>