Java学习--spring框架--入门使用配置文件配置AOP

建立相应的接口
ArithmeticCalculator.java

public interface ArithmeticCalculator {
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);

}

接口的实现类
ArithmeticCalculatorImpl.java

@Component("ArithmeticCalculatorImpl")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
    public int add(int i, int j) {
        return i+j;
    }

    public int sub(int i, int j) {
        return i-j;
    }

    public int mul(int i, int j) {
        return i*j;
    }

    public int div(int i, int j) {
        return i/j;
    }
}

两个切面
LoggingAspect.java

public class LoggingAspect {

    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The LoggingAspect "+methodName+" begins with "+args);
    }

    public void afterMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+methodName+" ends");
    }

    public void afterReturning(JoinPoint joinPoint,Object r){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+methodName+" "+args+" result is " + r);
    }

    public void afterThrowing(JoinPoint joinPoint,Exception exception){
        String methodName = joinPoint.getSignature().getName();
        System.out.println("The method "+methodName+" exception is "+exception);
    }

    public Object around(ProceedingJoinPoint proceedingJoinPoint){
        System.out.println("Around");
        Object result = null;
        String methodName = proceedingJoinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(proceedingJoinPoint.getArgs());
        try {
//            前置通知
            System.out.println("  --> Around The method "+methodName+" begins with "+args);
            result = proceedingJoinPoint.proceed();
//            返回通知
            System.out.println("  --> Around The method "+methodName+" "+args+" result is " + result);
        } catch (Throwable throwable) {
//            异常通知
            System.out.println("  --> Around The method "+methodName+" exception is "+throwable);
        }
//        后置通知
        System.out.println("  --> Around The method "+methodName+" ends");
        return result;
    }

}

ViberAspect.java

public class ViberAspect {

    public void beforeMethod(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        List<Object> args = Arrays.asList(joinPoint.getArgs());
        System.out.println("==》 The ViberAspect "+methodName+" begins with "+args);
    }
}

配置文件

<?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.xsd">
<!--    配置bean-->
    <bean id="ArithmeticCalculatorImpl"
          class="spring.aop.xml.ArithmeticCalculatorImpl">

    </bean>

<!--    配置切面bean-->
    <bean id="loggingAspect"
          class="spring.aop.xml.LoggingAspect"></bean>
    <bean id="viberAspect"
          class="spring.aop.xml.ViberAspect"></bean>

<!--    配置AOP-->
    <aop:config>
<!--        配置切点表达式-->
        <aop:pointcut id="pointcut"
                      expression="execution(* spring.aop.xml.ArithmeticCalculatorImpl.*(..))"/>
<!--        配置切面、通知、优先级-->
        <aop:aspect ref="loggingAspect" order="2">
            <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
            <aop:after method="afterMethod" pointcut-ref="pointcut" ></aop:after>
            <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="r"></aop:after-returning>
            <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="exception"></aop:after-throwing>
            <aop:around method="around" pointcut-ref="pointcut"></aop:around>
        </aop:aspect>
        <aop:aspect ref="viberAspect" order="1">
            <aop:before method="beforeMethod" pointcut-ref="pointcut"></aop:before>
        </aop:aspect>
    </aop:config>


</beans>

主函数

    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("aop-xml.xml");
        ArithmeticCalculator arithmeticCalculator =
                (ArithmeticCalculator) applicationContext.getBean("ArithmeticCalculatorImpl");
        System.out.println("result: "+arithmeticCalculator.add(1,7));
    }

运行结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值