我正在尝试使用Spring + GAE编写一个aoolication,但我遇到了一个非常奇怪的问题。
对于持久层I,用户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>
我也有弹簧配置文件,声明如下:
<?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>
最后我有DAO组件,标记为ad @Repository并扩展了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 init,但是在实际开始初始化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
答案 0 :(得分:0)
JpaDaoSupport类希望您在构造上设置EntityManagerFactory
或JpaTemplate
。这样的事情应该这样做:
@Repository
public class YourDAOImpl extends JpaDaoSupport implements YourDAO
{
@Autowired
public YourDAOImpl(EntityManagerFactory entityManagerFactory)
{
setEntityManagerFactory(entityManagerFactory);
}
...
}
答案 1 :(得分:0)
您可以使用以下内容自动注入实体管理器。
@PersistenceContext
private EntityManager entityManager;
答案 2 :(得分:0)
@Autowired
public ClaseDaoImpl(EntityManagerFactory entityManagerFactory) {
setEntityManagerFactory(entityManagerFactory);
}
每个都由Dao执行。