Saturday, August 18, 2007

Spring

Spring is a light weight frame work to develop enterprise applications. It can be used to configure declarative transactions, web services, mailing facilities and other configuration information.

Spring Architecture

The architecture consists of seven modules.

1. AOP (Aspect oriented programming)

The OO programming addressed designing complex systems as group of entities and interaction among the components. But the changes in requirement have impact on developing applications. The AOP allows the developers to dynamically modify the OO model to create new system. It consists of fours features,

1. Cross-Cutting concerns:

The OO model performs primary functions along with secondary functions. The secondary functions are shared across other models. Example, we need to add logging code with in DAO class, Servlet whenever the thread enters and exists.

2. Advice:

This is the additional code that you need to apply to your existing model.

3. Point-Cut:

It is a point of execution where the Cross-Cutting concerns needs to be applied. In our example, the point cut is reached when the thread enters and thread exists.

4. Aspect:

The combination of advice and point-cut is called aspect. In our example, we add aspect to our application by defining point-cut and giving correct advice.

2. ORM (Object Related Model)

The ORM is related to database access. It provides the integration layers for Hibernate, JDO and iBatis.

3. DAO (Data Access Object)

It is primarily for standardizing the data access work using Hibernate, JDO and others.

4. Context

It provides consistent of accessing the applications resources.

5. Web

It is a part of web application development.

6. Web MVC

These modules provide the MVC implementation.

7. Core

The core is important modules of Spring framework. It provides Dependency Injection features. The BeanFactory separates the object creation, initialization and access of the objects.


How Spring is configured?

The spring listener is configured in the web.xml. The listener initializes when the server starts up. It looks for a parameter contextConfigLocation in the web.xml.

org.springframework.web.context.ContextLoadListener

contextConfigLocation

WEB-INF/sampleContext.xml

How Hibernate is configured?

· A hibernate mapping file has to be created for each POJO (Plain Old Java Object).

This mapping file has to be defined in the applicationcontext.xml.

· Hibernate mapping file will be defined in the applicationcontext.xml file.

"http://www.springframework.org/dtd/spring-beans.dtd">

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

org.hsqldb.jdbcDriver

jdbc:hsqldb:db/appfuse

sa

class="org.springframework.orm.hibernate.LocalSessionFactoryBean">

org/appfuse/model/User.hbm.xml

net.sf.hibernate.dialect.HSQLDialect

create

class="org.springframework.orm.hibernate.HibernateTransactionManager">

JNDI Data source

class="org.springframework.jndi.JndiObjectFactoryBean">

java:comp/env/jdbc/appfuse

Implementing Hibernate with DAO

The DAO class has to implement the HibernateDAOSupport class.

Public SampleDAO extends HibernateDAOSupport implements ISampeDAO {

Public List getUsers(int userId) {

Return (List) getHibernateTemplate().find(“from user”);

}

Public UserVO getUser(int userId) {

Return (UserVO) getHibernateTemplate().get(UserVO.class,userid);

}

public void deleteUser(UserVO user) {

getHibernateTemplate().delete(user);

}

public void saveUser(UserVO user) {

getHibernateTemplate().save(user);

}

public void updateUser(UserVO user) {

getHibernateTemplate().update(user);

}

}

The VO object has to implement three methods.

public abstract String toString();

public abstract boolean equals(Object o);

public abstract int hashCode();

Business Delegate With Hibernate

The Spring framework allows layering the components. So the presentation layer does not need to access the DAO layer directly. The business delegate pattern facilitates separating the layers. It accesses the DAO to retrieve and store the data and the presentation layer interacts with the business delegate.

class="org.appfuse.service.UserManagerImpl">

class="org.springframework.transaction.interceptor.TransactionProxy

FactoryBean">

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED

PROPAGATION_REQUIRED,readOnly