栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring中AOP的操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring中AOP的操作

1、切入指定方法 切入方法
@RestController
@RequestMapping("/")
public class AopDemo {

    @GetMapping("/test")
    public void test(){
        System.err.println("执行内容。。。。。。");
    }
}
@Aspect配置类
@Component
@Aspect
public class AopConfig {

    //前置通知
    @Before(value = "execution(* com.king.demo.web.AopDemo.test(..))")
    public void before() {
        System.out.println("before.....");
    }

    // 最终通知, 不管有没有异常都通知
    @After(value = "execution(* com.king.demo.web.AopDemo.test(..))")
    public void after() {
        System.out.println("after.....");
    }

    //返回通知, 返回值执行完才通知   //后置通知
    @AfterReturning(value = "execution(* com.king.demo.web.AopDemo.test(..))")
    public void afterReturning() {
        System.out.println("afterReturning.....");
    }

    //异常通知
    @AfterThrowing(value = "execution(* com.king.demo.web.AopDemo.test(..))")
    public void afterThrowing() {
        System.out.println("afterThrowing.....");
    }

    //环绕通知, 方法前后都执行
    @Around(value = "execution(* com.king.demo.web.AopDemo.test(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around.....环绕之前....");
        proceedingJoinPoint.proceed();
        System.out.println("around.....环绕之后.....");

    }
}
执行结果
around.....环绕之前....
before.....
执行内容。。。。。。
afterReturning.....
after.....
around.....环绕之后.....
2、切入指定方法-自定义注解 切入方法
@RestController
@RequestMapping("/")
public class AopDemo {

    @GetMapping("/test")
    @King(value = "king")
    public void test(){
        System.err.println("执行内容。。。。。。");
    }
}
自定义注解类
@Documented
@Target(value={ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface King {

    String value() default "";
}
具体切入逻辑
@Component
@Aspect
public class KingAno {

    @Pointcut("@annotation(com.king.demo.config.King)")
    private void pointcut(){

    }

    @Around(value = "pointcut() && @annotation(king)")
    public void around(ProceedingJoinPoint proceedingJoinPoint, King king) throws Throwable {
        System.err.println("注解value字段值:"+king.value());
        proceedingJoinPoint.proceed();
    }
}
执行结果
注解value字段值:king
执行内容。。。。。。
3、切入指定方法-自定义注解-指定参数 切入方法
@RestController
@RequestMapping("/")
public class AopDemo {

    @GetMapping("/test")
    @King(value = "king")
    public void test(String str){
        System.err.println("执行内容。。。。。。");
    }
}
具体切入逻辑代码
@Component
@Aspect
public class KingAno {

    @Pointcut("@annotation(com.king.demo.config.King) && args(java.lang.String)")
    private void pointcut(){

    }

    @Around(value = "pointcut() && @annotation(king)")
    public void around(ProceedingJoinPoint proceedingJoinPoint, King king) throws Throwable {
        System.err.println("注解value字段值:"+king.value());
        proceedingJoinPoint.proceed();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/969925.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号