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 に更新しました。すべてのjarが更新されていることを確認しようとします。
現状では、アプリは maven でビルドされており、すべての Java コードがコンパイルされているようです。
この DAO は注釈付きのエンティティを使用せず、代わりに従来の hibernate.cfg.xml ファイルと多くの hbm.xml ファイルを使用します ... これを使用できることを願っています。非常に多くのエンティティを注釈付きエンティティに変換してから、DAO コードを変換するという考えは好きではありません。
いずれにせよ、以下の構成で、単一の DAO をロードして構成をテストしたいと思います。しかし、以下に示すようなエラーメッセージが表示されます。私はこれをGoogleとここで調査して、誰かが正確なエラーを持っているかどうかを確認するのに1日を費やしました.
新しいプロジェクトをゼロから構築した場合は、エンティティに注釈を付けることができますが、現在はそれがないため、この 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();
}
}