Wednesday, July 6, 2011

SPRING - Aspect Oriented Programming Example

This example captures the usage of the before and after advice during a method execution.

1. The Spring AOP related jar files should be available in your WEB-INF/lib or Maven Repository.

Jar File: spring-aop.jar
Maven Dependency: Refer Spring Dependencies From Maven for the list of Spring Jars that has to be added to pom.xml for any Spring based project.

2. Implement the Before and after advice that has get executed before and after the method execution. This example just logs the values before and after the method execution. This would help in removing boiler plate code from the project.

LoggingAfterAdvice.java:

package com.mybusiness.blueportal.aop;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.AfterReturningAdvice;

public class LoggingAfterAdvice implements AfterReturningAdvice {

Log log = LogFactory.getLog(LoggingAfterAdvice.class);

public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
log.info("Ending invokation : " + method);
}
}

LoggingBeforeAdvice.java:

package com.mybusiness.blueportal.aop;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;

public class LoggingBeforeAdvice implements MethodBeforeAdvice {

Log log = LogFactory.getLog(LoggingBeforeAdvice.class);

public void before(Method method, Object[] args, Object target)
throws Throwable {
if (log.isInfoEnabled()) {
log.info("Invoking Method : " + method);
log.info("With Arguments : " + args);
log.info("In Target : " + target);
}
}
}

3. Select the methods in which AOP has to be applied. In this example, i am selecting a simple method that is used to validate the user information. The AOP can be applied to multiple methods and multiple classes as well.

UserService.java

package com.mybusiness.blueportal.services;

import com.mybusiness.blueportal.beans.User;
public interface UserService {
public boolean validateUser(User user);
}

UserServiceImpl.java


package com.mybusiness.blueportal.services;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mybusiness.blueportal.beans.User;
public class UserServiceImpl implements UserService {

Log LOG = LogFactory.getLog(UserServiceImpl.class);

public boolean validateUser(User user) {
LOG.info("User Object : " + user.getFirstName());
return true;
}
}

4. Add the entries to the Spring Configuration to apply the AOP.

<bean name="/userSubmit.do"
class="com.mybusiness.blueportal.controllers.UserSubmitController">
<property name="sessionForm" value="true" />
<property name="commandName" value="user" />
<property name="commandClass" value="com.mybusiness.blueportal.beans.User" />
<property name="formView" value="UserSubmit" />
<property name="successView" value="UserSubmitSuccess" />
<property name="validator" ref="userValidator"></property>
<property name="userService" ref="userService"></property>
</bean>
<bean id="rLoggingBeforeAdvice"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="loggingBeforeAdvice" />
</property>
<property name="pattern" value=".*" />
</bean>

<bean id="rLoggingAfterAdvice"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="loggingAfterAdvice" />
</property>
                <!-- Tells to which kind of methods this PointCut has to be applied -->
<property name="pattern" value=".*" />
</bean>

<bean id="loggingBeforeAdvice"
class="com.mybusiness.blueportal.aop.LoggingBeforeAdvice" />
<bean id="loggingAfterAdvice"
class="com.mybusiness.blueportal.aop.LoggingAfterAdvice" />
<bean id="userServiceImpl"
class="com.mybusiness.blueportal.services.UserServiceImpl" />

<bean name="userService"
class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="proxyInterfaces"
value="com.mybusiness.blueportal.services.UserService" />
<property name="target">
<ref local="userServiceImpl" />
</property>
<property name="interceptorNames">
<list>
<value>rLoggingBeforeAdvice</value>
<value>rLoggingAfterAdvice</value>
</list>
</property>
</bean>


When the above configuration is executed, the logger statements should display the point cuts getting executed before and after the method execution.



2011-07-07 00:06:02 LoggingBeforeAdvice [INFO] Invoking Method : public abstract boolean com.mybusiness.blueportal.services.UserService.validateUser(com.mybusiness.blueportal.beans.User)
2011-07-07 00:06:02 LoggingBeforeAdvice [INFO] With Arguments : [Ljava.lang.Object;@14e45b3
2011-07-07 00:06:02 LoggingBeforeAdvice [INFO] In Target : com.mybusiness.blueportal.services.UserServiceImpl@1c1f5b2
2011-07-07 00:06:02 UserServiceImpl [INFO] User Object : asdf
2011-07-07 00:06:02 LoggingAfterAdvice [INFO] Ending invokation : public abstract boolean com.mybusiness.blueportal.services.UserService.validateUser(com.mybusiness.blueportal.beans.User)

No comments:

Post a Comment