问题
We currently have Spring 3.2.9.RELEASE configured and running (for a couples of years) and need to migrate to 4.1.4.RELEASE. We have an abstract DAO class that extends org.springframework.orm.jpa.support.JpaDaoSupport as well as other references to:
org.springframework.orm.jpa.JpaCallbackorg.springframework.orm.jpa.JpaTemplate
I've seen that JpaDaoSupport has been removed in Spring 4. I've removed references to the Jpa* classes and replaced with
@PersistenceContext
protected EntityManager theEntityManager;
and for method references in our DAO (like findByNamedParams()) found in JpaDaoSupport, and copied into our DAO.
After the above changes, we are able to compile our code but when it comes to running our JUnit tests, there is a reference in our applicationContext-test.xml of
<bean id="jpaTemplate" class="org.springframework.orm.jpa.JpaTemplate">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="abstractDAO" abstract="true" class="my.company.package.AbstractDAO">
<property name="jpaTemplate" ref="jpaTemplate" />
</bean>
<bean id="genericDAO" parent="abstractDAO" class="my.company.package.GenericDAO" />
<bean id="securityDAO" parent="abstractDAO" class="my.company.package.SecurityDAOImpl" />
Basically the error is there is no class reference of org.springframework.orm.jpa.JpaTemplate. How do we replace this JpaTemplate configuration for Spring 4.1.4?
Note that I'm picking this code up and wasn't the one who initially configured the system. Also I'm pretty new to Spring and its configuration setup.
回答1:
- Make sure
AbstractDAOis migrated to theEntityManagerAPI and uses@PersistenceContexton an injection point (a setter usually). - Enable annotation configuration using
<context:annotation-config />or configure aPersistenceAnnotationBeanPostProcessorto enable the injection of theEntityManagerinto the DAOs. You can get rid of the bean definition forAbstractDAOentirely. Note, that if you have<context:component-scan />activated somewhere, this should already work out of the box.
PS: You might wanna consider to upgrade to Spring 4.2 right away if you're migrating anyway. If that's not an option, please go with the latest Spring 4.1 version (4.1.7 at the time of writing).
来源:https://stackoverflow.com/questions/31925138/how-to-migrate-usage-of-jpatemplate-from-spring-3-2-to-4-1-4