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

Showing posts with label Refactoring. Show all posts
Showing posts with label Refactoring. Show all posts

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

Useful RAD keyboard shortcuts

I used Rational Application Developer (RAD 7.0) to test the below keyboard shortcuts. All of the shortcuts are for the Java/J2EE perspectives:

Editor

Ctrl+M -> Maximize/Minimize the current editor

Ctrl+W -> Close the current editor

Ctrl+E -> Show all editor

Ctrl+D -> Delete line from cursor position

Ctrl+T -> Open the Type, if on method or class then shows the available subtypes

Cntrl+O -> Open the members

Ctrl+Shift+F6 -> Show available Editors

Ctrl+Shift+F7 -> Show available views (need to check)

Ctrl+Shift+F8 -> Show available perspectives (need to check)

Alt+ Arrow keys -> move forward/backword to the last change position

F12 -> Activate the editor

F3 -> Open the declaration

Cntrl+Shift+M -> Add import statement for the Class used in the code (shows error as underline red
                            for the class used but not imported the class i.e. import statement isn't written
Cntrl+Shift+O -> Organize the import statements i.e. if you've removed some classes from your
                            code but the important statement wasn't deleted this command will clean up those
                            unused  import statements

Cntrl + F4 -> to close the currently active tab on the Editor


Search

Cntrl+Shift+R -> Search for Resources

Cntrl+Shift+T -> Search for Types

Cntrl+K -> Go to the next search result

Cntrl+Alt+H -> Show the call hierarchy of a method (highlight on the method you want to see the call trace)

Cntrl+Shift+H -> To search for Java Classes (Type)


Code structure change


Alt+Shift+R -> Rename (refactor) of class/member


Note: To change the keyboard shortcut mapping, go to Windows -> Preferences -> Workbench -> Keys