Tuesday, October 20, 2009

Spring & JPA for your web-application

This blog will help you to configure Spring with JPA for your web application.
Prerequisites:
1. Make sure you have these SPRING .jars in your classpath
spring.jar
spring-aspects.jar
common-logging.jar
log4j.jar
common-beanutils.jar
common-collections.jar
common-digester.jar

2. Make sure you have your JPA-persistence API in your classpath. You can get it from OpenJPA / Jboss Hibernate.
In my case I chose ejb3-persistence.jar from hibernate.

3. Make sure you have your persistence provider jars in classpath (hibernate / toplink / openjpa)
In my case I chose toplink and kept these jars in classpath.
toplink-essentials.jar
toplink-essentials-agent.jar
Note: Toplink requires class transformation when loading and unloading entities, so you should start your server with spring-agent.jar
In my case I started my tomcat6 server with this VM argument
-javaagent:\spring-framework-2.5\dist\weaving\spring-agent.jar

4. Make sure that you have your jdbc driver jars in classpath.
I copied mysql-connector-java.jar to tomcat's lib directory.

Steps to configure JPA + Spring in your web-application

Steps:1.Define your entities in my-persistence.xml
Note: We didn't define/write a "persistence.xml" so that the servlet containerwill not bring up the default persistence unit.

2.Define your spring listeners in web.xml

<context-param> <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <listener> <listener-class> org.springframework.web.context.request.RequestContextListener </listener-class> </listener>


3.Define a spring configuration file (WEB-INF/applicationContext.xml)

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd "></beans>

4.Define your datasource in applicationContext.xml

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" > <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/dbname" /> <property name="username" value="username" /> <property name="password" value="password" /> </bean>

5.Define your entityManagerFactory in applicationContext.xml


<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.TopLinkJpaVendorAdapter" > <property name="showSql" value="true" /> <property name="generateDdl" value="false" /> <property name="databasePlatform" value="oracle.toplink.essentials.platform.database.MySQL4Platform" /> </bean> </property>
<property name="persistenceXmlLocation" value="META-INF/my-persistence.xml" /> </bean>

6. Define your transactionManager in applicationContext.xml

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" > <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="dataSource" ref="dataSource" /> </bean>

7.Define your advice & point-cuts for transaction demarcation.I am using spring annotation for transaction demarcation.
<tx:annotation-driven />

8.Make sure that you configure these beans in applicationContext.xml for processing your JPA annotations
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>

Thats it !!! You have configured Spring with JPA in your web-application.

No comments:

Post a Comment