7.Spring之基于Schema的AOP

概述

AOP, 即面向方面编程(Aspect Oriented Programming),是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP),其存在目的是为了解耦,在进行OOP开发时,都是基于对组件(比如类)进行开发,然后对组件进行组合,OOP最大问题就是无法解耦组件进行开发,而AOP就是为了克服这个问题而出现的,AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口来使代码的耦合度降低,且类继承只能单继承,AOP弥补了OOP的不足。

AOP为开发者提供一种进行横切关注点分离并织入的机制,把横切关注点分离,然后通过某种技术织入到系统中,从而无耦合的完成了我们的功能。

AOP的作用

• 用于横切关注点的分离和织入横切关注点到系统;

• 完善OOP;

• 降低组件和模块之间的耦合性;

• 使系统容易扩展;

• 而且由于关注点分离从而可以获得组件的更好复用。

AOP的基本概念

进行AOP开发,我们经常要和如下几个概念打交道:

• 连接点(Jointpoint):表示需要在程序中插入横切关注点的扩展点,连接点可能是类初始化、方法执行、方法调用、字段调用或处理异常等等,Spring只支持方法执行连接点,在AOP中表示为“在哪里干”;

• 切入点(Pointcut):选择一组相关连接点的模式,即可以认为连接点的集合,Spring支持perl5正则表达式和AspectJ切入点模式,Spring默认使用AspectJ语法,在AOP中表示为“在哪里干的集合”;

• 通知(Advice):在连接点上执行的行为,通知提供了在AOP中需要在切入点所选择的连接点处进行扩展现有行为的手段;包括前置通知(before advice)、后置通知(after advice)、环绕通知(around advice),在Spring中通过代理模式实现AOP,并通过拦截器模式以环绕连接点的拦截器链织入通知;在AOP中表示为“干什么”;

• 方面/切面(Aspect):横切关注点的模块化,可以认为是通知、引入和切入点的组合;在Spring中可以使用Schema和@AspectJ方式进行组织实现;在AOP中表示为“在哪干和干什么集合”;

• 引入(inter-type declaration):也称为内部类型声明,为已有的类添加额外新的字段或方法,Spring允许引入新的接口(必须对应一个实现)到所有被代理对象(目标对象), 在AOP中表示为“干什么(引入什么)”;

• 目标对象(Target Object):需要被织入横切关注点的对象,即该对象是切入点选择的对象,需要被通知的对象,从而也可称为“被通知对象”;由于Spring AOP 通过代理模式实现,从而这个对象永远是被代理对象,在AOP中表示为“对谁干”;

• AOP代理(AOP Proxy):AOP框架使用代理模式创建的对象,从而实现在连接点处插入通知(即应用切面),就是通过代理来对目标对象应用切面。在Spring中,AOP代理可以用JDK动态代理或CGLIB代理实现,而通过拦截器模型应用切面。

• 织入(Weaving):织入是一个过程,是将切面应用到目标对象从而创建出AOP代理对象的过程,织入可以在编译期、类装载期、运行期进行。

 

AOP主要用于横切关注点分离和织入,因此需要理解横切关注点和织入:

• 关注点:可以认为是所关注的任何东西

• 关注点分离:将问题细化从而单独部分,即可以理解为不可再分割的组件,如上边的日志组件和支付组件;

• 横切关注点:一个组件无法完成需要的功能,需要其他组件协作完成,比如非业务的:日志、事务处理、缓存、性能统计、权限控制等等这些非业务的基础功能;还可能是业务的:如某个业务组件横切于多个模块。

• 织入:横切关注点分离后,需要通过某种技术将横切关注点融合到系统中从而完成需要的功能,因此需要织入,织入可能在编译期、加载期、运行期等进行。

 

基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面、切入点及声明通知。

在Spring配置文件中,所以AOP相关定义必须放在<aop:config>标签下,该标签下可以有<aop:pointcut>、<aop:advisor>、<aop:aspect>标签,配置顺序不可变。

• <aop:pointcut>:用来定义切入点,该切入点可以重用;

• <aop:advisor>:用来定义只有一个通知和一个切入点的切面;

