第一步:引入相应jar包.
* aspectj依赖aop环境.
* spring-aspects-3.2.0.RELEASE.jar
* com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
第二步:编写被增强的类:InfoDao
第三步:使用AspectJ注解形式:
@Aspect
public class MyAspect {
}
}
第四步:创建applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启AspectJ自动代理 -->
<aop:aspectj-autoproxy/>
<bean id="infoDao" class="cn.aspectj.InfoDao"></bean>
<!-- 定义切面 -->
<bean id="myaspect" class="cn.aspectj.MyAspect"></bean>
</beans>
@Before 前置通知,相当于BeforeAdvice
* 就在方法之前执行.没有办法阻止目标方法执行的.
@AfterReturning 后置通知,相当于AfterReturningAdvice
* 后置通知,获得方法返回值.
@Around 环绕通知,相当于MethodInterceptor
* 在可以方法之前和之后来执行的,而且可以阻止目标方法的执行.
@AfterThrowing抛出通知,相当于ThrowAdvice
@After 最终final通知,不管是否异常,该通知都会执行
@DeclareParents 引介通知,相当于IntroductionInterceptor (不要求掌握)
package cn.aspectj;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
/**
* 切面类:就是切点与增强的结合
* //注解的方式通知
* 如果某个方法多进行通知。比如(value="execution(* cn.aspectj.InfoDao.find(..))")
* 可以自己定义切点,方便些,用@Pointcut
* @author Administrator
*
*/
@Aspect
public class MyAspect {
/**
* @Before前置通知
* @param joinPoint
*/
@Before("execution(* cn.aspectj.InfoDao.add(..))")//某个类的的某个方法进行增强,如果是包下.*是包下的所有.包前的*与包要有空格
public void before(JoinPoint joinPoint) {
//joinPoint的值:execution(void cn.aspectj.InfoDao.add())
System.out.println("*******前置增强"+joinPoint);
}
/**
* @AfterReturing 后置通知
* @param returnValue
*/
@AfterReturning(value="execution(* cn.aspectj.InfoDao.find(..))",returning="returnValue")
public void afterReturning(Object returnValue){
//返回值是增强方法的返回值
System.out.println("后置增强*******"+"返回值:"+returnValue);
}
/**
* @Around 环绕通知
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around(value="execution(* cn.aspectj.InfoDao.delete(..))")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("****环绕前增强");
//如果不调用 ProceedingJoinPoint的 proceed方法,那么目标方法就被拦截了
Object returnValueObject=proceedingJoinPoint.proceed();
System.out.println("环绕后增强*****");
return returnValueObject;
}
/**
* @AfterThrowing 抛出通知
* @param e
*/
@AfterThrowing(value="execution(* cn.aspectj.InfoDao.update(..))",throwing="e")
//两个e变量保持一致
public void Afterthrow(Throwable e){
//不出现异常,不会输出
System.out.println("出现异常!!!"+"异常信息"+e.getMessage());
}
/**
*@After 最终通知
*/
@After(value="execution(* cn.aspectj.InfoDao.update(..))")
public void after(){
System.out.println("最终通知!!!----无论是否出现异常都会执行!");
}
}
上图的代码注意点:
注意:
* Advisor和Aspect的区别:
* Advisor:Spring传统意义上的切面:支持一个切点和一个通知的组合.
* Aspect:可以支持多个切点和多个通知的组合.