我正在尝试编写一个启用 Spring + GAE 的 aoolication,但我遇到了一个非常奇怪的问题。
对于持久层,我使用 JPA 和以下 persistence.xml 文件:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="transactions-optional">
<provider>org.datanucleus.store.appengine.jpa.DatastorePersistenceProvider</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true" />
<property name="datanucleus.NontransactionalWrite" value="true" />
<property name="datanucleus.ConnectionURL" value="appengine" />
</properties>
</persistence-unit>
</persistence>
我也有带有以下声明的spring配置文件:
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<tx:annotation-driven />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="transactions-optional" />
</bean>
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="datastoreService" class="com.google.appengine.api.datastore.DatastoreServiceFactory" factory-method="getDatastoreService" />
<bean id="memcacheServiceUser" class="com.google.appengine.api.memcache.MemcacheServiceFactory" factory-method="getMemcacheService">
<constructor-arg value="UserCache"/>
</bean>
</beans>
最后我有一个标记为广告 @Repository 的 DAO 组件并扩展了 JpaDaoSupport 这个配置为使用扫描的 bean
在尝试初始化该 DAO bean 后,我收到以下异常:
Caused by: java.lang.IllegalArgumentException: entityManagerFactory or jpaTemplate is required
at org.springframework.orm.jpa.support.JpaDaoSupport.checkDaoConfig(JpaDaoSupport.java:120)
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$5.run(AbstractAutowireCapableBeanFactory.java:1467)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1465)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 30 more
然后我查看了日志,我可以看到 entityManagerFactory 在 DAO 初始化之前成功创建,但就在 DAO 对象的实际初始化开始之前,spring 只是破坏了包括这个在内的所有单例。有很多日志,但我确信 entityManagerFactory 也会创建,对我来说非常奇怪的一件事是为什么 spring 会使用以下日志消息销毁所有单例:
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1594a88: defining beans [org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,entityManagerFactory,transactionManager,datastoreService,memcacheServiceUser,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,userDaoJpa,employeeDS,messageSource]; root of factory hierarchy
Jan 14, 2011 11:12:12 AM org.springframework.beans.factory.support.DisposableBeanAdapter destroy
FINE: Invoking destroy() on bean with name 'entityManagerFactory'
Jan 14, 2011 11:12:12 AM org.springframework.orm.jpa.AbstractEntityManagerFactoryBean destroy
INFO: Closing JPA EntityManagerFactory for persistence unit 'transactions-optional'
Jan 14, 2011 11:12:12 AM org.springframework.web.context.ContextLoader initWebApplicationContext
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDaoJpa' defined in file