首页 » 技术分享 » aspectjweaver AspectJ

aspectjweaver AspectJ

 
文章目录

基于aop的依赖jar包,可以实现切面过滤拦截。

依赖包:

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.0.RELEASE</version>
</dependency>

使用:

1.@Aspect和@Configuration添加到定义的Aspect类上,指明切面类。

2.定义切点:@Pointcut("execution(* com.controller.Test2Controller.*(..))")

将@Pointcut定义到切点方法上

3.切面过滤注解:

@Before("pointcut()")

@Around("pointcut()")

@AfterReturning(value = "pointcut()",returning = "returnValue")

@After("pointcut()")

@AfterThrowing

这几个注解的过滤顺序为:

@Around-joinPoint.proceed(args)前

@Before

@Around-joinPoint.proceed(args)后

@After

@AfterReturning/@AfterThrowing

4.举个栗子:

import com.alibaba.fastjson.JSON;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Aspect
@Configuration
public class TestAspect {

    Logger logger = LoggerFactory.getLogger(getClass());

    // 定义切点Pointcut
    @Pointcut("execution(* .test.Test2Controller.*(..))")
    public void pointcut() {
        logger.info("pointcut---------------execute");
    }


    @Before("pointcut()")
    public void before(){
        logger.info("@Before------------execute");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{

        Signature signature = joinPoint.getSignature();
        logger.info("@Around-joinPoint.proceed(args)前");
        logger.info("@Around-执行目标方法前参数:"+Arrays.toString(joinPoint.getArgs()));
        Object[] args = joinPoint.getArgs();
        if(args != null &&args.length>0){
            args[0]="update";
        }
        Object proceed = joinPoint.proceed(args);
        logger.info("@Around-joinPoint.proceed(args)后");
        logger.info("@Around-被织入的目标对象:" + joinPoint.getTarget());
        logger.info("@Around-返回值:"+JSON.toJSONString(proceed));
        return JSON.toJSONString(proceed);
    }

    @AfterReturning(value = "pointcut()",returning = "returnValue")
    public void afterReturning(JoinPoint point, Object returnValue) {
        logger.info("@AfterReturning-方法:"+point.getSignature().getDeclaringTypeName()+point.getSignature().getName());
        logger.info("@AfterReturning-参数:"+Arrays.toString(point.getArgs()));
        logger.info("@AfterReturning-返回值:" + returnValue);
        logger.info("@AfterReturning-被织入的目标对象:" + point.getTarget());
    }

    @After("pointcut()")
    public void after(JoinPoint point){
        logger.info("@After-方法:"+point.getSignature().getDeclaringTypeName()+point.getSignature().getName());
        logger.info("@After-参数:"+Arrays.toString(point.getArgs()));
        logger.info("@After-被织入的目标对象:" + point.getTarget());
    }
    @AfterThrowing("pointcut()")
    public void afterThrowing(JoinPoint point){
        logger.info("@AfterThrowing-----",point.getTarget());
    }

}

控制台:

2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.around(TestAspect.java:36)
-@Around-joinPoint.proceed(args)前
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.around(TestAspect.java:37)
-@Around-执行目标方法前参数:[null]
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.before(TestAspect.java:29)
-@Before------------execute
执行test-----update
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.around(TestAspect.java:43)
-@Around-joinPoint.proceed(args)后
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.around(TestAspect.java:44)
-@Around-被织入的目标对象:net.buybal.order.controller.test.Test2Controller@155814e5
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.around(TestAspect.java:45)
-@Around-返回值:"update"
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.after(TestAspect.java:59)
-@After-方法:net.buybal.order.controller.test.Test2Controllertest
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.after(TestAspect.java:60)
-@After-参数:[update]
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.after(TestAspect.java:61)
-@After-被织入的目标对象:net.buybal.order.controller.test.Test2Controller@155814e5
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.afterReturning(TestAspect.java:51)
-@AfterReturning-方法:net.buybal.order.controller.test.Test2Controllertest
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.afterReturning(TestAspect.java:52)
-@AfterReturning-参数:[update]
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.afterReturning(TestAspect.java:53)
-@AfterReturning-返回值:"update"
2019-11-26 17:22:56 [http-bio-9998-exec-1] INFO -Caller+0    at *.TestAspect.afterReturning(TestAspect.java:54)
-@AfterReturning-被织入的目标对象:net.buybal.order.controller.test.Test2Controller@155814e5
2019-11-26 17:19:27 [http-bio-9998-exec-1] INFO -Caller+0    at *.aspect.TestAspect.around(TestAspect.java:36)
-@Around-joinPoint.proceed(args)前
2019-11-26 17:19:27 [http-bio-9998-exec-1] INFO -Caller+0    at *.aspect.TestAspect.around(TestAspect.java:37)
-@Around-执行目标方法前参数:[null]
2019-11-26 17:19:27 [http-bio-9998-exec-1] INFO -Caller+0    at *.aspect.TestAspect.before(TestAspect.java:29)
-@Before------------execute
执行test-----update
2019-11-26 17:19:27 [http-bio-9998-exec-1] INFO -Caller+0    at *.aspect.TestAspect.after(TestAspect.java:59)
-@After-方法:net.buybal.order.controller.test.Test2Controllertest
2019-11-26 17:19:27 [http-bio-9998-exec-1] INFO -Caller+0    at *.aspect.TestAspect.after(TestAspect.java:60)
-@After-参数:[update]
2019-11-26 17:19:27 [http-bio-9998-exec-1] INFO -Caller+0    at *.aspect.TestAspect.after(TestAspect.java:61)
-@After-被织入的目标对象:net.buybal.order.controller.test.Test2Controller@2965bb5f
2019-11-26 17:19:27 [http-bio-9998-exec-1] INFO -Caller+0    at *.aspect.TestAspect.afterThrowing(TestAspect.java:65)
-@AfterThrowing-----

 

转载自原文链接, 如需删除请联系管理员。

原文链接:aspectjweaver AspectJ,转载请注明来源!

0