JavaWeb学习笔记-spring-17-AOP-基于schema配置切面
2018-02-11 14:57
826 查看
基于schema配置切面
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <aop:before pointcut="target(com.smart.advice.NativeWaiter)and execution(* greetTo(..))" method="preGreeting"></aop:before> </aop:aspect> </aop:config> <bean id="adviceMethods" class="com.smart.schema.AdviceMethod"/> <bean id="naiveWaiter" class="com.smart.advice.NativeWaiter"/> </beans>
public class AdviceMethod { public void preGreeting(){ System.out.println("---how are you ---"); } }
配置切点命名
pointcut在aspect内部,只能被当前aspect使用
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <aop:pointcut id="greetToPointcut" expression="target(com.smart.advice.NativeWaiter)and execution(* greetTo(..))"/> <aop:before method="preGreeting" pointcut-ref="greetToPointcut"/> </aop:aspect> </aop:config>
在config下定义,能被所有ascpet使用
<aop:config proxy-target-class="true"> <aop:pointcut id="greetToPointcut" expression="target(com.smart.advice.NativeWaiter)and execution(* greetTo(..))"/> <aop:aspect ref="adviceMethods"> <aop:before method="preGreeting" pointcut-ref="greetToPointcut"/> </aop:aspect> </aop:config>
config下必须先定义pointcut,再定义advisor,最后aspect
各种增强类型的配置
后置增强
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <aop:after-returning pointcut="target(com.smart.advice.Seller)" method="afterReturning" returning="retval"></aop:before> </aop:aspect> </aop:config>
public class AdviceMethods{ public void afterReturning(int retval){} }
环绕增强
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <aop:around method="aroundMethod" pointcut="execution(* serveTo(..))and within(com.smart.advice.Waiter))"/> </aop:aspect> </aop:config>
//环绕增强方法,pjp可以访问到环绕增强的连接点信息 public void aroundMethod(ProceedingJoinPoint pjp){ }
抛出异常增强
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <!-- 通过iae查找增强方法对应名字的参数,进而获取需要增强的连接点的匹配异常类型为IllegalArgumentException--> <aop:after-throwing method="afterThrowingMethod" pointcut="target(com.smart.advice.NativeWaiter)and execution(* greetTo(..))" throwing="iae"/> </aop:aspect> </aop:config>
public void afterThrowingMethod(IllegalArgumentException iae){ }
Final增强
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <aop:after method="afterMethod" pointcut="execution(* com..*.Waiter.greetTo(..))"/> </aop:aspect> </aop:config>
public void afterMethod(){}
引介增强
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <!-- implement-interface声明要实现的接口--> <!-- default-impl指定默认的接口实现类--> <!-- types-matching指定需要引介接口Seller的实现--> <aop:declare-parents implement-interface="com.smart.schema.Seller" default-impl="com.smart.schema.SmartSeller" types-matching="com.smart.schema.Waiter+"/> </aop:aspect> </aop:config>
ApplicationContext ctx = new ClassPathXmlApplicationContext(configPath); Waiter naiveWaiter = (Waiter)ctx.getBean("naiveWaiter"); ((Seller)naiveWaiter).sell("Beer","John");
绑定连接点信息
所有增强类型对应方法的第一个参数都可以声明为JohnPoint(环绕增强可声明ProceedingJoinPoint),访问连接点
,可通过returning属性绑定连接点方法返回值,可通过throwing属性绑定连接点方法抛出的异常
所有增强类型可以通过绑定参数的切点函数绑定连接点方法的参数
<aop:config proxy-target-class="true"> <aop:aspect ref="adviceMethods"> <aop:before pointcut="target(com.smart.advice.NativeWaiter)and args(name,num,..)" method="bindParams"></aop:before> </aop:aspect> </aop:config>
public class AdviceMethods{ public void bindParams(int num,String name){ System.out.println("-----bindParams()-------"); System.out.println("name:"+name); System.out.println("num:"+num); System.out.println("-----bindParmas()-------"); } }
Advisor配置
<aop:config proxy-target-class="true"> <aop:advisor advice-ref="testAdvice" pointcut="execution(* com..*.Waiter.greetTo(..))"/> </aop:config>
public class TestBeforeAdvice implements MethodBeforeAdvice{ public void before(Method method,Object[] args,Object target)throws Throwable{ System.out.println("------TestBeforeAdvice------"); System.out.println("clientName:"+args[0]); System.out.println("---------TestBeforeAdvice-------"); } }
相关文章推荐
- spring学习笔记(13)基于Schema配置AOP详解
- spring学习笔记(23)基于tx/aop配置切面增强事务
- JavaWeb学习笔记-spring-14-AOP-创建切面(静态正则表达式方法匹配切面)
- JavaWeb学习笔记-spring-06-ioc-基于注解配置
- JavaWeb学习笔记-spring-13-AOP-创建切面(静态普通方法名匹配切面)
- Spring学习(19)--- Schema-based AOP(基于配置的AOP实现) --- 配置切面aspect
- JavaWeb学习笔记-spring-07-ioc-基于java类的配置
- 【Spring AOP】基于Schema配置切面
- spring学习笔记(22)----基于配置文件的方式来配置AOP
- Spring4学习笔记-AOP(基于配置文件的方式)
- Spring学习4-面向切面(AOP)之schema配置方式
- Spring4学习笔记-AOP(基于配置文件的方式)
- Spring笔记之六:AOP基于@AspectJ配置切面
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
- 20.Spring学习笔记_基于配置文件的方式来配置 AOP(by尚硅谷_佟刚)
- Spring学习4-面向切面(AOP)之schema配置方式
- 【Spring AOP】基于Schema配置切面
- Spring-AOP 基于Schema配置切面
- Spring4 学习笔记(3)-Spring 基于 XML 的方式配置 Bean(供自己学习)
- Spring MVC 学习笔记(一) 基于spring2.5的纯xml配置