Spring Aop支持

时间:2022-07-22
本文章向大家介绍Spring Aop支持,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring提供了四种Aop支持:

  1. 基于代理的经典Spring Aop
  2. 纯Pojo切面
  3. @AspectJ注解驱动的切面
  4. 注入式@AspectJ切面

Aop编程过程:

1、定义普通业务组件

2、定义切入点,一个切入点可能横切多个业务组件

3、定义增强处理,增强处理就是在AOP框架为普通业务组件织入的处理动作

所以进行AOP编程的关键就是定义切入点和定义增强处理,一旦定义了合适的切入点和增强处理,AOP框架将自动生成AOP代理,即:代理对象的方法=增强处理+被代理对象的方法。

execution() 用于匹配连接点执行的方法
arg()   限定连接点匹配参数为指定类型的执行方法
this()  限定连接点匹配AOP代理的类型bean引用为指定类型的类
target()    限定连接点匹配目标对象为指定类型的类

只有execution()是实际执行匹配的,而其他指示器都是限制匹配的。所以我们通常使用execution指示器匹配,然后通过其他指示器进行限制。

例子:

public interface Performance {
    
    public void perform();
}

定义一个接口为接入点

execution(* concert.Perfirmance.perform(..))

并编写切点 第一个*代表匹配所有类型的返回值,中间的类名.方法名指定匹配的类的特定方法,()中的 .. 表示匹配所有的参数列表。 我们好可以在ececution中使用其他的匹配器,用来过滤匹配的方法:

execution(* concert.Perfirmance.perform(..) && within(concer.*))

上面的表达式表示匹配concert.Perfirmance的perform方法,并且该方法在concer包中。里面的&&表示与的意思,除此之外还有 || 表示或, ! 表示非。但是 & 在Xml中有特殊的含义,这时我们也可以使用 and 代替 && ,用 or 代替 || ,用 not 代替 ! 可以通过其他符号来代替。

同时可以在切点中选择Bean

execution(* concert.Perfirmance.perform(..)  and bean('woodstock'))
使用Xml创建切面

创建接口

public interface HelloWorld
{
    void printHelloWorld();
    void doPrint();
}

定义实现类

public class HelloWorldImpl1 implements HelloWorld
{
    public void printHelloWorld()
    {
        System.out.println("Enter HelloWorldImpl1.printHelloWorld()");
    }
    
    public void doPrint()
    {
        System.out.println("Enter HelloWorldImpl1.doPrint()");
        return ;
    }
}

定义横向关注点

public class TimeHandler
{
    public void printTime()
    {
        System.out.println("CurrentTime = " + System.currentTimeMillis());
    }
}

xml中进行配值

<aop:config>
            <aop:aspect id="time" ref="timeHandler">
                <aop:pointcut id="addAllMethod" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
                <aop:before method="printTime" pointcut-ref="addAllMethod" />
                <aop:after method="printTime" pointcut-ref="addAllMethod" />
            </aop:aspect>
</aop:config>
使用注解创建切面

1.使用@Aspect注解标注类为切面。

2.使用以下AspectJ注解来声明切面通知方法:

1)@After:通知方法在目标方法返回或抛出异常后调用;

2)@AfterReturning:通知方法在目标方法返回后调用;

3)@AfterThrowing:通知方法在目标方法抛出异常后调用;

4)@Around:通知方法将目标方法封装起来;

5)@Before:通知方法在目标方法调用之前执行。

@Aspect
public class TimeHandler
{
    @Before(execution(* com.xrq.aop.HelloWorld.*(..))
    public void printTime()
    {
        System.out.println("CurrentTime = " + System.currentTimeMillis());
    }
}

使用

 @Pointcut(execution(* com.xrq.aop.HelloWorld.*(..))")

定义切点表达式后就可以@Before("performance()")直接使用,省去了重复

@Around环绕通知是最强大的通知类型,它能够让你所编写的逻辑将被通知的目标方法完全包装起来,实际它就像在一个通知方法中同时编写前置通知和后置通知。