【第六章】 AOP 之 6.3 基于Schema的AOP ——跟小弟我学spring3
基于Schema的AOP从Spring2.0之后通过“aop”命名空间来定义切面、切入点及声明通知。 在Spring配置文件中,所以AOP相关定义必须放在<aop:config>标签下,该标签下可以有<aop:pointcut>、<aop:advisor>、<aop:aspect>标签,配置顺序不可变。 6.3.1 声明切面 切面就是包含切入点和通知的对象,在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属性指定切入点表达式: 2)在<aop:aspect>标签下使用<aop:pointcut>声明一个切入点Bean,该切入点可以被多个切面使用,但一般该切入点只被该切面使用,当然也可以被其他切面使用,但最好不要那样使用,该切入点使用id属性指定Bean名字,在通知定义时使用pointcut-ref属性通过该id引用切入点,expression属性指定切入点表达式: 3)匿名切入点Bean,可以在声明通知时通过pointcut属性指定切入点表达式,该切入点是匿名切入点,只被该通知使用: 基于Schema方式支持前边介绍的5中通知类型: 一、前置通知:在切入点选择的方法之前执行,通过<aop:aspect>标签下的<aop:before>标签声明: 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定义一个测试方法: 其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现 第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现: 最后在chapter6/advice.xml配置文件中进行如下配置: 测试代码cn.javass.spring.chapter6.AopTest: 将输入: ========================================== ===========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>标签声明: pointcut和pointcut-ref:同前置通知同义; method:同前置通知同义; arg-names:同前置通知同义; returning:定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法执行正常返回后,将把目标方法返回值传给通知方法;returning限定了只有目标方法返回值匹配与通知方法相应参数类型时才能执行后置返回通知,否则不执行,对于returning对应的通知方法参数为Object类型将匹配任何目标返回值。 首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法: 其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现 第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现: 最后在chapter6/advice.xml配置文件中接着前置通知配置的例子添加如下配置: 测试代码cn.javass.spring.chapter6.AopTest: 将输入: ====================================== ============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>标签声明: pointcut和pointcut-ref:同前置通知同义; method:同前置通知同义; arg-names:同前置通知同义; throwing:定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法抛出异常返回后,将把目标方法抛出的异常传给通知方法;throwing限定了只有目标方法抛出的异常匹配与通知方法相应参数异常类型时才能执行后置异常通知,否则不执行,对于throwing对应的通知方法参数为Throwable类型将匹配任何异常。 首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法: 其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现 第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现: 最后在chapter6/advice.xml配置文件中接着前置通知配置的例子添加如下配置: 测试代码cn.javass.spring.chapter6.AopTest: 将输入: ====================================== ============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 >标签声明: pointcut和pointcut-ref:同前置通知同义; method:同前置通知同义; arg-names:同前置通知同义; 首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法: 其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现 第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现: 最后在chapter6/advice.xml配置文件中接着前置通知配置的例子添加如下配置: 测试代码cn.javass.spring.chapter6.AopTest: 将输入: ====================================== ============before finally ===========after finally advice ====================================== 分析一下吧: 1)切入点匹配:在配置中使用“execution(* cn.javass..*.sayAfterFinally(..))”匹配目标方法sayAfterFinally,该方法将抛出RuntimeException异常; 2)目标方法定义:使用method=" afterFinallyAdvice "指定后置最终通知实现方法。 五、环绕通知:环绕着在切入点选择的连接点处的方法所执行的通知,环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值,可通过<aop:aspect>标签下的<aop:around >标签声明: pointcut和pointcut-ref:同前置通知同义; method:同前置通知同义; arg-names:同前置通知同义; 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型,在通知实现方法内部使用ProceedingJoinPoint的proceed()方法使目标方法执行,proceed 方法可以传入可选的Object[]数组,该数组的值将被作为目标方法执行时的参数。 首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法: 其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现 第三在cn.javass.spring.chapter6.aop. HelloWorldAspect定义通知实现: 最后在chapter6/advice.xml配置文件中接着前置通知配置的例子添加如下配置: 测试代码cn.javass.spring.chapter6.AopTest: 将输入: ====================================== ===========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”,说明参数被替换了。 Spring引入允许为目标对象引入新的接口,通过在< aop:aspect>标签内使用< aop:declare-parents>标签进行引入,定义方式如下: types-matching:匹配需要引入接口的目标对象的AspectJ语法类型表达式; implement-interface:定义需要引入的接口; default-impl和delegate-ref:定义引入接口的默认实现,二者选一,default-impl是接口的默认实现类全限定名,而delegate-ref是默认的实现的委托Bean名; 接下来让我们练习一下吧: 首先定义引入的接口及默认实现: 其次在chapter6/advice.xml配置文件中接着前置通知配置的例子添加如下配置: 最后测试一下吧,测试代码cn.javass.spring.chapter6.AopTest: 将输入: ====================================== =========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);”可直接获取到引入的接口。 Advisor表示只有一个通知和一个切入点的切面,由于Spring AOP都是基于AOP联盟的拦截器模型的环绕通知的,所以引入Advisor来支持各种通知类型(如前置通知等5种),Advisor概念来自于Spring1.2对AOP的支持,在AspectJ中没有相应的概念对应。 Advisor可以使用<aop:config>标签下的<aop:advisor>标签定义: pointcut和pointcut-ref:二者选一,指定切入点表达式; advice-ref:引用通知API实现Bean,如前置通知接口为MethodBeforeAdvice; 接下来让我们看一下示例吧: 首先在cn.javass.spring.chapter6.service.IhelloWorldService定义一个测试方法: 其次在cn.javass.spring.chapter6.service.impl. HelloWorldService定义实现 第三定义前置通知API实现: 在chapter6/advice.xml配置文件中先添加通知实现Bean定义: 然后在<aop:config>标签下,添加Advisor定义,添加时注意顺序: 测试代码cn.javass.spring.chapter6.AopTest: 将输入: ====================================== ===========before advice ============say haha ====================================== 在此我们只介绍了前置通知API,其他类型的在后边章节介绍。 不推荐使用Advisor,除了在进行事务控制的情况下,其他情况一般不推荐使用该方式,该方式属于侵入式设计,必须实现通知API。 6.3 基于Schema的AOP
6.3.2 声明切入点
6.3.3 声明通知
6.3.4 引入
6.3.5 Advisor
谢谢支持
谢谢
写的很好~辛苦了~
写的很好~辛苦了~
谢谢
fei chang qiang da
谢谢支持
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=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]
[com.javass.spring.service.IIntroductionService], but was actually of type [com.javass.spring.service.impl.HelloWorldService]
soryy,很少发评论
<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"/>
请解释下这个的原因。
<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+" 我是用的这个通配符 + 表示子类
即要使用实现类