• <aop:aspect>:用来定义切面,该切面可以包含多个切入点和通知,而且标签内部的通知和切入点定义是无序的;和advisor的区别就在此,advisor只包含一个通知和一个切入点。

声明切面

切面就是包含切入点和通知的对象,在Spring容器中将被定义为一个Bean,Schema方式的切面需要一个切面支持Bean,该支持Bean的字段和方法提供了切面的状态和行为信息,并通过配置方式来指定切入点和通知实现。

切面使用<aop:aspect>标签指定,ref属性用来引用切面支持Bean。

切面支持Bean“aspectSupportBean”跟普通Bean完全一样使用,切面使用“ref”属性引用它。

声明切入点

切入点在Spring中也是一个Bean,Bean定义方式可以有三种方式:

1)在<aop:config>标签下使用<aop:pointcut>声明一个切入点Bean,该切入点可以被多个切面使用,对于需要共享使用的切入点最好使用该方式,该切入点使用id属性指定Bean名字,在通知定义时使用pointcut-ref属性通过该id引用切入点,expression属性指定切入点表达式:

<aop:config>
    <aop:pointcut id="pointcut" expression="execution(*com.spring..*.*(..))"/>
    <aop:aspect ref="aspectSupportBean">
        <aop:before pointcut-ref="pointcut" method="before"/>
    </aop:aspect>
</aop:config>

2)在<aop:aspect>标签下使用<aop:pointcut>声明一个切入点Bean,该切入点可以被多个切面使用,但一般该切入点只被该切面使用,当然也可以被其他切面使用,但最好不要那样使用,该切入点使用id属性指定Bean名字,在通知定义时使用pointcut-ref属性通过该id引用切入点,expression属性指定切入点表达式:

<aop:config>
    <aop:aspect ref="aspectSupportBean">
        <aop:pointcut id=" pointcut" expression="execution(* com.spring..*.*(..))"/>
        <aop:before pointcut-ref="pointcut" method="before"/>
    </aop:aspect>
</aop:config>

3)匿名切入点Bean,可以在声明通知时通过pointcut属性指定切入点表达式,该切入点是匿名切入点,只被该通知使用:

<aop:config>
    <aop:aspect ref="aspectSupportBean">
        <aop:after pointcut="execution(* com.spring..*.*(..))" method="afterFinallyAdvice"/>
    </aop:aspect>
</aop:config>

声明通知

在AOP中,通过切入点选择目标对象的连接点,然后在目标对象的相应连接点处织入通知,而切入点和通知就是切面(横切关注点),而在目标对象连接点处应用切面的实现方式是通过AOP代理对象,接下来再让我们具体看看Spring有哪些通知类型:

  • 前置通知(Before Advice):在切入点选择的连接点处的方法之前执行的通知,该通知不影响正常程序执行流程(除非该通知抛出异常,该异常将中断当前方法链的执行而返回)。
  • 后置通知(After Advice):在切入点选择的连接点处的方法之后执行的通知,包括如下类型的后置通知:

1、后置返回通知(After returning Advice):在切入点选择的连接点处的方法正常执行完毕时执行的通知,必须是连接点处的方法没抛出任何异常正常返回时才调用后置通知。

2、后置异常通知(After throwing Advice): 在切入点选择的连接点处的方法抛出异常返回时执行的通知,必须是连接点处的方法抛出任何异常返回时才调用异常通知。

3、后置最终通知(After finally Advice): 在切入点选择的连接点处的方法返回时执行的通知,不管抛没抛出异常都执行,类似于Java中的finally块。

  • 环绕通知(Around Advices):环绕着在切入点选择的连接点处的方法所执行的通知,环绕通知可以在方法调用之前和之后自定义任何行为,并且可以决定是否执行连接点处的方法、替换返回值、抛出异常等等。

AOP代理

AOP代理就是AOP框架通过代理模式创建的对象,Spring使用JDK动态代理或CGLIB代理来实现,Spring缺省使用JDK动态代理来实现,从而任何接口都可别代理,如果被代理的对象实现不是接口将默认使用CGLIB代理,不过CGLIB代理当然也可应用到接口。AOP代理的目的就是将切面织入到目标对象。

