Sunday, September 29, 2013

Spring AOP - Part II

For Part -I, refer blog: Spring AOP-Part I

Let me give a simple example of AOP to add logging service.



<!--  logging -->

AOP Targets:

These are the interfaces/classes on which AOP will be applied. We will define a simple tax calculation service.
public interface TaxService {
        public double calculate(String state,String price);


public class TaxServiceImpl implements TaxService{
         public double calculate(String state, String price) {
                double tax = 0.0;
                double priceDbl = Double.parseDouble(price);
                       tax =priceDbl +  priceDbl *0.25;
                       tax =priceDbl +  priceDbl *0.20;
                return tax;

Aspect classes:

These are the interface/classes where you will implement the cross cutting concern. In our case, logging. Note that we will get the JoinPoint instance, from which we can derive the method arguments being passed to the target methods.Logging will be  applied before entering a method, after method exit as well in case of any exception being thrown.
public interface LoggingService {
       public void logEntry(JoinPoint jp);
       public void logExit(JoinPoint jp);
       public void logException(JoinPoint jp);

public class LoggingServiceImpl implements LoggingService{
        private static final Logger logger = LoggerFactory.getLogger(com.myorg.service.LoggingService.class);
        public void logEntry(JoinPoint joinPoint){
                logger.debug("Entering method: "+joinPoint + " with args: "+Arrays.toString(joinPoint.getArgs()));
        public void logExit(JoinPoint joinPoint){
                logger.debug("Exit method: "+joinPoint);
        public void logException(JoinPoint joinPoint){
                logger.debug("In Exception method: "+joinPoint);

Spring Config XML

Here we configure the AOP targets and the point cuts and specify the expression for identifying the pointcuts.

<bean id="taxService" class="com.myorg.service.TaxServiceImpl"/>
<bean id="loggingService" class="com.myorg.service.LoggingServiceImpl"/>

    <aop:aspect ref="loggingService">  
        <aop:pointcut  id ="logMethod" expression="execution(* com.myorg.service.TaxService.*(..))"/>
        <aop:before  method="logEntry"  pointcut-ref="logMethod"/>
        <aop:after  method="logExit"  pointcut-ref="logMethod"/>
       <aop:after-throwing  method="logException"  pointcut-ref="logMethod"/>


log4j properties

Place the below file in classpath to configure the log4j.

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
# Set the name of the file

# Set the immediate flush to true (default)

# Set the threshold to debug mode

# Set the append to false, overwrite

# Define the layout for file appender


Now that the configurations and code is in place, invoke the TaxService from any of the controllers and you will find the log statements for every method invoked on TaxService. This way, logging is separated out of the actual business logic. 


No comments:

Post a Comment