zoukankan      html  css  js  c++  java
  • Aop_AspectJ实现

    Aop(面向切面编程)

    aop中的横切关注点也就是说的通用模块,可以被模块化为特殊的类,这些类被称为切面,可以通过通过声明的方式定义这个功能要以何种方式在何处应用。

    AOP术语:

    通知(Advice):增强的功能,切面要完成的工作。

    通知类型:

      前置

      后置

      返回

      异常

      环绕

    连接点(Join point):是在程序执行过程中能够应用通知的所有点,是一个运行时用到的概念,连接点可以是调用方法时、抛出异常时等。

    切点(Poincut):定义了通知被应用的具体位置,“何处”

    切面(Aspect):通知和切点的集合,通知和切点共同定义了切面的全部内容——它是什么,何时在何处完成其功能。

    引入(Introduction)

    织入(Weaving)

    spring提供了4种类型的aop支持    spring对aop的支持局限于方法拦截

    基于代理的经典spring aop

    纯pojo切面

    @Aspectj注解驱动的切面

    注入式Aspectj切面

    Spring中AspectJ使用

      1 perform()作为切点

    package com.springa.aop.concert;
    //切点
    public interface Performance {
        public void perform();
    }

    2 定义切面

    package com.springa.aop.concert;
    import org.aspectj.lang.annotation.*;
    @Aspect
    public class Audience {
        @Pointcut("execution(public * com.springa.aop.concert.Performance.perform(..))")
        public void performance(){}
    
        @Before("performance()")
        public void silenceCellPhones(){
            System.out.println("Silencing cell phones");
        }
        @Before("performance()")
        public void takeSeats(){
            System.out.println("Taking seata");
        }
        @AfterReturning("performance()")
        public void applause(){
            System.out.println("CALL CALL");
        }
        @AfterThrowing("execution(public * com.springa.aop.concert.Performance.perform(..))")
        public void demandRefund(){
            System.out.println("Demanding a refund");
        }
    }

    @Aspect  表明Audience不仅仅是一个Pojo,还是一个切面

    如果是使用普通的javaconfig的话,需要使用@EnableAspectJAutoProxy启用自动代理。xml文件通过

    <aop:aspectj-autoproxy />开启。

    package com.springa.aop.concert;
    
    import com.springa.aop.aspect.Audience;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan
    public class ConcertConfig {
        @Bean
        public Audience audience(){
            return new Audience();
        }
    }

    上面是使用spring的代码,使用springboot参考下面:

    在sprigboot中,在pom.xml中导入了依赖,默认是开启了aop,不需要@EnableAspectJAutoProxy注解

    步骤1 不变,定义切面步骤中使用@Compoent注解,不使用配置类。

    目录结构和上面的有所不同。完成了上面的可以进行测试了。

     

     Git地址:https://github.com/hutaoying/springboot1/tree/master/aop    springboot代码实现

  • 相关阅读:
    Flutter采坑之路 Run Configuration error:broken configuration due to unavailable
    Android24以上拍照代码
    android Studio 出现:Unable to resolve dependency for ':app@debug/compileClasspath'
    Android Studio 使用本地gradle配置详解
    windows server2008 IIS搭建网站简易教程(阿里云)
    FileProvider 添加二级目录
    Android中如何解决editText一进入activity就自动获取焦点的bug
    关于AndroidStudio 经常弹出TortoiseSVN 同步的解决办法
    Awesome-VR
    Magento2 常见错误 ----- 定期更新
  • 原文地址:https://www.cnblogs.com/joan-HTY/p/10102817.html
Copyright © 2011-2022 走看看