已经简单介绍了AOP相关概念,接下来就通过HelloWorld来简单介绍下AOP的使用

添加依赖

mvn方式

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.0.2.RELEASE</version>
</dependency>

gradle方式

compile 'org.springframework:spring-context:5.0.2.RELEASE'

compile 'org.springframework:spring-aop:5.0.2.RELEASE'

编写接口

public interface IHelloWorldService {
    public void sayHello();
}

实现接口

public class HelloWorldService implements IHelloWorldService {
    @Override
    public void sayHello() {
        System.out.println("============Hello World!");
    }
}

定义切面支持类

有了目标类,该定义切面了,切面就是通知和切入点的组合,而切面是通过配置方式定义的,因此这定义切面前,我们需要定义切面支持类,切面支持类提供了通知实现:

public class HelloWorldAspect {
    //前置通知
    public void beforeAdvice(String param) {
        System.out.println("===========before advice param:" + param);
    }

    //后置返回通知
    public void afterReturningAdvice(Object retVal) {
        System.out.println("===========after returning advice retVal:" + retVal);
    }

    //后置异常通知
    public void afterThrowingAdvice(Exception exception) {
        System.out.println("===========after throwing advice exception:" + exception);
    }

    //后置最终通知
    public void afterFinallyAdvice() {
        System.out.println("===========after finally advice");
    }

    //环绕通知
    public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("===========around before advice");
        Object retVal = pjp.proceed(new Object[]{"replace"});
        System.out.println("===========around after advice");
        return retVal;
    }

}

声明通知

基于Schema方式支持前边介绍的5中通知类型:

一、前置通知(Before Advice):在切入点选择的连接点处的方法之前执行的通知,该通知不影响正常程序执行流程(除非该通知抛出异常,该异常将中断当前方法链的执行而返回)。通过<aop:aspect>标签下的<aop:before>标签声明:

<aop:before pointcut="切入点表达式" pointcut-ref="切入点Bean引用" method="前置通知实现方法名" arg-names="前置通知实现方法参数列表参数名字"/>
  • pointcut和pointcut-ref:二者选一,指定切入点;
  • method:指定前置通知实现方法名,如果是多态需要加上参数类型,多个用“,”隔开,如beforeAdvice(java.lang.String);
  • arg-names:指定通知实现方法的参数名字,多个用“,”分隔,可选,类似于 构造器注入中的参数名注入限制:在class文件中没生成变量调试信息是获取不到方法参数名字的,因此只有在类没生成变量调试信息时才需要使用arg-names属性来指定参数名,如arg-names="param"表示通知实现方法的参数列表的第一个参数名字为“param”。

首先在IhelloWorldService接口类中添加

void sayBefore(String param);

其次在HelloWorldService实现该方法

然后在HelloWorldAspect定义通知实现:

public class HelloWorldAspect {
    //前置通知
    public void beforeAdvice(String param) {
        System.out.println("===========before advice param:" + param);
    }
}

最后在advice.xml配置文件中进行如下配置:

<bean id="helloWorldService" class="com.spring.aop.service.impl.HelloWorldService"/>
   <bean id="aspect" class="com.spring.aop.HelloWorldAspect"/>
    <aop:config>
        <aop:aspect ref="aspect">
             <aop:before pointcut="execution(* com.spring..*.sayBefore(..)) and args(param)" 
                         method="beforeAdvice(java.lang.String)" 
                         arg-names="param"/>
        </aop:aspect>
    </aop:config>        
</beans>

测试方法:

public void testSchemaBeforeAdvice() {
    System.out.println("======================================");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/advice.xml");
    IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);
    helloworldService.sayBefore("before");
    System.out.println("======================================");
}

分析下代码:

1)切入点匹配:在配置中使用“execution(* com.spring..*.sayBefore(..)) ”匹配目标方法sayBefore,且使用“args(param)”匹配目标方法只有一个参数且传入的参数类型为通知实现方法中同名的参数类型;

2)目标方法定义:使用method=" beforeAdvice(java.lang.String) "指定前置通知实现方法,且该通知有一个参数类型为java.lang.String参数;

