Spring AOP的配置可以采用两种方式:XML和Annotation(需要JDK 5.0+)。
1.以XML方式配置:
业务逻辑类BusinessServiceImpl .java:
package org.wh.tech.spring.aop;
import org.springframework.beans.factory.annotation.Autowired;
import org.wh.tech.spring.sample.SimpleBean;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:业务逻辑类
*/
public class BusinessServiceImpl implements BusinessService {
@Autowired
private SimpleBean simpleBean;
@Override
public void addProcess(String processName) {
simpleBean.print(processName + " is added !") ;
}
@Override
public boolean updateProcess(String processName) {
simpleBean.print(processName + " is updated !") ;
return true;
}
}
XML切面类XMLExampleAspect.java:
package org.wh.tech.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:XML配置切面类
*/
public class XMLExampleAspect {
/*
* Around advice: 执行在join point这个方法执行之前与之后的advice。
*/
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");
return retVal;
}
/*
* After advice: 执行在join point这个方法执行之后的advice。
*/
public void doAfter(JoinPoint jp) {
System.out.println("log Ending method: "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}
/*
* Before advice: 执行在join point这个方法执行之前的advice。
*/
public void doBefore(JoinPoint jp) {
System.out.println("log Begining method: "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}
/*
* Throwing advice: 执行在join point这个方法抛出异常之后的advice。
*/
public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println("method " + jp.getTarget().getClass().getName()
+ "." + jp.getSignature().getName() + " throw exception");
System.out.println(ex.getMessage());
}
}
测试类TestXmlAop.java
package org.wh.tech.spring.aop.test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.wh.tech.spring.aop.BusinessService;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:以XML方式配置Aop测试
*/
@ContextConfiguration(locations={"classpath:applicationContext-*.xml"})
public class TestXmlAop extends AbstractJUnit4SpringContextTests{
@Autowired
private BusinessService businessService;
@Test
public void testAddProcess(){
businessService.addProcess("1st,");
}
}
applicationContext.xml需要配置:
<!-- 以XML方式配置Aop --> <aop:config> <aop:aspect id="xmlAspect" ref="xmlAspectBean"> <aop:pointcut id="businessService" expression="execution(* org.wh.tech.spring.aop.*.*(..))" /> <aop:after pointcut-ref="businessService" method="doAfter" /> <aop:before pointcut-ref="businessService" method="doBefore" /> <aop:around pointcut-ref="businessService" method="doAround" /> </aop:aspect> </aop:config> <bean id="xmlAspectBean" class="org.wh.tech.spring.aop.XMLExampleAspect" /> <bean id="businessServiceImpl" class="org.wh.tech.spring.aop.BusinessServiceImpl"></bean>
2.以Annotation方式配置:
切面类AnnotationExampleAspect.java:
package org.wh.tech.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.wh.tech.spring.sample.SimpleBean;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:通过annotation注解配置aop
*/
@Aspect
public class AnnotationExampleAspect {
@Autowired
private SimpleBean simpleBean;
/*
* 定义pointcunt
* */
@Pointcut("execution(* org.wh.tech.spring.aop.*.*(..))")
public void aPointcut() {
}
/*
* After-Returning advice: 执行在join point这个方法抛出异常之后的advice。
*/
@AfterReturning(pointcut = "aPointcut()", returning="r")
public void doAfterReturning(boolean r) {
if(r){
simpleBean.print("return is :" + r);
}else{
simpleBean.print("r is null");
}
}
}
测试类TestAnnotationAop.java:
package org.wh.tech.spring.aop.test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.wh.tech.spring.aop.BusinessService;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:以Annotation方式配置Aop测试
*/
@ContextConfiguration(locations={"classpath:applicationContext-*.xml"})
public class TestAnnotationAop extends AbstractJUnit4SpringContextTests {
@Autowired
private BusinessService businessService;
@Test
public void testUpdateProcess(){
businessService.updateProcess("update,");
}
}
applicationContext.xml只需添加:
<!-- 以annotation方式配置Aop --> <aop:aspectj-autoproxy /> <bean id="annotationExampleAspect" class="org.wh.tech.spring.aop.AnnotationExampleAspect" />
3.小结:
两种方式各有优势:
1)XML方式灵活性更高,一旦出现配置变更,只需要修改配置文件而不需要程序进行改动,但配置较为复杂。
2)Annotation方式配置较为简洁,但变更成本较高,需要对程序进行修改。