`
jinnianshilongnian
  • 浏览: 21433990 次
  • 性别: Icon_minigender_1
博客专栏
5c8dac6a-21dc-3466-8abb-057664ab39c7
跟我学spring3
浏览量:2404932
D659df3e-4ad7-3b12-8b9a-1e94abd75ac3
Spring杂谈
浏览量:2997590
43989fe4-8b6b-3109-aaec-379d27dd4090
跟开涛学SpringMVC...
浏览量:5631404
1df97887-a9e1-3328-b6da-091f51f886a1
Servlet3.1规范翻...
浏览量:257542
4f347843-a078-36c1-977f-797c7fc123fc
springmvc杂谈
浏览量:1593136
22722232-95c1-34f2-b8e1-d059493d3d98
hibernate杂谈
浏览量:248958
45b32b6f-7468-3077-be40-00a5853c9a48
跟我学Shiro
浏览量:5847466
Group-logo
跟我学Nginx+Lua开...
浏览量:698110
5041f67a-12b2-30ba-814d-b55f466529d5
亿级流量网站架构核心技术
浏览量:780420
社区版块
存档分类
最新评论

【第六章】 AOP 之 6.3 基于Schema的AOP ——跟我学spring3

阅读更多

6.3  基于Schema的AOP

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

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

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

 

6.3.1  声明切面

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

 

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

 

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

6.3.2  声明切入点

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

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

 

java代码:
  1. <aop:config>  
  2.  <aop:pointcut id="pointcut" expression="execution(* cn.javass..*.*(..))"/>  
  3.  <aop:aspect ref="aspectSupportBean">  
  4.     <aop:before pointcut-ref="pointcut" method="before"/>  
  5.  </aop:aspect>  
  6. </aop:config>  

 

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

 

java代码:
  1. <aop:config>  
  2.  <aop:aspect ref="aspectSupportBean">  
  3.     <aop:pointcut id=" pointcut" expression="execution(* cn.javass..*.*(..))"/>  
  4.     <aop:before pointcut-ref="pointcut" method="before"/>  
  5.  </aop:aspect>  
  6. </aop:config>  

 

 

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

 

java代码:
  1. <aop:config>  
  2.  <aop:aspect ref="aspectSupportBean">  
  3.      <aop:after pointcut="execution(* cn.javass..*.*(..))" method="afterFinallyAdvice"/>  
  4.  </aop:aspect>  
  5. </aop:config>  

 

6.3.3  声明通知

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

一、前置通知:在切入点选择的方法之前执行,通过<aop:aspect>标签下的<aop:before>标签声明:

 

java代码:
  1. <aop:before pointcut="切入点表达式"  pointcut-ref="切入点Bean引用"  
  2. method="前置通知实现方法名"  
  3. arg-names="前置通知实现方法参数列表参数名字"/>  

 

         pointcut和pointcut-ref二者选一,指定切入点;

         method指定前置通知实现方法名,如果是多态需要加上参数类型,多个用“,”隔开,如beforeAdvice(java.lang.String);

         arg-names指定通知实现方法的参数名字,多个用“,”分隔,可选,类似于【3.1.2 构造器注入】中的参数名注入限制:class文件中没生成变量调试信息是获取不到方法参数名字的,因此只有在类没生成变量调试信息时才需要使用arg-names属性来指定参数名,如arg-names="param"表示通知实现方法的参数列表的第一个参数名字为“param”。

 

首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法:

 

java代码:
  1. public void sayBefore(String param);  

 

其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现

 

java代码:
  1. @Override  
  2. public void sayBefore(String param) {  
  3.     System.out.println("============say " + param);  
  4. }  

 

 

第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现:

 

java代码:
  1. public void beforeAdvice(String param) {  
  2.     System.out.println("===========before advice param:" + param);  
  3. }  

 

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

 

java代码:
  1. <bean id="helloWorldService" class="cn.javass.spring.chapter6.service.impl.HelloWorldService"/>  
  2. <bean id="aspect" class="cn.javass.spring.chapter6.aop.HelloWorldAspect"/>  
  3. <aop:config>  
  4.     <aop:aspect ref="aspect">  
  5.         <aop:before pointcut="execution(* cn.javass..*.sayBefore(..)) and args(param)"   
  6.                            method="beforeAdvice(java.lang.String)"   
  7.                            arg-names="param"/>  
  8.     </aop:aspect>  
  9. </aop:config>  

 

 

测试代码cn.javass.spring.chapter6.AopTest:

 

java代码:
  1. @Test  
  2. public void testSchemaBeforeAdvice(){  
  3.      System.out.println("======================================");  
  4.      ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");  
  5.      IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);  
  6.      helloworldService.sayBefore("before");  
  7.     System.out.println("======================================");  
  8. }  

 

将输入:

 
 

==========================================

===========before advice param:before

============say before

==========================================

 

 

 

 

 

 

 

 

 

分析一下吧:

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

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

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

 

 

二、后置返回通知:在切入点选择的方法正常返回时执行,通过<aop:aspect>标签下的<aop:after-returning>标签声明:

 

java代码:
  1. <aop:after-returning pointcut="切入点表达式"  pointcut-ref="切入点Bean引用"  
  2.     method="后置返回通知实现方法名"  
  3.     arg-names="后置返回通知实现方法参数列表参数名字"  
  4.     returning="返回值对应的后置返回通知实现方法参数名"  
  5. />  

 

         pointcut和pointcut-ref同前置通知同义;

         method同前置通知同义;

         arg-names同前置通知同义;

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

 

首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法:

 

java代码:
  1. public boolean sayAfterReturning();  
  2.    

 

 

其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现

 

java代码:
  1. @Override  
  2. public boolean sayAfterReturning() {  
  3.     System.out.println("============after returning");  
  4.     return true;  
  5. }  

 

第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现:

 

java代码:
  1. public void afterReturningAdvice(Object retVal) {  
  2.     System.out.println("===========after returning advice retVal:" + retVal);  
  3. }  

 

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

 

java代码:
  1. <aop:after-returning pointcut="execution(* cn.javass..*.sayAfterReturning(..))"  
  2.                                 method="afterReturningAdvice"  
  3.                                arg-names="retVal"    
  4.                                returning="retVal"/>  

 

 

测试代码cn.javass.spring.chapter6.AopTest:

 

java代码:
  1. @Test  
  2. public void testSchemaAfterReturningAdvice() {  
  3.     System.out.println("======================================");  
  4.     ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");  
  5.     IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);  
  6.     helloworldService.sayAfterReturning();      
  7.     System.out.println("======================================");  
  8. }  

 

将输入:

 
 

======================================

============after returning

===========after returning advice retVal:true

======================================

 

 

 

 

 

 

 

 

 

分析一下吧:

1)切入点匹配:在配置中使用“execution(* cn.javass..*.sayAfterReturning(..)) ”匹配目标方法sayAfterReturning,该方法返回true;

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

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

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

 

 

三、后置异常通知:在切入点选择的方法抛出异常时执行,通过<aop:aspect>标签下的<aop:after-throwing>标签声明:

 

java代码:
  1. <aop:after-throwing pointcut="切入点表达式"  pointcut-ref="切入点Bean引用"  
  2.                                 method="后置异常通知实现方法名"  
  3.                                 arg-names="后置异常通知实现方法参数列表参数名字"  
  4.                                 throwing="将抛出的异常赋值给的通知实现方法参数名"/>  

 

         pointcut和pointcut-ref同前置通知同义;

         method同前置通知同义;

         arg-names同前置通知同义;

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

 

首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法:

 

java代码:
  1. public void sayAfterThrowing();  
  2.    

 

其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现

 

java代码:
  1. @Override  
  2. public void sayAfterThrowing() {  
  3.     System.out.println("============before throwing");  
  4.     throw new RuntimeException();  
  5. }  

 

第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现:

 

java代码:
  1. public void afterThrowingAdvice(Exception exception) {  
  2.   System.out.println("===========after throwing advice exception:" + exception);  
  3. }  

 

 

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

 

java代码:
  1. <aop:after-throwing pointcut="execution(* cn.javass..*.sayAfterThrowing(..))"  
  2.                                 method="afterThrowingAdvice"  
  3.                                 arg-names="exception"  
  4.                                 throwing="exception"/>  

 

测试代码cn.javass.spring.chapter6.AopTest:

 

 

java代码:
  1. @Test(expected = RuntimeException.class)  
  2. public void testSchemaAfterThrowingAdvice() {  
  3.     System.out.println("======================================");  
  4.     ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");  
  5.     IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);  
  6.     helloworldService.sayAfterThrowing();  
  7.     System.out.println("======================================");  
  8. }  

将输入:

 
 

======================================

============before throwing

===========after throwing advice exception:java.lang.RuntimeException

======================================

 

 

 

 

 

 

 

 

 

 

分析一下吧:

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

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

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

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

 

 

四、后置最终通知:在切入点选择的方法返回时执行,不管是正常返回还是抛出异常都执行,通过<aop:aspect>标签下的<aop:after >标签声明:

 

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

 

         pointcut和pointcut-ref同前置通知同义;

         method同前置通知同义;

         arg-names同前置通知同义;

 

首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法:

 

java代码:
  1. public boolean sayAfterFinally();  

 

其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现

 

java代码:
  1. @Override  
  2. public boolean sayAfterFinally() {  
  3.         System.out.println("============before finally");  
  4.         throw new RuntimeException();  
  5. }  

 

第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现:

 

java代码:
  1. public void afterFinallyAdvice() {  
  2.         System.out.println("===========after finally advice");  
  3. }  

 

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

 

java代码:
  1. <aop:after pointcut="execution(* cn.javass..*.sayAfterFinally(..))"  
  2.          method="afterFinallyAdvice"/>  

 

 

测试代码cn.javass.spring.chapter6.AopTest:

 

java代码:
  1. @Test(expected = RuntimeException.class)  
  2. public void testSchemaAfterFinallyAdvice() {  
  3.     System.out.println("======================================");  
  4.     ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");  
  5.     IHelloWorldService helloworldService = ctx.getBean("helloWorldService", IHelloWorldService.class);  
  6.     helloworldService.sayAfterFinally();  
  7.     System.out.println("======================================");  
  8. }  

 

将输入:

 
 

======================================

============before finally

===========after finally advice

======================================

 

 

 

 

 

 

 

 

 

 

分析一下吧:

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

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

 

 

五、环绕通知:环绕着在切入点选择的连接点处的方法所执行的通知,环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值,可通过<aop:aspect>标签下的<aop:around >标签声明:

 

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

 

         pointcut和pointcut-ref同前置通知同义;

         method同前置通知同义;

         arg-names同前置通知同义;

 

环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型,在通知实现方法内部使用ProceedingJoinPoint的proceed()方法使目标方法执行,proceed 方法可以传入可选的Object[]数组,该数组的值将被作为目标方法执行时的参数。

 

首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法:

 

java代码:
  1. public void sayAround(String param);  

 

其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现

 

java代码:
  1. @Override  
  2. public void sayAround(String param) {  
  3.    System.out.println("============around param:" + param);  
  4. }  

 

第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现:

 

java代码:
  1. public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {  
  2.     System.out.println("===========around before advice");  
  3.     Object retVal = pjp.proceed(new Object[] {"replace"});  
  4.     System.out.println("===========around after advice");  
  5.     return retVal;  
  6. }  

 

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

 

java代码:
  1. <aop:around pointcut="execution(* cn.javass..*.sayAround(..))"  
  2.            method="aroundAdvice"/>  

 

 

测试代码cn.javass.spring.chapter6.AopTest:

 

java代码:
  1. @Test  
  2. public void testSchemaAroundAdvice() {  
  3.     System.out.println("======================================");  
  4.     ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");  
  5.     IHelloWorldService helloworldService =  
  6.     ctx.getBean("helloWorldService", IHelloWorldService.class);  
  7.     helloworldService.sayAround("haha");  
  8.     System.out.println("======================================");  
  9. }  

 

将输入:

 
 

======================================

===========around before advice

============around param:replace

===========around after advice

======================================

 

 

 

 

 

 

 

 

 

 

 

分析一下吧:

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”,说明参数被替换了。

 

6.3.4  引入

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

 

java代码:
  1. <aop:declare-parents  
  2.           types-matching="AspectJ语法类型表达式"  
  3.           implement-interface=引入的接口"               
  4.           default-impl="引入接口的默认实现"  
  5.           delegate-ref="引入接口的默认实现Bean引用"/>  

 

         types-matching匹配需要引入接口的目标对象的AspectJ语法类型表达式;

         implement-interface定义需要引入的接口;

         default-impl和delegate-ref定义引入接口的默认实现,二者选一,default-impl是接口的默认实现类全限定名,而delegate-ref是默认的实现的委托Bean名;

 

接下来让我们练习一下吧:

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

 

java代码:
  1. package cn.javass.spring.chapter6.service;  
  2. public interface IIntroductionService {  
  3.     public void induct();  
  4. }  

 

 

java代码:
  1. package cn.javass.spring.chapter6.service.impl;  
  2. import cn.javass.spring.chapter6.service.IIntroductionService;  
  3. public class IntroductiondService implements IIntroductionService {  
  4.     @Override  
  5.     public void induct() {  
  6.         System.out.println("=========introduction");  
  7.     }  
  8. }  

 

 

其次在chapter6/advice.xml配置文件中接着前置通知配置的例子添加如下配置:

 

java代码:
  1. <aop:declare-parents  
  2.     types-matching="cn.javass..*.IHelloWorldService+"  
  3.     implement-interface="cn.javass.spring.chapter6.service.IIntroductionService"                           
  4.     default-impl="cn.javass.spring.chapter6.service.impl.IntroductiondService"/>  
  5.    

 

最后测试一下吧,测试代码cn.javass.spring.chapter6.AopTest:

 

java代码:
  1. @Test  
  2. public void testSchemaIntroduction() {  
  3.     System.out.println("======================================");  
  4.     ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");  
  5.     IIntroductionService introductionService =  
  6.     ctx.getBean("helloWorldService", IIntroductionService.class);  
  7.     introductionService.induct();  
  8.     System.out.println("======================================");  
  9. }  
  10.    

 

 

将输入:

 
 

======================================

=========introduction

======================================

 

 

 

 

 

 

 

 

分析一下吧:

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

2)引入接口定义:通过implement-interface属性表示引入的接口,如“cn.javass.spring.chapter6.service.IIntroductionService”。

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

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

 

6.3.5 Advisor

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

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

 

java代码:
  1. <aop:advisor pointcut="切入点表达式" pointcut-ref="切入点Bean引用"  
  2.                      advice-ref="通知API实现引用"/>  

 

         pointcut和pointcut-ref二者选一,指定切入点表达式;

         advice-ref引用通知API实现Bean,如前置通知接口为MethodBeforeAdvice;

 

接下来让我们看一下示例吧:

首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法:

 

java代码:
  1. public void sayAdvisorBefore(String param);  

 

其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现

 

java代码:
  1. @Override  
  2. public void sayAdvisorBefore(String param) {  
  3.     System.out.println("============say " + param);  
  4. }  

 

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

 

java代码:
  1. package cn.javass.spring.chapter6.aop;  
  2. import java.lang.reflect.Method;  
  3. import org.springframework.aop.MethodBeforeAdvice;  
  4. public class BeforeAdviceImpl implements MethodBeforeAdvice {  
  5.     @Override  
  6.     public void before(Method method, Object[] args, Object target) throws Throwable {  
  7.         System.out.println("===========before advice");  
  8.     }  
  9. }  

 

 

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

 

java代码:
  1.       
  2. <bean id="beforeAdvice" class="cn.javass.spring.chapter6.aop.BeforeAdviceImpl"/>  
  3.    

 

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

 

java代码:
  1. <aop:advisor pointcut="execution(* cn.javass..*.sayAdvisorBefore(..))"  
  2.                      advice-ref="beforeAdvice"/>  
  3.    

 

测试代码cn.javass.spring.chapter6.AopTest:

 

java代码:
  1. @Test  
  2. public void testSchemaAdvisor() {  
  3.    System.out.println("======================================");  
  4.    ApplicationContext ctx = new ClassPathXmlApplicationContext("chapter6/advice.xml");  
  5.    IHelloWorldService helloworldService =  
  6.    ctx.getBean("helloWorldService", IHelloWorldService.class);  
  7.    helloworldService.sayAdvisorBefore("haha");  
  8.    System.out.println("======================================");  
  9. }   

 

将输入:

 
 

======================================

===========before advice

============say haha

======================================

 

 

 

 

 

 

 

 

 

 

在此我们只介绍了前置通知API,其他类型的在后边章节介绍。

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

 

29
3
分享到:
评论
29 楼 snfdf 2017-09-22  
引用

method:指定前置通知实现方法名,如果是多态需要加上参数类型,多个用“,”隔开,如beforeAdvice(java.lang.String);

请问这里是不是写错了?是多态还是重载?
28 楼 一月十年 2017-03-01  
afterThrowing遇到问题,和您的输出结果有冲突

我的测试类会抛出异常,后面的代码并不会执行
27 楼 hx252502115 2016-08-10  
newboy2004 写道
那个引入的作用是啥?使用场景呢?它是为了扩展那个IHelloworldService这个接口吗?


例如权限控制时,我可以通过动态引入接口的方式为某个用户或者角色赋予一个业务接口
26 楼 wubingyang527 2016-06-30  
切入点中使用“args(param)”匹配的目标方法参数将自动传递给通知实现方法同名参数。

-----这句话好难断
25 楼 newboy2004 2015-11-20  
那个引入的作用是啥?使用场景呢?它是为了扩展那个IHelloworldService这个接口吗?
24 楼 魔落凡尘 2015-10-23  
bridge_ca 写道
不知道为什么前置通知的方法有参数,后置通知没有参数启动就报错呢
<aop:config proxy-target-class="true">
		<aop:pointcut expression="execution(* com.test..*.sayHello(..)) and args(param)" id="pointcut"/>
		<aop:aspect ref="aspect">
			<aop:before method="beforeAdvice(java.lang.String)" arg-names="param" pointcut-ref="pointcut"/>
			<aop:after method="afterFinallyAdvice" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#1': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: param [Xlint:invalidAbsoluteTypeName]



你的两个切面用了同一个切入点,但是只有一个需要参数,after的那个不需要,所以会报错。你把after的规则改成不要 and args的就可以了
23 楼 bridge_ca 2015-04-01  
不知道为什么前置通知的方法有参数,后置通知没有参数启动就报错呢
<aop:config proxy-target-class="true">
		<aop:pointcut expression="execution(* com.test..*.sayHello(..)) and args(param)" id="pointcut"/>
		<aop:aspect ref="aspect">
			<aop:before method="beforeAdvice(java.lang.String)" arg-names="param" pointcut-ref="pointcut"/>
			<aop:after method="afterFinallyAdvice" pointcut-ref="pointcut"/>
		</aop:aspect>
	</aop:config>

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.aop.aspectj.AspectJPointcutAdvisor#1': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.aop.aspectj.AspectJPointcutAdvisor]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: param [Xlint:invalidAbsoluteTypeName]
22 楼 agong 2014-08-27  
agong 写道
请教下, 如果在这个切面类里,比如您这儿的BeforeAdviceImpl中放一个:
@Resource
XXDao xxDao;

这样可以吗, 我尝试中, 总是报错:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxDao' must be of type [xxx.xxx.XXDao], but was actually of type [com.sun.proxy.$Proxy36]

<aop:config proxy-target-class="true">

即使这样写了也没用
21 楼 agong 2014-08-27  
请教下, 如果在这个切面类里,比如您这儿的BeforeAdviceImpl中放一个:
@Resource
XXDao xxDao;

这样可以吗, 我尝试中, 总是报错:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'xxDao' must be of type [xxx.xxx.XXDao], but was actually of type [com.sun.proxy.$Proxy36]

即使这样写了也没用
20 楼 zhuyucheng123 2013-12-09  
ForgiDaved 写道
  楼主你好,我在试验6.3.4  引入的过程中抛出了这样的异常,反复检查了好几遍都没有找到问题,我是根据你的步骤一步步试验的,异常如下:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'helloworldService' must be of type [com.javass.spring.service.IIntroductionService], but was actually of type [com.javass.spring.service.impl.HelloWorldService]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:347)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1008)
	at com.javass.spring.AopTest.testAop(AopTest.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)



写成这样就好了
	<aop:config proxy-target-class="true">
19 楼 jinnianshilongnian 2012-11-21  
w5pand 写道
有个地方不明白,请问楼主:
xml中arg-names:和 throwing:
这两个参数名一定要一样么?在通知里的具体方法里参数名可以随意指定啊?


arg-names:指定通知实现方法的参数名字,多个用“,”分隔,可选,类似于【3.1.2 构造器注入】中的参数名注入限制:在class文件中没生成变量调试信息是获取不到方法参数名字的,因此只有在类没生成变量调试信息时才需要使用arg-names属性来指定参数名,如arg-names="param"表示通知实现方法的参数列表的第一个参数名字为“param”。

去看下3.1.2
18 楼 w5pand 2012-11-21  
有个地方不明白,请问楼主:
xml中arg-names:和 throwing:
这两个参数名一定要一样么?在通知里的具体方法里参数名可以随意指定啊?
17 楼 jinnianshilongnian 2012-09-25  
lsjinpeng 写道
好长...看了一上午,看好有例子,要不非折腾几天
 

呵呵
16 楼 lsjinpeng 2012-09-25  
好长...看了一上午,看好有例子,要不非折腾几天
 
15 楼 jinnianshilongnian 2012-05-30  
ForgiDaved 写道
楼主,你好,6.3.4  引入的那个异常,我通过修改配置文件将types-matching指向具体的实现类,而不是您示例中的接口,运行则通过了,配置如下:
<aop:declare-parents 
    types-matching="cn.javass..*.HelloWorldService" 
    implement-interface="cn.javass.spring.chapter6.service.IIntroductionService"                          
    default-impl="cn.javass.spring.chapter6.service.impl.IntroductiondService"/>
  请解释下这个的原因。


types-matching="cn.javass..*.IHelloWorldService+" 我是用的这个通配符  + 表示子类

即要使用实现类
14 楼 ForgiDaved 2012-05-30  
楼主,你好,6.3.4  引入的那个异常,我通过修改配置文件将types-matching指向具体的实现类,而不是您示例中的接口,运行则通过了,配置如下:
<aop:declare-parents 
    types-matching="cn.javass..*.HelloWorldService" 
    implement-interface="cn.javass.spring.chapter6.service.IIntroductionService"                          
    default-impl="cn.javass.spring.chapter6.service.impl.IntroductiondService"/>
  请解释下这个的原因。
13 楼 ForgiDaved 2012-05-30  
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'helloworldService' must be of type
[com.javass.spring.service.IIntroductionService], but was actually of type [com.javass.spring.service.impl.HelloWorldService]
  soryy,很少发评论
12 楼 ForgiDaved 2012-05-30  
ForgiDaved 写道
  楼主你好,我在试验6.3.4  引入的过程中抛出了这样的异常,反复检查了好几遍都没有找到问题,我是根据你的步骤一步步试验的,异常如下:
[align=left]
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'helloworldService' must be of type [com.javass.spring.service.IIntroductionService], but was actually of type [com.javass.spring.service.impl.HelloWorldService]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:347)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1008)
	at com.javass.spring.AopTest.testAop(AopTest.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


[/align]

11 楼 ForgiDaved 2012-05-30  
  楼主你好,我在试验6.3.4  引入的过程中抛出了这样的异常,反复检查了好几遍都没有找到问题,我是根据你的步骤一步步试验的,异常如下:
org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'helloworldService' must be of type [com.javass.spring.service.IIntroductionService], but was actually of type [com.javass.spring.service.impl.HelloWorldService]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:347)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1008)
	at com.javass.spring.AopTest.testAop(AopTest.java:28)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
	at java.lang.reflect.Method.invoke(Method.java:597)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
	at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
	at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


10 楼 jinnianshilongnian 2012-05-28  
samuel0818 写道
学了一天了,非常好...支持.

谢谢支持

相关推荐

Global site tag (gtag.js) - Google Analytics