Using AOP by declaring/annotation in Spring

Categories: Java; Tagged with: ; @ June 26th, 2012 23:21

Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-introduction-defn

Here is a simple example showing the AOP feature in Spring, like the definition of bean, we can use AOP by declaring or annotation. we have a method that can insert a new user into the database, and we want to trace a log by AOP:

	/**
	 * Insert new user using transactionTemplate.
	 * @param user
	 */
	public void insertUserByTxTemplate(final User user) {
		session.save(user);
	}

1. Using AOP by Declaring

First, we need to crate an Aspect class:

package com.liguoliang.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logging {
	
	public void preLog() {
		System.out.println("PreLog...");
	}
	
	public void postLog() {
		System.out.println("PostLog...");
	}
	
	public void aroundLog(ProceedingJoinPoint joinPoint) {
		System.out.println("Around...Logging begin..");
		
		try {
			joinPoint.proceed();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Around...Logging end...");
	}

}

and then, we need define the AOP in spring config file:

<bean id="logging" class="com.liguoliang.spring.aop.Logging"></bean>
    <aop:config>
        <aop:aspect ref="logging">
            <aop:before pointcut="execution(* com.liguoliang.spring.UserHibernateDAO.insertUserByTxTemplate(..))"  method="preLog" />
            <aop:after pointcut="execution(* com.liguoliang.spring.UserHibernateDAO.insertUserByTxTemplate(..))"  method="postLog" />
            <aop:around pointcut="execution(* com.liguoliang.spring.UserHibernateDAO.insertUserByTxTemplate(..))"  method="aroundLog" />
        </aop:aspect>
    </aop:config>

And here is the output:

PreLog...
Around...Logging begin..
Exception catched...rollback now...
PostLog...
Around...Logging end...

2. Using Annotation.

We can define the pointcut and advices in the aspect class:

package com.liguoliang.spring.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoggingAnnotation {
	
	@Pointcut("execution(* com.liguoliang.spring.UserHibernateDAO.insertUserByTxTemplate(..))")
	public void newUser() {
	}
	
	@Before("newUser()")
	public void preLog() {
		System.out.println("[Annotation] PreLog...");
	}
	
	@After("newUser()")
	public void postLog() {
		System.out.println("[Annotation] PostLog...");
	}
	
	@Around("newUser()")
	public void aroundLog(ProceedingJoinPoint joinPoint) {
		System.out.println("[Annotation] Around...Logging begin..");
		
		try {
			joinPoint.proceed();
		} catch (Throwable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("[Annotation] Around...Logging end...");
	}

}

Because all configuration info is sorted in this class, so the spring config file is different:

<bean id="loggingAnnotation" class="com.liguoliang.spring.aop.LoggingAnnotation"></bean>
<aop:aspectj-autoproxy />

and here is the output:

[Annotation] PreLog...
[Annotation] Around...Logging begin..
Exception catched...rollback now...
[Annotation] PostLog...
[Annotation] Around...Logging end...

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.