使用hibernate.cfg.xml的Spring Autowired DAO

时间:2013-08-06 18:04:36

标签: spring hibernate junit4 dao autowired

我有一个Spring Maven DAO项目,它被打包成一个罐子。它最初是一个Spring DAO Ivy项目,但我把它转换成了maven。这是一个使用Spring 3.2.0和Hibernate 3.5的遗留项目,现在我创建了一个新项目并更新到Spring 3.2.3.RELEASE和Hibernate 4.2.3.Final。我确实试着确保我的所有罐子都更新了。

目前,应用程序使用maven构建,并且所有Java代码似乎都在编译。

这个DAO不使用带注释的实体,而是使用传统的hibernate.cfg.xml文件和许多hbm.xml文件...我希望我可以使用它。我不喜欢将很多实体转换为带注释的实体,然后转换DAO代码的想法。

无论如何,通过以下配置,我想测试加载单个DAO以测试配置。但是,我收到如下所列的错误消息。我花了一天时间在谷歌上研究这个,并在这里查看是否有人有确切的错误。

如果我从头开始构建一个新项目,我会有一些注释实体,但我现在没有这个,所以我必须调整配置才能使这个DAO项目正常工作。

这是错误:

2013-08-06 13:36:42,834 INFO [org.springframework.context.support.GenericApplicationContext] - <Refreshing org.springframework.context.support.GenericApplicationContext@165973ea: startup date [Tue Aug 06 13:36:42 EDT 2013]; root of context hierarchy>
2013-08-06 13:36:42,881 INFO  [org.springframework.beans.factory.support.DefaultListableBeanFactory] -  <Pre-instantiating singletons in  org.springframework.beans.factory.support.DefaultListableBeanFactory@6c4fc156: defining beans     [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy>
2013-08-06 13:36:42,912 ERROR [org.springframework.test.context.TestContextManager]     
<Caught exception while allowing TestExecutionListener     [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@667cbde6] to prepare test instance [com.tom.project.dao.AaaaBaseDAOLoaderTest@7977b9b]>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.tom.project.dao.AaaaBaseDAOLoaderTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: public com.tom.project.dao.accounting.AccountingAccountDAO  com.tom.project.dao.AaaaBaseDAOLoaderTest.accountingAccountDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tom.project.dao.accounting.AccountingAccountDAO] 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.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:376)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:312)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at  org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
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)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.tom.projct.dao.accounting.AccountingAccountDAO com.tom.project.dao.AaaaBaseDAOLoaderTest.accountingAccountDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tom.project.dao.accounting.AccountingAccountDAO] 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$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 26 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.tom.project.dao.accounting.AccountingAccountDAO] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:986)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:856)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:768)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 28 more
2013-08-06 13:36:42,928 INFO [org.springframework.context.support.GenericApplicationContext] - <Closing org.springframework.context.support.GenericApplicationContext@165973ea: startup date [Tue Aug 06 13:36:42 EDT 2013]; root of context hierarchy>
2013-08-06 13:36:42,928 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6c4fc156: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy>

所以,这是applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref bean="pooledDataSource" />
    </property>
    <property name="configLocation">
        <value>classpath*:hibernate.cfg.xml</value>
    </property>
</bean>

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

<!-- Activates scanning of @Autowired -->
<context:annotation-config/>

<!-- Activates scanning of @Repository and @Service -->
<context:component-scan base-package="com.tom.project"/>

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>  

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven/>

</beans>

这是project-infrastructure.xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.2.xsd
    http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<!--  Properties files -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:hibernate.properties</value>
            <value>classpath*:project.properties</value>
        </list>
    </property>
</bean>

<!-- Database Connection Pool -->
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="idleConnectionTestPeriod" value="100800"/>
    <property name="maxIdleTime" value="201600"/>
    <property name="driverClass">
        <value>${project.hibernate.connection.driver_class}</value>
    </property>
    <property name="jdbcUrl">
        <value>${project.hibernate.connection.url}</value>
    </property>
    <property name="user">
        <value>${project.hibernate.connection.username}</value>
    </property>
    <property name="password">
        <value>${project.hibernate.connection.password}</value>
    </property>
    <property name="maxPoolSize">
        <value>1000</value>
    </property>
