JavaWeb学习笔记-spring-17-AOP-基于schema配置切面

基于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-------");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值