SpringAOP-xml配置AOP-纯xml配置AOP

实体类如下

Calculation是我们用的类,PointCut是我们要实现通知的切面类

Calculation类如下

package com.llf.aop.xml;

/**
 * @author linglongfang
 */
public class Calculation {

    public int add(int i,int j){
        System.out.println("执行add");
        return i+j;
    }

    public int sub(int i,int j){
        System.out.println("执行sub");
        return i-j;
    }

    public int mul(int i,int j){
        System.out.println("执行mul");
        return i*j;
    }

    public int div(int i,int j){
        System.out.println("执行div");
        return i/j;
    }
}

PointCut类如下

package com.llf.aop.xml;

import org.aspectj.lang.JoinPoint;
/**
 * @author linglongfang
 */
public class PointCut {

    public void before(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        System.out.println("执行方法前:"+methodName+"参数为"+args);
    }

    public void after(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法后:"+methodName);
    }

    public void afterReturning(JoinPoint joinPoint,Object result){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法返回后:"+methodName+":结果是"+result);
    }

    public void afterThrowing(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("执行方法异常后:"+methodName+":抛出"+ex);
    }
    
    public Object aroud(ProceedingJoinPoint pjd){
        Object result = null;
        try {
            System.out.println("前置通知");
            result = pjd.proceed();
            System.out.println("返回通知");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常通知");
        }
        System.out.println("后置通知");
        return result;
    }
}

 xml中配置

<?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.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <!--  配置bean  -->
    <bean id="calculation" class="com.llf.aop.xml.Calculation"></bean>

    <!--  配置切面bean  -->
    <bean id="pointCut" class="com.llf.aop.xml.PointCut"></bean>

    <!--  配置aop  -->
    <aop:config>
        <!-- 配置切点 -->
        <aop:pointcut id="point" expression="execution(* com.llf.aop.xml.Calculation.*(int, int))"/>
        <!-- ref配置指定切面bean,order配置切面优先级,越小越优先 -->
        <aop:aspect ref="pointCut" order="1">
            <!-- 前置通知 -->
            <aop:before method="before" pointcut-ref="point"></aop:before>
            <!-- 后置通知 -->
            <aop:after method="after" pointcut-ref="point"></aop:after>
            <!-- 返回通知 -->
            <aop:after-returning method="afterReturning" returning="result" pointcut-ref="point"></aop:after-returning>
            <!-- 异常通知 -->
            <aop:after-throwing method="afterThrowing" throwing="ex" pointcut-ref="point"></aop:after-throwing>

            <!-- 环绕通知:如果配了环绕通知,可以选择不配其他通知 -->
            <!--<aop:around method="aroud" pointcut-ref="point"></aop:around>-->
        </aop:aspect>
    </aop:config>

 </beans>

测试类Main:

package com.llf.aop.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author linglongfang
 */
public class Main {

    public static void main(String[] args){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("aop-xml.xml");
        Calculation calculation = (Calculation) ctx.getBean("calculation");
        calculation.add(2,3);
    }
}

 测试结果:

猜你喜欢

转载自www.cnblogs.com/linglongfang/p/12731149.html