1.************代理类***********************
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @creationTime:2017年10月26日 上午12:07:22
* @version: 1.1
*/
public class OrderServiceHelper {
public void before(JoinPoint jp){
System.out.println("拦截的目标类"+"\t"+jp.getSignature().getDeclaringTypeName());
System.out.println("拦截的方法名称"+"\t"+jp.getSignature().getName());
System.out.println("前置通知");
}
public void afterReturning(JoinPoint pjp,Object val){
System.out.println("目标方法返回值"+val);
System.out.println("后置通知");
}
public Object around(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("环绕前");
Object value = pjp.proceed();
System.out.println("环绕后");
return value;
}
public void afterThrowing(JoinPoint jp,Throwable ex){
System.out.println("发现异常..."+ex);
}
public void after(JoinPoint jp){
System.out.println(jp.getSignature().getName());
System.out.println("最终通知");
}
}
2.*****************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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--目标类-->
<bean id="orderService" class="cn.xxxx.aspectj.OrderServiceImpl"></bean>
<!--通知-->
<bean id="orderServiceAdvice" class="cn.xxxx.aspectj.OrderServiceHelper"></bean>
<!--使用aop:config来声明,使用 aop:aspect来配置切面(false表示在没有借口的情况下,默认使用cglib)-->
<aop:config proxy-target-class="false">
<aop:aspect ref="orderServiceAdvice">
<!--切点-->
<aop:pointcut expression="execution(* cn.xxxx.aspectj..*(..))" id="anyPointcut"/>
<!--前置通知-->
<aop:before method="before" pointcut="execution(* *..add(..))"/>
<!--后置通知-->
<aop:after-returning method="afterReturning" pointcut-ref="anyPointcut" returning="val"/>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="anyPointcut"/>
<!--异常抛出通知-->
<aop:after-throwing method="afterThrowing" pointcut-ref="anyPointcut" throwing="ex"/>
<!--最终通知-->
<aop:after method="after" pointcut-ref="anyPointcut"/>
</aop:aspect>
</aop:config>
</beans>