springboot Aop 方法拦截、注解拦截

Action.java

//此注解只能修饰方法
@Target(ElementType.METHOD)
//当前注解如何去保持
@Retention(RetentionPolicy.RUNTIME)
//生成到API文档
@Documented
public @interface Action {
    String name();
}

AopConfig.java

//JAVA配置类
@Configuration
//Bean扫描器
@ComponentScan("com.wangzhi.springboot.aop.test")
//开启spring对aspectJ的支持
@EnableAspectJAutoProxy
public class AopConfig {}

这个类下的方法我们采用注解来拦截

@Service
public class DemoAnnotationService {

    @Action( name = "注解式拦截的add操作")
    public void add() {}
}

这个类下的方法我们采用方法规则来拦截

@Service
public class DemoMethodService {
    public void add() {}
}

LogAspect.java

@Aspect
@Component
public class LogAspect {
    
    //定义切面
    @Pointcut("@annotation(com.wangzhi.springboot.aop.test.Action)")
    public void annotationPointCut() {}
    
    //声明一个advice,并使用@pointCut定义的切点
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //从切面中获取当前方法
        Method method = signature.getMethod();
        //得到了方,提取出他的注解
        Action action = method.getAnnotation(Action.class);
        //输出
        System.out.println("注解式拦截" + action.name());
    }
    //定义方法拦截的规则
    @Before("execution(* com.wangzhi.springboot.aop.test.DemoMethodService.*(..))")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature =  (MethodSignature) joinPoint.getSignature();
        //拦截了方法
        Method method = signature.getMethod();
        //直接获取方法名字
        System.out.println("方法规则式拦截" + method.getName());
    }
}

启动器

public class Application {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class);
        DemoMethodService methodService = context.getBean(DemoMethodService.class);
        DemoAnnotationService annotationService = context.getBean(DemoAnnotationService.class);
        
        annotationService.add();
        methodService.add();
        
        context.close();
    }
}

pom.xml依赖

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.12.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.10</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.10</version>
    </dependency>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,227评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,288评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,290评论 25 709
  • 持续分享85天,20171007。张红 随着时间的流逝,很快要到了上班时间了,我倒比较期待。假期里阴雨绵绵,整...
    啊呦a7_94阅读 1,702评论 2 2
  • 一、互联网碎片化时代,需要具备的三种能力 1、碎片整理能力 有效方法:思维导图 2、多线程处理能力 3、放空能力(...
    d3ed2c762be7阅读 2,206评论 0 0

友情链接更多精彩内容