您的位置:首页 > 运维架构 > Tomcat

Tomcat下使用Spring + JPA 需要注意的问题

2013-11-15 17:22 429 查看
Tomcat默认的类加载器不支持类转换,所以需要使用自定义的类加载器,具体操作步骤如下:

1.在LocalContainerEntityManagerFactoryBean中,设置loadTimeWeaver属性

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceXmlLocation"
value="classpath:META-INF/persistence.xml" />
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</property>
</bean>
2.在WEB应用的META-INF/context.xml文件中,使用自定义类加载器

<Context antiJARLocking="true" path="/web">
<!--
由于需要在Tomcat下使用JPA,并且和Spring进行集成,所以需要使用自定义的类加载器
复制spring-instrument-tomcat.jar文件到${CATALINA_HOME}/lib目录下
-->
<Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
</Context>
3.下载spring-instrument-tomcat-xxxxx.jar
最好使用和Spring版本相同的jar包,根据使用不同版本的Spring,可以在在Maven存储库中搜索:
http://search.maven.org/#search|ga|1|spring-instrument-tomcat
我使用的是spring-instrument-tomcat-3.2.3.RELEASE.jar,下载了文件以后,把jar文件复制到tomcat的lib

转自:http://blog.163.com/java_star/blog/static/1177148052013627389268/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Spring Jpa Tomcat