使用Spring 3 +,Hibernate 4,Junit 4.8我得到以下无法自动装载字段 msg
Running com.twitter.apidemo.test.integration.UserDAOHibernateTest
Dec 22, 2013 6:30:27 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [test-springContext.xml]
Dec 22, 2013 6:30:27 PM org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters
INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
Dec 22, 2013 6:30:27 PM org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider registerDefaultFilters
INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
Dec 22, 2013 6:30:27 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.GenericApplicationContext@121dcac: startup date [Sun Dec 22 18:30:27 GMT 2013]; root of context hierarchy
Dec 22, 2013 6:30:27 PM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from URL [file:C:/Users/xxx/yyyy/mySVNWorkSpace/TwitterApiDemo/src/main/webapp/WEB-INF/database/database.properties]
Dec 22, 2013 6:30:27 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Dec 22, 2013 6:30:27 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e86e8a: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,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,userDAOHibernate,propertiesConfigurer,dataSource,sessionFactory,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,transactionManager,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; root of factory hierarchy
Dec 22, 2013 6:30:28 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 4.3.0.Final
Dec 22, 2013 6:30:28 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName
INFO: Loaded JDBC driver: com.mysql.jdbc.Driver
Dec 22, 2013 6:30:29 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Dec 22, 2013 6:30:29 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.1.Final}
Dec 22, 2013 6:30:29 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Dec 22, 2013 6:30:29 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Dec 22, 2013 6:30:29 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Dec 22, 2013 6:30:30 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Dec 22, 2013 6:30:30 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Dec 22, 2013 6:30:30 PM org.springframework.orm.hibernate4.HibernateTransactionManager afterPropertiesSet
INFO: Using DataSource [org.springframework.jdbc.datasource.DriverManagerDataSource@a6a2c3] of Hibernate SessionFactory for HibernateTransactionManager
Dec 22, 2013 6:30:30 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1a54967] to prepare test instance [com.twitter.apidemo.test.integration.UserDAOHibernateTest@762be3]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.twitter.apidemo.test.integration.UserDAOHibernateTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate com.twitter.apidemo.test.integration.UserDAOHibernateTest.userDAOHibernate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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:1122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
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:15)
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:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
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:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate com.twitter.apidemo.test.integration.UserDAOHibernateTest.userDAOHibernate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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)
... 32 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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)
... 34 more
Dec 22, 2013 6:30:30 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@1a54967] to prepare test instance [com.twitter.apidemo.test.integration.UserDAOHibernateTest@70a141]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.twitter.apidemo.test.integration.UserDAOHibernateTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate com.twitter.apidemo.test.integration.UserDAOHibernateTest.userDAOHibernate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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:1122)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:379)
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:15)
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:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
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:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
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.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate com.twitter.apidemo.test.integration.UserDAOHibernateTest.userDAOHibernate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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)
... 32 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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)
... 34 more
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 4.04 sec <<< FAILURE!
Results :
Tests in error:
testDAOAutowire(com.twitter.apidemo.test.integration.UserDAOHibernateTest): Error creating bean with name 'com.twitter.apidemo.test.integration.UserDAOHibernateTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate com.twitter.apidemo.test.integration.UserDAOHibernateTest.userDAOHibernate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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)}
testFindAll(com.twitter.apidemo.test.integration.UserDAOHibernateTest): Error creating bean with name 'com.twitter.apidemo.test.integration.UserDAOHibernateTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate com.twitter.apidemo.test.integration.UserDAOHibernateTest.userDAOHibernate; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate] 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)
我的 test-springContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
">
<mvc:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="com.twitter.apidemo.persistence.hibernateDAO"/>
<!-- define place holder as a bean -->
<bean id="propertiesConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:C:/Users/xxx/yyyy/mySVNWorkSpace/TwitterApiDemo/src/main/webapp/WEB-INF/database/database.properties</value>
</list>
</property>
</bean>
<!-- declare datasource that has pooled connection capabilities -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- annotation-based configuration -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- declare datasource that has pooled connection capabilities -->
<property name="dataSource">
<ref bean="dataSource"/>
</property> <!-- already defined in dataSourcecontet refernce it here -->
<property name="annotatedClasses">
<list>
<value>com.twitter.apidemo.domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
</bean>
<!-- Hibernate transaction manager -->
**<tx:annotation-driven></tx:annotation-driven>**
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
UserDAOHibernateTest
public class UserDAOHibernateTest extends DomainAwareBase {
public UserDAOHibernateTest() {
}
@Autowired(required = true)
private UserDAOHibernate userDAOHibernate;
@Test
public void testDAOAutowire(){
assertNotNull(userDAOHibernate);
}
@Test
public void testFindAll(){
List<User> appUsers = userDAOHibernate.findAll();
userDAOHibernate.flush();
assert(appUsers.size() ==2);
}
}
DomainAwareBase是一个包含注释的抽象类
@TransactionConfiguration(defaultRollback=false)
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:test-springContext.xml"
UserDAOHibernateTest用于测试 UserDAOHibernate 实现
@Transactional
@Repository
public class UserDAOHibernate extends GenericDAOHibernate<User,Long > implements UserDAO {
@Transactional(readOnly = true)
@Override
public List<User> findAll() {
return super.findAll();
}
}
域类用户
@Entity
@Table(name = "AppUser")
public class User implements Serializable {
public User() {
}
public User(Long id) {
this.id = id;
}
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(columnDefinition = "int(10)", name = "AppUserId", nullable = false)
private Long id;
getters/setters and other fields not shown
当我注释掉 tx:annotationdriven 时,代码工作正常,否则我得到了BeanCreationError。我究竟做错了什么。非常感谢。
编辑来自proxy-target-class =“true”的堆栈跟踪
Running com.twitter.apidemo.test.integration.UserDAOHibernateTest
Dec 22, 2013 8:07:05 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [test-springContext.xml]
Dec 22, 2013 8:07:05 PM
Dec 22, 2013 8:07:06 PM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from URL [file:C:/Users/yyyyy/mySVNWorkSpace/TwitterApiDemo/src/main/webapp/WEB-INF/database/database.properties]
Dec 22, 2013 8:07:06 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
Dec 22, 2013 8:07:06 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in Dec 22, 2013 8:07:10 PM org.springframework.test.context.TestContextManager prepareTestInstance
SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@311578] to prepare test instance [com.twitter.apidemo.test.integration.UserDAOHibernateTest@78adb2]
java.lang.IllegalStateException: Failed to load ApplicationContext
at Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAOHibernate' defined in file [C:\Users\yyyy\mySVNWorkSpace\TwitterApiDemo\target\classes\com\twitter\apidemo\persistence\hibernateDAO\UserDAOHibernate.class]: Initialization of bean failed; nested exception is Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate]: Common causes of this Caused by: org.springframework.cglib.core.CodeGenerationException: java.lang.ClassCastException-->java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at com.twitter.apidemo.persistence.hibernateDAO.GenericDAOHibernate.<init>(GenericDAOHibernate.java:31)
at com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate.<init>(UserDAOHibernate.java:14)
at com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate$$EnhancerByCGLIB$$f06044b3.<init>(<generated>)
INFO: Loading XML bean definitions from class path resource [test-springContext.xml]
Dec 22, 2013 8:07:10 PM loadProperties
INFO: Loading properties file from URL [file:C:/Users//mySVNWorkSpace/TwitterApiDemo/src/main/webapp/WEB-INF/database/database.properties]
Dec 22, 2013 8:07:10 PM Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDAOHibernate' defined in file [C:\Users\yyy\mySVNWorkSpace\TwitterApiDemo\target\classes\com\twitter\apidemo\persistence\hibernateDAO\UserDAOHibernate.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.twitter.apidemo.persistence.hibernateDAO.UserDAOHibernate]: Tests in error:
testFindAll(com.twitter.apidemo.test.integration.UserDAOHibernateTest): Failed to load ApplicationContext
testDAOAutowire(com.twitter.apidemo.test.integration.UserDAOHibernateTest): Failed to load ApplicationContext
修改 GenericDAOHibernate
public abstract class GenericDAOHibernate<T,ID extends Serializable> implements GenericDAO<T, ID>{
private Class<T> persistentClass;
private Session session;
@Autowired
private SessionFactory sessionFactory;
@Autowired
public GenericDAOHibernate() {// need to know what is the class of the entity to persist find by reflection
this.persistentClass= (Class<T>)( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[0];
}
it has get/set session methods
答案 0 :(得分:0)
如果我们继续提议改变这个
public GenericDAOHibernate() {
this.persistentClass= (Class<T>)( (ParameterizedType) getClass().getGenericSuperclass() ).getActualTypeArguments()[0];
}
这将无效,因为getClass()
返回CGLIB正在创建的Proxy类。到
public GenericDAOHibernate() {
this.persistentClass = getDaoClass();
}
其中getDaoClass()
将在子类中实现,例如
public Class<User> /* Class<T> in the parent class */ getDaoClass() {
return User.class;
}
如果有效,我会添加其余的解释。