Tuesday, October 20, 2009

Spring & JPA for your web-application

This blog will help you to configure Spring with JPA for your web application.
1. Make sure you have these SPRING .jars in your classpath

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.
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

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: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/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.

Thursday, October 8, 2009

The SPRING season...

Spring, a non-intrusive framework was developed with the aim to reduce the complexity of Java Enterprise developement.

Spring plumbs together your components (POJOs) of a modular java application, may it be enterprise or web and stays out of your code.

At the core of Spring framework is the Spring Container that constructs your POJOs (on demand) and wire them together with dependency injection (a form of IoC - Inversion of Control ) to construct the application.

Spring comes with ready-made modules to support ORM,Java Enterprise Apps,Web MVC,Remoting & AOP.

Straight forward applications of Spring are
1) Plumbing / Wiring POJOs together to construct a modular application
2) Aspect Oriented Programming (AOP)
3) Dependency Injection
4) Abstraction of data access through DAO pattern
5) Abstraction of various ORMs (Hibernate, Toplink, JPA) and uniform simple access to all.

Friday, October 2, 2009

Integrate Spring with JSF1.2

With JSF1.2 the <variable-resolver> element of faces configuration (faces-config.xml) has been deprecated. The new element <el-resolver> has been added and replaces <variable-resolver> in <application> element tree.

When we integrate Spring with JSF we write in our faces-config.xml as


But with JSF1.2 we should write and integrate Spring with JSF as


Your web.xml entries remains unchanged

Note: SpringBeanFacesELResolver is available only in Spring 2.5 API