3)目标方法参数命名:其中使用arg-names=" param "指定通知实现方法参数名为“param”,切入点中使用“args(param)”匹配的目标方法参数将自动传递给通知实现方法同名参数。

二、后置返回通知(After returning Advice):在切入点选择的连接点处的方法正常执行完毕时执行的通知,必须是连接点处的方法没抛出任何异常正常返回时才调用后置通知,通过<aop:aspect>标签下的<aop:after-returning>标签声明:

<aop:after-returning pointcut="切入点表达式" pointcut-ref="切入点Bean引用"
method="后置返回通知实现方法名"
arg-names="后置返回通知实现方法参数列表参数名字"
returning="返回值对应的后置返回通知实现方法参数名"/>
  • pointcut和pointcut-ref:同前置通知同义;
  • method:同前置通知同义;
  • arg-names:同前置通知同义;
  • returning:定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法执行正常返回后,将把目标方法返回值传给通知方法;returning限定了只有目标方法返回值匹配与通知方法相应参数类型时才能执行后置返回通知,否则不执行,对于returning对应的通知方法参数为Object类型将匹配任何目标返回值。

首先在IhelloWorldService接口中定义一个方法:

boolean sayAfterReturning();

其次在HelloWorldService中实现

public boolean sayAfterReturning() {
    System.out.println("============after returning");
    return true;
}

第三在HelloWorldAspect定义通知实现:

public void afterReturningAdvice(Object retVal) {
    System.out.println("===========after returning advice retVal:" + retVal);
}

最后在advice.xml配置文件中接着前置通知配置的例子添加如下配置:

<aop:after-returning pointcut="execution(* com.spring..*.sayAfterReturning(..))"
                     method="afterReturningAdvice"
                     arg-names="retVal"
                     returning="retVal"/>

测试代码

public void testSchemaIntroduction() {
    System.out.println("======================================");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/advice.xml");
    IIntroductionService introductionService = ctx.getBean("helloWorldService", IIntroductionService.class);
    introductionService.induct();
    System.out.println("======================================");
}

分析一下:

1)切入点匹配:在配置中使用“execution(* com.spring..*.sayAfterReturning(..)) ”匹配目标方法

sayAfterReturning,该方法返回true;

2)目标方法定义:使用method="afterReturningAdvice"指定后置返回通知实现方法;

3)目标方法参数命名:其中使用arg-names="retVal"指定通知实现方法参数名为“retVal”;

4)返回值命名:returning="retVal"用于将目标返回值赋值给通知实现方法参数名为“retVal”的参数上。

 

三、后置异常通知(After throwing Advice): 在切入点选择的连接点处的方法抛出异常返回时执行的通知,必须是连接点处的方法抛出任何异常返回时才调用异常通知,通过<aop:aspect>标签下的<aop:after-throwing>标签声明:

<aop:after-throwing>标签声明:
<aop:after-throwing pointcut="切入点表达式" pointcut-ref="切入点Bean引用"
method="后置异常通知实现方法名"
arg-names="后置异常通知实现方法参数列表参数名字"
throwing="将抛出的异常赋值给的通知实现方法参数名"/>

throwing:定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法抛出异常返回后,将把目标方法抛出的异常传给通知方法;throwing限定了只有目标方法抛出的异常匹配与通知方法相应参数异常类型时才能执行后置异常通知,否则不执行,对于throwing对应的通知方法参数为Throwable类型将匹配任何异常。

首先在IhelloWorldService定义一个测试方法:

public void sayAfterThrowing();

其次在HelloWorldService定义实现:

@Override
public void sayAfterThrowing() {
    System.out.println("============before throwing");
    throw new RuntimeException();
}

第三在HelloWorldAspect定义通知实现:

public void afterThrowingAdvice(Exception exception) {
    System.out.println("===========after throwing advice exception:" + exception);
}

最后在advice.xml配置文件中添加如下配置:

<aop:after-throwing pointcut="execution(* com.spring..*.sayAfterThrowing(..))"
                     method="afterThrowingAdvice"
                     arg-names="exception"
                     throwing="exception"/>

测试代码

public void testSchemaAfterReturningAdvice() {
    System.out.println("======================================");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/advice.xml");
    IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);
    helloworldService.sayAfterReturning();
    System.out.println("======================================");
}

