您的位置:首页 > 编程语言 > Java开发

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-------");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: