Showing posts with label spring 2. Show all posts
Showing posts with label spring 2. Show all posts

Recipe: Spring + JPA Annotation + Hibernate

1
Modern OO applications use a lot of Models to present the business domain object. It is often that we have to write a matching data access object for each models, and in most cases they are just normal CRUD operations.

What I am trying to do here is to create a generic way to create DAO from POJO that uses JPA as mapping tool to simplify the repeating tasks. The following example uses hibernate for DB connectivity.

Of course you can simply use EJB 3.0 and remove all this completely by using method like entityManager.persist(), but if we want to stay away from EJB and persistence model, this should give you a very good place to start. (I am using JPA as my mapping tool only because of my personal preference, you can use hibernate annotation, and it should work exactly the same way).

Step 1 - Create the Generic class
I am using the sample from the "Don't repeat the DAO". I think this is pretty neat.

GenericDao Interface

package com.blogspot.programmingpanda.commons

import java.io.Serializable;
import java.util.Collection;

public interface GenericDao&ls;T, PK extends Serializable> {
Collection<T> listAll();
}


The implementation...
GenericDaoHibernateImpl

package com.blogspot.programmingpanda.commons

import java.io.Serializable;
import java.util.Collection;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class GenericDaoHibernateImpl<T, PK extends Serializable> extends HibernateDaoSupport implements GenericDao<T, PK> {

private Class<T> type;

public GenericDaoHibernateImpl(Class<T> type){
this.type = type;
}

public Collection<T> listAll() {
return this.getHibernateTemplate().loadAll(this.type);
}

/**
* @return the type
*/
public Class<T> getType() {
return type;
}

/**
* @param type the type to set
*/
public void setType(Class<T> type) {
this.type = type;
}
}


Once we have the generic DAO class, we can use spring to inject the model into the DAO.

Now we have to create the model and use JPA to do the O/R Mapping

Step 2 - Create the model and map it to the database

package com.blogspot.programmingpanda.commons.models;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="myschema.model")
public class MyModel implements Serializable {

private Integer id;
private String col1;
private String col2;

/**
* @return the id
*/
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}

@Column(name="col1")
public String getCol1() {
return col1;
}

@Column(name="col2")
public String getCol2() {
return col2;
}

/** And all those setter method **/



Step 3 - Spring Config
After the Model and GenericDao class, all we have to do is to
i) Let the session factory know the class(es) to map

<!-- Annotation Session Factory Bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="mysqlDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<property name="annotatedClasses">
<list>

<value>com.blogspot.programmingpanda.commons.models.MyModel</value>
</list>

</property>
</bean>

ii) Create the dao by injecting the class into the GenericDao

<!-- This bean will replace the actual Dao class -->
<bean id="myModelDao" class="com.blogspot.programmingpanda.commons.GenericDaoHibernateImpl" autowire="byName">
<constructor-arg>
<value>com.blogspot.programmingpanda.commons.models.MyModel</value>
</constructor-arg>
</bean>


The full applicationContext.xml will look something like this

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

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/spring-config/dev.properties</value>
</list>
</property>
</bean>

<bean id="systemPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

<!-- MySql DS -->
<bean id="mysqlDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${mysql.url}" />
<property name="username" value="${mysql.username}" />
<property name="password" value="${mysql.password}" />
</bean>

<!-- Annotation Session Factory Bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="mysqlDataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<property name="annotatedClasses">
<list>

<value>com.blogspot.programmingpanda.commons.models.MyModel</value>
</list>

</property>
</bean>
<!--sessionFactory will get autowired-->
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate3.HibernateInterceptor"
autowire="byName" />

<!-- This bean will replace the actual Dao class -->
<bean id="myModelDao" class="com.blogspot.programmingpanda.commons.GenericDaoHibernateImpl" autowire="byName">
<constructor-arg>
<value>com.blogspot.programmingpanda.commons.models.MyModel</value>
</constructor-arg>
</bean>

<!-- Inject the Dao to an action -->
<bean id="action" class="actions.ListAction">
<property name="myModelDao" ref="myModelDao" />
</bean>

</beans>


Conclusion
This is only a minimal example demonstrating how we can use spring, JPA annotation and a generic dao template to avoid writing DAO class for each model we create in our application. You can always extend the GenericDao class to include your own method(s) other than the generic CRUD methods.

Using different log4j proerties for different environment in spring 2

1
Default location of log4j properties is in /WEB-INF/classes/logj4.properties. There are ways to point log4j to different properties. In spring, we can do something like this.


<!-- log4j setting -->
<bean id="log4jInitialization"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.springframework.util.Log4jConfigurer" />
<property name="targetMethod" value="initLogging" />
<property name="arguments">
<list>
<value>log4j.properties</value>
</list>
</property>
</bean>


where initLogging takes (String location), you can use absolute path or relative path here. But a more elegant way is to use a variable for the application root. like this


<value>${webapp.root}/${log4j.properties.location}</value>


You can get the ${webapp.root} from webAppRootKey. To bring it to the next level we can group all configs into 1 properties file and that will make the build process easier.

The full setup looks something like this

/WEB-INF/web.xml

<listener>
<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>webapp.root</param-value>
</context-param>


/WEB-INF/applicationContext.xml

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>/WEB-INF/spring-config/dev.properties</value>
</list>
</property>
</bean>
<!-- log4j setting -->
<bean id="log4jInitialization"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"
value="org.springframework.util.Log4jConfigurer"/>
<property name="targetMethod" value="initLogging"/>
<property name="arguments">
<list>
<value>${webapp.root}/${log4j.properties.location}</value>
</list>
</property>
</bean>


/WEB-INF/spring-config/dev.properties

#log4j setting
log4j.properties.location=WEB-INF/log4j-config/log4j.dev.properties


/WEB-INF/log4j-config/log4j.dev.properties

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.Threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=DEBUG, stdout

/WEB-INF/log4j-config/log4j.prod.properties

# Configuration for receiving e-mails when ERROR messages occur.
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.To=to@mydomain.com
log4j.appender.mail.From=from@mydomain.com
log4j.appender.mail.SMTPHost=smtp.mydomain.com
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.BufferSize=1
log4j.appender.mail.Subject=An application error occured
log4j.appender.mail.layout=org.apache.log4j.HTMLLayout

# Standrd System.out appender
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.rootLogger=INFO, stdout, mail

Accessing Context/System (Application Root) properties in Spring

0
In the last post I talked about how to parameterize the applicationContext.xml with a properties file. But sometime we would like to access system/context properties in the applicationContext.xml (for example, you want to reference a file in your webapp, but the class only takes absolute path). The spring PropertyPlaceholderConfigurer class would do the work for you as well.

By default, the system property mode is set to use fall back (SYSTEM_PROPERTIES_MODE_FALLBACK), which means if a property is not in our properties file that we supply, the property will be filled with the one in the context/system.

One good use would be getting the application root and use it to reference a config file.

For example

${webapp.root}/WEB-INF/velocity


(In reference to Spring documentation velocity properties chapter)

In order to use ${webapp.root} you will need to setup WebAppRootListener in web.xml



org.springframework.web.util.WebAppRootListener



and assign it to a context param



webAppRootKey
webapp.root


Parameterize Spring applicationContext with properties file

0
Spring has a very useful class called PropertyPlaceholderConfigurer that helps you to parameterize your applicationContext.xml file. In order to use this, you will need to add the bean in spring like this




/WEB-INF/spring-config/dev.properties




where the property location points to the list of properties files.

I use this primarily becasue
1. easier to maintain properties in a properties file than applicationContext
2. switch between different environments, e.g. development vs. staging vs. production

An example of the use of this would be

/WEB-INF/web.xml


org.springframework.web.context.ContextLoaderListener



contextConfigLocation
/WEB-INF/applicationContext.xml



/WEB-INF/spring-config/dev.properties

# Dev DB Setting
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://dev.mydomain.com:3306/
mysql.username=devuser
mysql.password=xxx

/WEB-INF/spring-config/prod.properties

# Product DB Setting
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://prod.mydomain.com:3306/
mysql.username=produser
mysql.password=xxx

/WEB-INF/applicationContext.xml




/WEB-INF/spring-config/dev.properties













To switch between environments, all you have to update is


/WEB-INF/spring-config/prod.properties

in the applicationContext.xml

To make it fancier, of course you can supply the properties to an ant script when you build your application.