分析一下吧:

1)切入点匹配:在配置中使用“execution(* com.spring..*.sayAfterThrowing(..))”匹配目标方法sayAfterThrowing,该方法将抛出RuntimeException异常;

2)目标方法定义:使用method="afterThrowingAdvice"指定后置异常通知实现方法;

3)目标方法参数命名:其中使用arg-names="exception"指定通知实现方法参数名为“exception”;

4)异常命名:returning="exception"用于将目标方法抛出的异常赋值给通知实现方法参数名为“exception”的参数上。

 

四、后置最终通知(After finally Advice): 在切入点选择的连接点处的方法返回时执行的通知,不管是正常返回还是抛出异常都执行,类似于Java中的finally块。通过<aop:aspect>标签下的<aop:after >标签声明:

<aop:after pointcut="切入点表达式" pointcut-ref="切入点Bean引用"
method="后置最终通知实现方法名"
arg-names="后置最终通知实现方法参数列表参数名字"/>

首先在IhelloWorldService接口中定义一个测试方法:

public boolean sayAfterFinally();

其次在HelloWorldService中实现

@Override
public boolean sayAfterFinally() {
    System.out.println("============before finally");
    throw new RuntimeException();
}

第三在 HelloWorldAspect定义通知实现:

public void afterFinallyAdvice() {
    System.out.println("===========after finally advice");
}

最后在/advice.xml配置文件中添加如下配置:

<aop:after pointcut="execution(* com.spring..*.sayAfterFinally(..))" 
          method="afterFinallyAdvice"/>

测试代码

public void testSchemaAfterFinallyAdvice() {
    System.out.println("======================================");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/advice.xml");
    IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);
    helloworldService.sayAfterFinally();
    System.out.println("======================================");
}

分析一下吧:

1)切入点匹配:在配置中使用“execution(* com.spring..*.sayAfterFinally(..))”匹配目标方法sayAfterFinally,该方法

将抛出RuntimeException异常;

2)目标方法定义:使用method=" afterFinallyAdvice "指定后置最终通知实现方法。

 

五、环绕通知(Around Advices):环绕着在切入点选择的连接点处的方法所执行的通知,环绕通知可以在方法调用之前和之后自定义任何行为,并且可以决定是否执行连接点处的方法、替换返回值、抛出异常等等。可通过<aop:aspect>标签下的<aop:around >标签声明:

<aop:around pointcut="切入点表达式" pointcut-ref="切入点Bean引用"
method="后置最终通知实现方法名"
arg-names="后置最终通知实现方法参数列表参数名字"/>

环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型,在通知实现方法内部使用

ProceedingJoinPoint的proceed()方法使目标方法执行,proceed 方法可以传入可选的Object[]数组,该数组的值将被作为目标方法执行时的参数。

首先在IhelloWorldService定义一个测试方法:

public void sayAround(String param);

其次在 HelloWorldService定义实现

@Override
public void sayAround(String param) {
    System.out.println("============around param:" + param);
}

第三在HelloWorldAspect定义通知实现:

public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("===========around before advice");
    Object retVal = pjp.proceed(new Object[]{"replace"});
    System.out.println("===========around after advice");
    return retVal;
}

最后在advice.xml配置文件中接着前置通知配置的例子添加如下配置:

<aop:around pointcut="execution(* com.spring..*.sayAround(..))" 
            method="aroundAdvice"/>

测试代码

public void testSchemaAroundAdvice() {
    System.out.println("======================================");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/advice.xml");
    IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);
    helloworldService.sayAround("haha");
    System.out.println("======================================");
}

分析一下吧:

1)切入点匹配:在配置中使用“execution(* cn.javass..*.sayAround(..))”匹配目标方法sayAround;

2)目标方法定义:使用method="aroundAdvice"指定环绕通知实现方法,在该实现中,第一个方法参数为pjp,类型为ProceedingJoinPoint,其中“Object retVal = pjp.proceed(new Object[] {"replace"});”,用于执行目标方法,且目标方法参数被“new Object[] {"replace"}”替换,最后返回“retVal ”返回值。

