zoukankan      html  css  js  c++  java
  • 第三章 AOP

    • 什么是AOP
    • AOP的编写方式

    什么是AOP?

    是一种面向切面的思想,关注的是切面中的相似功能,将这些功能抽离出来,提高代码的复用性

    AOP术语

    advice-通知:要执行的任务

      Spring切面有5种通知,Before、After、After-returning、After-throwing、Around

    Joinpoint-连接点:

    Pointcut-切点:

    Aspect-切面:切面是通知和切点的结合

    Introduction-引入:向现有的类添加方法或属性

    Weaving-织入:将切面应用到目标对象来创建新的代理对象的过程

    在目标对象的生命周期里有多个点进行织入

    编译期:AspectJ

    类加载期:

    运行期:Spring AOP

     

    如何表示切面?

    AspectJ指示器 描述
    arg() 限制连接点匹配参数为指定类型的执行方法
    @args() 限制连接点匹配参数由指定注解标注的执行方法
    execution() 用于匹配是连接点的执行方法
    this() 限制连接点匹配AOP代理Bean引用为指定类型的类
    target() 限制连接点匹配目标对象为指定类型的类
    @target() 限制连接点匹配特定的执行对象,这些对象对应的类要具备指定类型的注解
    within() 限制连接点匹配指定的类型
    @within() 限制连接点匹配指定注解所标注的类型
    @annotation 限制匹配带有指定注解连接点

    使用举例:


    exection(* com.spring.User.say(..)) - 第一个*表示返回值类型 第一个..表示参数列表  *表示任意返回类型,..表示任意参数

    exection(* com.spring.DD.say(..)) && within(com.spring.DD.*) - 两个指示器的交集,DD.* 表示DD类的任意方法

    && - 与;|| - 或;! - 非

    bean(beanId) - 限定BeanId,这是Spring自己提供的指示器

    可以使用XML和注解描述切面,XML方式繁琐,所以就使用注解

    注解编写切面演示

    1、编写切面

    package demo02.part01;

    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;

    /**
    * Created by sherry on 17/1/11.
    */
    @Aspect
    public class AspectDemo {

    /**
    * 日志
    */
    private Logger logger = LoggerFactory.getLogger(AspectDemo.class);

    //Instruent类的任意返回类型,任意参数的play方法
    @Pointcut("execution(* demo02.part01.Instruent.play(..))")
    public void pointcutmathod(){}

    @Before("pointcutmathod()")
    public void dd(){
    logger.debug("Before");
    }
    }


    2、配置切面

    package demo02.part01;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;

    /**
    * Created by sherry on 17/1/11.
    */
    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan("demo02")
    public class SpringBean {

    @Bean
    public AspectDemo aspectDemo(){
    return new AspectDemo();
    }
    }

    注意,一定要先使用@EnableAspectJAutoProxy打开AspectJ自动代理,否则无法使用切面

    3、测试

    package demo02.part01;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;

    /**
    * Created by sherry on 17/1/11.
    */
    public class Main {
    /**
    * 日志
    */
    private static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringBean.class);
    Instruent instruent = applicationContext.getBean(Instruent.class);
    instruent.play();

    }
    }


    10:09:23.392 [main] DEBUG demo02.part01.AspectDemo - Before
    10:09:23.410 [main] DEBUG demo02.part01.Instruent - 执行 play

    上述只是一个简单的演示

    复杂的包括环绕通知、目标方法参数设置与获取,在环绕通知中获取方法名等都是能够实现的。遇到实际情况了再来补充说明

  • 相关阅读:
    Kubernetes(k8s)1.12.2集群搭建
    Drools规则引擎入门指南(三)——使用Docker部署Workbench
    Drools规则引擎入门指南(二)
    网络协议 11
    网络协议 10
    网络协议 9
    网络协议 8
    网络协议 7
    网络协议 6
    网络协议 5
  • 原文地址:https://www.cnblogs.com/sherrykid/p/6270621.html
Copyright © 2011-2022 走看看