在package-data.dao中有一个bean
@Component("questionDao")
@Transactional
public class QuestionDao extends AbstractDao<Question, Long>{
}
@Autowired
@Qualifier(value="questionDao")
private QuestionDao questionDao;
@Component("questionDao")
@Transactional
public class QuestionDao extends AbstractDao<Question, Long>{
public void doSomething(){
}
}
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();
}
}
<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>
完整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
编辑:问题解决了:我只是删除了“@transactional”,它可以工作了。我还重构了一点我的问题,这样它就实现了Peter Jurkovic建议的接口。
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(){
}
}
我在调用get请求(modes-calcul)时遇到这个错误,我不明白为什么...我的依赖注入是正确的吗? ModeCalculController: 谢谢你的帮助..
这是我第一次尝试使用Spring MVC使用Spring Data JPA,但我无法解决这个错误。 UserRepository.java MainController.java AppConfig.java
我有一个可以从应用程序中启用/禁用的组件。我的spring boot应用程序中的属性。。 在我的应用程序中。我拥有的财产 在组件中看起来像这样 最后,在我使用wikiclient的另一个类中,我在我的构造函数中像这样自动安装了它。 但我还是有例外 如果启用该属性,它的工作方式与启用该组件类似。
你知道为什么我会犯这个错误吗?我是Spring的新手,冬眠的,junit的。总之,我认为我应该这样做。或者我该怎么做这个测试类? 我在某处读到导致问题的原因是extends AbstractTransactionalJUnit4SpringContextTest,如果没有它,我就不会再出现这个错误了。
我的servlet-contents.xml
我按照教程创建批处理作业https://spring.io/guides/gs/batch-processing/,如果我从MainClass运行应用程序,则程序正在运行。 但是,当我使用运行相同的代码时,我会得到关于未找到DataSource的错误。我的pom文件是 https://maven.apache.org/xsd/maven-4.0.0.xsd“>4.0.0 org.springfra