3)测试:我们使用“helloworldService.sayAround("haha");”传入参数为“haha”,但最终输出为“replace”,说明参数被替换了。

引入

Spring引入允许为目标对象引入新的接口,通过在< aop:aspect>标签内使用< aop:declare-parents>标签进行引入,定义方式如下:

<aop:declare-parents
types-matching="AspectJ语法类型表达式"
implement-interface=引入的接口"
default-impl="引入接口的默认实现"
delegate-ref="引入接口的默认实现Bean引用"/>
  • types-matching:匹配需要引入接口的目标对象的AspectJ语法类型表达式;
  • implement-interface:定义需要引入的接口;
  • default-impl和delegate-ref:定义引入接口的默认实现,二者选一,default-impl是接口的默认实现类全限定名,而delegate-ref是默认的实现的委托Bean名;

首先定义引入的接口及默认实现:

public interface IIntroductionService {   
    public void induct();  
}
public class IntroductiondService implements IIntroductionService {    
    @Override
    public void induct() {
        System.out.println("=========introduction");
    }
}

其次在advice.xml配置文件中添加如下配置:

<aop:declare-parents types-matching="com.spring..*.IHelloWorldService+" 
                     implement-interface="com.spring.aop.service.IIntroductionService"
                     default-impl="com.spring.aop.service.impl.IntroductiondService"/>

测试代码

public void testSchemaIntroduction() {
    System.out.println("======================================");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/advice.xml");
    IIntroductionService introductionService = ctx.getBean("helloWorldService", IIntroductionService.class);
    introductionService.induct();
    System.out.println("======================================");
}

分析一下吧:

1)目标对象类型匹配:使用types-matching="cn.javass..*.IHelloWorldService+"匹配IHelloWorldService接口的子类型,如HelloWorldService实现;

2)引入接口定义:通过implement-interface属性表示引入的接口,如

“com.spring.aop.service.IIntroductionService”。

3)引入接口的实现:通过default-impl属性指定,如“com.spring.aop.service.impl.IntroductiondService”,也可以使用“delegate-ref”来指定实现的Bean。

4)获取引入接口:如使用“ctx.getBean("helloWorldService", IIntroductionService.class);”可直接获取到引入的接口。

Advisor

Advisor表示只有一个通知和一个切入点的切面,由于Spring AOP都是基于AOP联盟的拦截器模型的环绕通知的,所以引入Advisor来支持各种通知类型(如前置通知等5种),Advisor概念来自于Spring1.2对AOP的支持,在AspectJ中没有相应的概念对应。

Advisor可以使用<aop:config>标签下的<aop:advisor>标签定义:

<aop:advisor pointcut="切入点表达式" pointcut-ref="切入点Bean引用"
advice-ref="通知API实现引用"/>
  • pointcut和pointcut-ref:二者选一,指定切入点表达式;
  • advice-ref:引用通知API实现Bean,如前置通知接口为MethodBeforeAdvice;

首先在IHelloWorldService定义一个测试方法:

public void sayAdvisorBefore(String param);

其次在HelloWorldService定义实现

public void sayAdvisorBefore(String param) {
    System.out.println("============say " + param);
}

第三定义前置通知API实现:

public class BeforeAdviceImpl implements MethodBeforeAdvice {
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println("===========before advice");
    }
    
}

在advice.xml配置文件中先添加通知实现Bean定义:

<bean id="beforeAdvice" class="com.spring.aop.service.impl.BeforeAdviceImpl"/>

然后在<aop:config>标签下,添加Advisor定义,添加时注意顺序:

<aop:advisor pointcut="execution(* com.spring..*.sayAdvisorBefore(..))" advice-ref="beforeAdvice"/>

测试代码

public void testSchemaAdvisor() {
    System.out.println("======================================");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/advice.xml");
    IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);
    helloworldService.sayAdvisorBefore("haha");
    System.out.println("======================================");
}

实际应用中不推荐使用Advisor,除了在进行事务控制的情况下,其他情况一般不推荐使用该方式,该方式属于侵入式设计,必须实现通知API。

 

参考:

http://sishuok.com/forum/blogPost/list/2467.html

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值