1

我有一个打包成 jar 的 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();
    }
}
4

3 回答 3

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/main/java src/main/resources src/main/resources/spring <-- 我的 spring 应用程序上下文文件所在的位置

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

@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 文件中。

所以,我希望这对某人有所帮助......

于 2013-08-09T18:31:41.843 回答
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 定义以使其正常工作!

于 2013-08-06T18:12:57.603 回答
0

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

于 2013-08-06T18:15:12.563 回答