Spring -- 基于XML的AOP通知配置

上一篇博客,我们学习了基于注解配置AOP。

下面我们基于XML来配置AOP。

看代码

public interface Calculation {

    public int add(int x, int y);

    public int sub(int x, int y);

    public int mul(int x, int y);

    public int dev(int x, int y);
}
public class CalculationImpl implements Calculation {

    @Override
    public int add(int x, int y) {
        int result = x + y;
        System.out.println("executeing ...");
        return result;
    }

    @Override
    public int sub(int x, int y) {
        int result = x - y;
        return result;
    }

    @Override
    public int mul(int x, int y) {
        int result = x * y;
        return result;
    }

    @Override
    public int dev(int x, int y) {
        int result = x / y;
        System.out.println("executeing ...");
        return result;
    }

}
public class CalculationAspect {

    public void afterReturnMethod(JoinPoint joinPoint, Object ret) {
        String name = joinPoint.getSignature().getName();
        List<Object> list = Arrays.asList(joinPoint.getArgs());
        System.out.println("@AfterReturning ... ,method=" + name + ", args = "
                + list + ", return = " + ret);
    }

    public Object aroundMethod(ProceedingJoinPoint pjd) {
        String name = pjd.getSignature().getName();
        List<Object> list = Arrays.asList(pjd.getArgs());
        Object obj = null;

        System.out.println("前置通知 ... ,method=" + name + ", args = "
                + list);
        try {
            obj = pjd.proceed();
            System.out.println("返回通知 ... ,method=" + name + ", args = "
                    + list);
        } catch (Throwable e) {
            System.out.println("异常通知 ... , exception = " + e);
            e.printStackTrace();
        }
        System.out.println("后置通知 ... ,method=" + name + ", args = "
                + list);
        return obj;
    } 
}

第一个切入方法,我们其中有后置返回通知、环绕通知两个方法。

public class ValidateAspect {

    public void validate(){
        System.out.println("验证方法 com.gp.spring.aop.impl.ValidateAspect");
    }
}

第二个切入方法。

public class ValidateAspect {

    public void validate(){
        System.out.println("验证方法 com.gp.spring.aop.impl.ValidateAspect");
    }
}
<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"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd">

    <bean id="calculationImpl" class="com.gp.spring.aop.impl.xml.CalculationImpl"></bean>

    <bean id="calculationAspect" class="com.gp.spring.aop.impl.xml.CalculationAspect"></bean>

    <bean id="validateAspect" class="com.gp.spring.aop.impl.xml.ValidateAspect"></bean>

    <aop:config>
        <aop:pointcut
            expression="execution(public int com.gp.spring.aop.impl.xml.Calculation.*(int, int))"
            id="pointcut" />

        <aop:aspect ref="calculationAspect" order="1">
            <aop:after-returning method="afterReturnMethod"
                pointcut-ref="pointcut" returning="ret" />
            <aop:around method="aroundMethod" pointcut-ref="pointcut" />
        </aop:aspect>

        <aop:aspect ref="validateAspect" order="2">
            <aop:before method="validate" pointcut-ref="pointcut" />
        </aop:aspect>

    </aop:config>
</beans>

核心的东西来了
这里我们用XML配置代理了注解
这里写图片描述

执行测试方法

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-xml.xml");
        Calculation calculation = (Calculation)context.getBean("calculationImpl");
        int result = calculation.add(1, 3);
        System.out.println(result);
    }

输出结果

前置通知 … ,method=add, args = [1, 3]
验证方法 com.gp.spring.aop.impl.ValidateAspect
executeing …
@AfterReturning … ,method=add, args = [1, 3], return = 4
返回通知 … ,method=add, args = [1, 3]
后置通知 … ,method=add, args = [1, 3]
4

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值