---
title: "Spring2.5整合JPA"
author: "erikchang"
date: "2009-11-12T21:36:38Z"
tags: ["AOP", "Bean", "JPA", "Servlet", "Spring"]
slug: "spring2-5-jpa"
draft: false
lastmod: "2009-11-12T21:37:42Z"
id: 515734
url: "https://erikchang.iteye.com/blog/515734"
---

一直都在使用自己写的Spring2.0+JPA的开发基础框架，最近研究了一下Spring2.5，确实发现该升级一下自己的基础框架了，这里给Spring2.5整合JAP的心得贴上来，和大家共享，也算是一个笔记！

    Spring是个好东西，但是他是基于很多不同的jar来写的，对依赖包的版本要求很高，不兼容老版本的，所以使用哪一个版本的Spring，需要使用它对应的lib文件夹下的依赖包，这估计也是很多人使用Spring整合框架的失败支持！

   Spring2.5整合JPA按照以下步骤来做，肯定不会出问题。

第一步 拷贝相关包，列表如下：   

 

![](http://dl.iteye.com/upload/attachment/167254/ef4f4b97-6f42-3ef9-9bdb-4d8f236209e9.jpg)

这些包最好从Spring2.5源码下的lib文件夹中来找，避免版本冲突出现的某明其妙的问题！

第二步：配置web.xml文件，代码如下：

```
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/mvc-config.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>*.jhtml</url-pattern>
	</servlet-mapping>
</web-app>
```

 

在web.xml中使用contextConfigLocation来完成其他配置文件的导入。

第三步：配置ApplicationContext.xml，这个文件是Spring整合JPA最重要的文件，这里配置不当也会导致很多问题的，配置如下：

```
<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<!-- 启用支持Annotation注解方式的Bean管理 -->
	<context:component-scan base-package="com.javatakler"></context:component-scan>
	<!-- property-placeholder是一个属性遍历器，定位一个属性文件，属性文件存放的是jdbc一些连接数据 -->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!-- 配置数据源 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName"
			value="${driverClassName}"  />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
		<property name="initialSize" value="${initialSize}" />
		<property name="maxActive" value="${maxActive}" />
		<property name="maxIdle" value="${maxIdle}" />
		<property name="minIdle" value="${minIdle}" />
	</bean>
	
	<bean name="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<!--	<property name="allowRedeploymentWithSameName" value="true" /> -->
		<property name="persistenceXmlLocation"
			value="classpath:persistence.xml" />
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter">
			<bean
				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="database" value="MYSQL" />
				<property name="showSql" value="false" />
				<property name="generateDdl" value="false" />
			</bean>
		</property>
	</bean>
	<!-- JPA事务管理器 -->	
	<bean id="transactionManager"
		class="org.springframework.orm.jpa.JpaTransactionManager">
		<property name="entityManagerFactory"
			ref="entityManagerFactory" />
		<property name="dataSource" ref="dataSource" />
	</bean>
	<!-- 启用支持Annotation注解方式的事务管理 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
```

 

Spring2.5以前entityManagerFactory中有个allowRedeploymentWithSameName属性，该属性用来防止连接池中出现重名的现象，在Spring2.5中好像已经不存在这种问题的发生了，这里也不能配置了，已经没有该属性了！

最后一步就是配置persistence.xml文件了，这里文件默认是在META-INF文件夹下，实际上不一定这么做，使用persistenceXmlLocation来指定对应的位置就可以了，不过最好在classpath下！代码如下：

```
<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="template"
		transaction-type="RESOURCE_LOCAL">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.show_sql" value="false" />
			<property name="hibernate.hbm2ddl.auto" value="update" />
		</properties>
		</persistence-unit>
</persistence>
```

 

至此Spring2.5整合JPA的工作就全部结束了，启动一下web服务器，就能够顺利的进行下步开发了！