package com.hhh.aspect.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
/**
* 前置通知
* @param joinPoint 对目标对象的封装
*/
public void myBefore(JoinPoint joinPoint){
// joinPoint.getTarget(); 获取目标对象
// joinPoint.getSignature().getName(); 获取目标方法名
// joinPoint.getArgs(); 获取目标方法参数列表
// joinPoint.getThis(); 获取代理对象
System.out.println("Before..."+joinPoint.getSignature().getName());
}
/**
* 后置对象
* @param joinPoint
*/
public void myAfterReturning(JoinPoint joinPoint){
System.out.println("After..."+joinPoint.getSignature());
}
/**
* 环绕通知
*/
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("Around Before"+proceedingJoinPoint.getSignature().getName());
Object obj = proceedingJoinPoint.proceed();
System.out.println("Around After环绕通知"+proceedingJoinPoint);
return obj;
}
/**
* 异常通知
* @param e
*/
public void myAfterThrowing(Exception e){
System.out.println("Exception "+e);
}
/**
* 最终通知
*/
public void myAfter(){
System.out.println("最终通知");
}
}
修改业务层:创建接口及实现类
配置切面:
<?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">
<!-- 配置目标对象-->
<bean id="usersService" class="com.hhh.aspect.service.impl.usersServiceImpl"/>
<!-- 配置切面对象-->
<bean id="myAspect" class="com.hhh.aspect.aop.MyAspect"/>
<!-- 配置切面-->
<aop:config>
<aop:aspect ref="myAspect">
<!-- 配置切点-->
<aop:pointcut id="myPointcut" expression="execution(* com.hhh.aspect.service.*.*(..))"/>
<!-- 前置通知-->
<aop:before method="myBefore" pointcut-ref="myPointcut"/>
<!-- 后置通知-->
<aop:after-returning method="myAfterReturning" pointcut-ref="myPointcut"/>
<!-- 环绕通知-->
<aop:around method="myAround" pointcut-ref="myPointcut"/>
<!-- 配置异常通知-->
<aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointcut" throwing="e"/>
<!-- 最终通知-->
<aop:after method="myAfter" pointcut-ref="myPointcut"/>
</aop:aspect>
</aop:config>
</beans>
测试:
package com.hhh.schema_based.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import java.lang.reflect.Method;
public class BasedMyAspect implements MethodBeforeAdvice, AfterReturningAdvice, MethodInterceptor, ThrowsAdvice {
/**
* 环绕通知
* @param methodInvocation
* @return
* @throws Throwable
*/
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Around Before");
Object obj = methodInvocation.proceed();
System.out.println("Around After");
return obj;
}
/**
* 后置通知
* @param o
* @param method
* @param objects
* @param o1
* @throws Throwable
*/
@Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
System.out.println("After...");
}
/**
* 前置通知
* @param method
* @param objects
* @param o
* @throws Throwable
*/
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("Before....");
}
/**
* 异常通知
* @param ex
*/
public void afterThrowing(Exception ex){
System.out.println("异常通知");
}
}
配置多切面:
package com.hhh.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect //指定当前对象为切面对象
public class MyAspect {
/**
* 配置切点:后面的通知类型就可以用这个方法了
*/
@Pointcut("execution(* com.hhh.service.*.*(..)")
public void myPointcut(){
}
/**
* 前置通知
* @param joinPoint
*/
// @Before(value = "execution(* com.hhh.service.*.*(..)")
@Before(value = "myPointcut")
public void myBefore(JoinPoint joinPoint){
System.out.println("Before..."+joinPoint.getSignature().getName());
}
/**
* 后置通知
* @param joinPoint
*/
// @After(value = "execution(* com.hhh.service.*.*(..)")
@After(value = "myPointcut")
public void myAfterRetuning(JoinPoint joinPoint){
System.out.println("AfterReturning "+joinPoint.getSignature().getName());
}
/**
* 环绕通知
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around("myPointcut")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("Around Before");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("Around After");
return proceed;
}
/**
* 最终通知
*/
// @After("execution(* com.hhh.service.*.*(..)")
@After("myPointcut")
public void myAfter(){
System.out.println("最终通知");
}
/**
* 异常通知
* @param e
*/
// @AfterThrowing(value = "execution(* com.hhh.service.*.*(..)",throwing = "e")
@AfterThrowing(value = "myPointcut",throwing = "e")
public void myAfterThrowing(Exception e){
System.out.println("有异常 "+e);
}
}
创建目标对象:接口及实现类
配置注解式的切面;
package com.hhh.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect //指定当前对象为切面对象
public class MyAspect {
/**
* 配置切点:后面的通知类型就可以用这个方法了
*/
@Pointcut("execution(* com.hhh.service.*.*(..))")
public void myPointcut(){
}
/**
* 前置通知
* @param joinPoint
*/
// @Before(value = "execution(* com.hhh.service.*.*(..)")
@Before(value = "myPointcut()")
public void myBefore(JoinPoint joinPoint){
System.out.println("Before..."+joinPoint.getSignature().getName());
}
/**
* 后置通知
* @param joinPoint
*/
// @After(value = "execution(* com.hhh.service.*.*(..)")
@After(value = "myPointcut()")
public void myAfterRetuning(JoinPoint joinPoint){
System.out.println("AfterReturning "+joinPoint.getSignature().getName());
}
/**
* 环绕通知
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around("myPointcut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("Around Before");
Object proceed = proceedingJoinPoint.proceed();
System.out.println("Around After");
return proceed;
}
/**
* 最终通知
*/
// @After("execution(* com.hhh.service.*.*(..)")
@After("myPointcut()")
public void myAfter(){
System.out.println("最终通知");
}
/**
* 异常通知
* @param e
*/
// @AfterThrowing(value = "execution(* com.hhh.service.*.*(..)",throwing = "e")
@AfterThrowing(value = "myPointcut()",throwing = "e")
public void myAfterThrowing(Exception e){
System.out.println("有异常 "+e);
}
}
创建测试类:
新创建一个切面:
配置这个新添加的切面:
注意多切面执行的方法:
由于切面中@Order小的先执行:
注意执行的方式:先执行Order小的的环绕前置然后前置然后Order大的的前置然后方法然后后置。。。总之他是一个环形