</bean>

<!--  Transaction Manager -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

</beans>

这是我DAO的界面:

public interface AccountingAccountDAO {
    void delete(AccountingAccount accountingAccountID);
    void persist(AccountingAccount accountingAccountID, User queryUser);
    List<AccountingAccount> getAllByAttribute(String fieldName, String name);
    AccountingAccount getById(int i);
}

DAO的实施:

@Repository("accountingAccountDAO")
public class HibernateAccountingAccountDAO implements AccountingAccountDAO
{
    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory()
    {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory)
    {
        this.sessionFactory = sessionFactory;
    }

    private static final Log logger = LogFactory.getLog(HibernateAccountingAccountDAO.class);

    @Override
    public void delete(AccountingAccount accountingAccountID)
     {
     }

    @Override
    public void persist(AccountingAccount accountingAccountID, User query2User)
    {
    }

    @Override
    public List<AccountingAccount> getAllByAttribute(String fieldName, String name)
    {
        return null;
     }

     @Override
     public AccountingAccount getById(int i)
     {
         return null;
     }

}

这是生成消息的单元测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations =
{    "classpath*:project-infrastructure.xml","classpath*:applicationContext.xml"})
public class AaaaBaseDAOLoaderTest
{
    @Autowired
    public AccountingAccountDAO accountingAccountDAO;

    public AccountingAccountDAO getAccountingAccountDAO()
    {
        return accountingAccountDAO;
    }

    public void setAccountingAccountDAO(AccountingAccountDAO accountingAccountDAO)
    {
        this.accountingAccountDAO = accountingAccountDAO;
    }

    @Test
    public void initResources()
    {
        accountingAccountDAO.getClass();
    }
}

3 个答案:

答案 0 :(得分:1)

我找到了答案。单元测试的问题在于我正在使用:

 @ContextConfiguration(locations =
{"classpath*:project-infrastructure.xml","classpath*:applicationContext.xml"})

这意味着这会引入完全错误的applicationContext.xml文件和错误的project-infrastructure.xml文件。

在我的Spring DAO应用程序中,我确保我专门为此项目使用了应用程序上下文文件,因此我将其重命名为一个唯一的名称:myproject1-app-context.xml和myproject1-infrastructure.xml文件。

在这个maven项目中,目录结构如下:

的src /主/ JAVA 的src / main /资源 src / main / resources / spring&lt; - 我的spring app上下文文件所在的位置

在单元测试中,我将位置更改为:

@ContextConfiguration(locations =
{"classpath:/spring/myproject1-infrastructure.xml",
"classpath:/spring/myproject1-app-context.xml"})

一旦我这样做,我就停止了自动装配错误。

我知道有一些代码或技巧可以指出我正在使用的类路径中的文件。我有一天应该使用它,但至少这解决了我的问题。

如前所述,在DAO类中使用@Repository,并从myproject1-app-context.xml文件中删除定义,我确实包括:

    <!-- Activates scanning of @Autowired -->
<context:annotation-config />

<!-- Activates scanning of @Repository and @Service -->
<context:component-scan base-package="com.tom.project" />

这包含在myproject1-app-context.xml文件中。

所以,我希望有时能帮助别人......

答案 1 :(得分:0)

你只需要定义你的bean两次!一个使用XML配置:

<bean id="accountingAccountDAO"
    class="com.tom.project.dao.accounting.HibernateAccountingAccountDAO">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

和一个@Repository注释

@Repository("accountingAccountDAO")
public class HibernateAccountingAccountDAO implements AccountingAccountDAO

删除XML定义以使其正常工作!

答案 2 :(得分:0)

为什么要在xml中定义accountingAccountDAO并使用注释@Repository("accountingAccountDAO")?删除xml定义并进行注释!