我正在編寫一個所有Hibernate DAO類將從中繼承的泛型Hibernate DAO類。我想用spring來爲類注入依賴關係,但我得到一個異常。Spring通用Hibernate DAO
@Repository
public class HibernateDaoImpl <T> implements Dao<T>{
@Autowired
private SessionFactory sessionFactory;
@Autowired
private Class<T> clazz;
//setter & getter
public HibernateDaoImpl (Class<T> clazz)
{
this.clazz = clazz;
}
public Session getCurrentSession()
{
return sessionFactory.getCurrentSession();
}
public void save(T t)
{
getCurrentSession().save(t);
}
public void update(T t)
{
getCurrentSession().update(t);
}
@SuppressWarnings("unchecked")
public T findById(Object object)
{
return (T) getCurrentSession().get(clazz, (Serializable)object);
}
@SuppressWarnings("unchecked")
public List<T> findAll(int page, int maxResult)
{
return getCurrentSession().createQuery(String.format("FROM %s", clazz.getSimpleName())).list();
}
我的派生類
public class SubscriberHibernateDao extends HibernateDaoImpl<Subscriber> implements SubscriberDao {
public SubscriberHibernateDao()
{
super(Subscriber.class);
}
}
我的彈簧XML
<bean id="subscriber" class="com.example.model.Subscriber" autowire="byName">
<property name="email" value="mdennis10"></property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/kyro"/>
<property name="username" value="root"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.kyrogaming.model"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="subcriberDao" class="com.example.dao.SubscriberHibernateDao">
<property name="clazz" value="com.example.model.Subscriber"></property>
</bean>
當我運行此我得到以下堆棧跟蹤:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.lang.Class com.example.dao.HibernateDaoImpl.clazz; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [java.lang.Class] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.kyrogaming.test.BeanTest.test(BeanTest.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
造成的:組織.springframework.beans.factory.BeanCreationEx ception:無法自動裝配字段:private java.lang.Class com.example.dao.HibernateDaoImpl.clazz;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合依賴關係的[java.lang.Class]類型的合格bean:期望至少1個符合此依賴關係自動裝配候選資格的bean。依賴註釋:在@ org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514) 處, springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) ...... 36多個 造成的: org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合依賴關係的[java.lang.Class]類型的合格bean:期望至少1個符合此依賴關係自動裝配候選資格的bean。依賴註釋:{@ org.springframework.beans.factory.annotation.Autowired(required = true)} org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986) at org.springframework。 在org.springframework.beans.factory.annotation上的beans.factory.support.DefaultListableBeanFactory.doResolveDependency AutowiredAnnotationBeanPostProcessor $ AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486) ... 38更多
我該如何解決這個問題。
我爲你提供了以下的答案。讓我知道如果這是有道理的,如果是的話,你能接受它作爲答案嗎? – Khush