Featured Post

The great debacle of healthcare.gov

This is the first time in history when the president of the United States of America, or probably for any head of state around the world,...

Tuesday, November 13, 2007

Implement Spring in a Struts project

The implementation of Spring framework in a matured project could be tricky. Following step by step progressive integration could help to ease some of the pains:

- To prepare a project for Spring, first of all, we've to ensure that Inversion of Control Design concept is followed through out the application. And for that preparation the team might need to undergo thorough refactoring and/or redesigning, depending on the existing implementation and design.

- Write or re-write the business logic and data logic (DAO) classes with DI pattern. Create the Spring context XML file for the business and DAO classes. Inject the business logic objects into the Action classes using any Spring Context.

- After refactoring the existing code enabling Dependency Injection, second step is to prepare the Struts action classes to integrate with Spring. In this case the same DI needs to be enabled in the Action classes. The Action class shouldn't be burdened with redundant logic codes other than controlling logics. Use the Delegation Proxy to create the Struts Action classes through Spring. Before that ensure that the injection of

- Remove all logging cross-cut codes from the classes and use Spring Interceptor (BeforeMethod advice) and implement the logging

- To manage the application's transaction using Spring declarative transaction, implement it through proxy. The transaction declaration needs more time to analyze rather than implement. Do the homework on transaction grouping and then implement it in the xml file. Rigorously testing requires to confirm functional and performance requirements.

Struts-Spring Integration Using Proxy

DelegatingActionProxy is the object that plays the role to create Struts Action class when the request is invoked.

1. Modify your struts-config.xml file to give the DelegatingActionProxy to play its role

path="/someAction" name="someForm"
type="org.springframework.web.struts.DelegatingActionProxy"
input="/someOtherAction.do">


2. Add an entry for the path, in this case, '/someAction' in the spring configuration




Open View in transaction in Spring with Hibernate

1. Create a Filter to handle the transaction:


AppFilter</filter-name>
Application Transaction Filter

org.springframework.web.filter.DelegatingFilterProxy



2. Set the Filter to accept any request ends with .d0

AppFilter
*.


3. Set the Listener that loads Spring's WebAppContext


org.springframework.web.context.ContextLoader


4. set the parameter value of the configuration file that configures the Transaction Filter and Transaction Manager


contextConfigLocation

/WEB-INF/WebContext.xml



5. Configure the Filter










6. Implement the Filter class

DefaultTransactionDefinition def = new DefaultTransactionDefinition();

if (!TransactionSynchronizationManager.hasResource(sessionFactory)) {
session = SessionFactoryUtils.getSession(sessionFactory, true);
TransactionSynchronizationManager.bindResource(sessionFactory, new SessionHolder(session));
}

status = transManager.getTransaction(def);
chain.doFilter(req, res);

//commit
getTransactionManager().commit(status);
//rollback
getTransactionManager().rollback(status);

//Close the Session
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSession(session);


Resources:

http://springtips.blogspot.com/ - Spring tips
Spring-Struts integration - Spring integration with Struts Action
Spring transaction blog - A compact discussion of spring transaction implementation

No comments: