Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

实现两个整数的加减乘除,在每个方法执行前后打印日志。

ArithmeticCalculator.java:

package spring.aop.impl.xml;

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:

package spring.aop.impl.xml;


public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

    public int add(int i, int j) {
        int result = i+j;
        return result;
    }

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

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

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

}

LoggingAspect.java:

package spring.aop.impl.xml;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

public class LoggingAspect {
    

    public void beforeMethod(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        List<Object> args=Arrays.asList(joinPoint.getArgs());
        System.out.println("The method "+methodName+" begins "+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 afterReturnMethod(JoinPoint joinPoint,Object result){
        String methodName=joinPoint.getSignature().getName();
        System.out.println("The method "+methodName+" ends with afterReturning "+ result);
    }
    

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

    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        
        Object result=null;
        String methodName=joinPoint.getSignature().getName();
        try {
            //前置通知
            System.out.println("---->The method "+methodName+" begins with" +Arrays.asList(joinPoint.getArgs()));
            //执行目标方法
            result=joinPoint.proceed();
            //返回通知
            System.out.println("---->"+result);
        } catch (Throwable e) {
            e.printStackTrace();
            //异常通知
            System.out.println("---->"+e);
        }
        
        //后置通知
        System.out.println("---->The method "+methodName+ "ends");
        return result;
    }
}

ApplicationContext.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.3.xsd">

    <!-- 配置bean  -->
    <bean id="arithmeticCalculator" class="spring.aop.impl.xml.ArithmeticCalculatorImpl">
    </bean>
    
    <!-- 配置切面的bean -->
    <bean id="loggingAspect" class="spring.aop.impl.xml.LoggingAspect">
    </bean>
    
    <!-- 配置AOP -->
    <aop:config>
        <!-- 配置切点表达式 -->
        <aop:pointcut expression="execution(* spring.aop.impl.xml.ArithmeticCalculator.*(..))" 
        id="pointcut"/>
        
        <!-- 配置切面及通知 -->
        <aop:aspect ref="loggingAspect" order="1">
             <aop:before method="beforeMethod" pointcut-ref="pointcut"/>
             <aop:after method="afterMethod" pointcut-ref="pointcut"/>
             <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="ex"/>
        </aop:aspect>
    </aop:config>
    
</beans>

测试:

package spring.aop.impl.xml.test;

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

import spring.aop.impl.xml.ArithmeticCalculator;


public class Main {

    public static void main(String[] args) {

        ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext-xml.xml");
        ArithmeticCalculator arithmeticCalculator=(ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
    
        int result=arithmeticCalculator.add(10, 20);
        System.out.println("result:"+result);
        
        result=arithmeticCalculator.div(10, 0);
        System.out.println("result:"+result);
    }

}

输出:

The method add begins [10, 20]
The method add ends
result:30
The method div begins [10, 0]
The method div ends
The method div occurs exection: java.lang.ArithmeticException: / by zero
Exception in thread "main" java.lang.ArithmeticException: / by zero
at spring.aop.impl.xml.ArithmeticCalculatorImpl.div(ArithmeticCalculatorImpl.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:52)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterAdvice.invoke(AspectJAfterAdvice.java:47)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:62)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy2.div(Unknown Source)
at spring.aop.impl.xml.test.Main.main(Main.java:19)

 

全部评论

相关推荐

#简历#先说一说我自己的想法,很多人都很排斥苍穹外卖,认为没什么技术点和含金量,但实际上我觉得恰恰相反,苍穹外卖虽然代码本身并不是你自身能力的证明,但是是作为一个新人学习时很好的跳板和原始框架,在这个框架上进行的改进可以很好的辐射到你自己的个人成果上,并作为你和面试官聊天的筹码大多数人的苍穹外卖只写增删改查,千篇一律,吸引不了面试官,所以这才让大家误以为只要是苍穹外卖就不要写进简历里这种误区,但实际上如果你在原有的层面上进行改进,并作为你的项目亮点和面试官介绍,告诉他你的苍穹外卖和别人的有什么不同,增加了哪些技术难点,这才显得你是完全自己理解了这个项目,并且有自己动手实践项目的能力,而不是就看了个课程就以为自己会了,就当成自己的了,如此一来,这反而成为你的加分项苍穹外卖为什么看的人最多,说明它好啊,如果它不好,为什么看的人还这么多,想清楚这个逻辑,我觉得要做的最重要的事,就是如何在原有框架上进行改进提效,比起听其他人的话重新搞一个项目性价比高得多,而且我亲测项目并没有成为我找到工作的阻碍,我投的大厂一大半都给我面试了,而且很多不止一个部门,退一万步说,当你手头没有其他项目的时候,有苍穹外卖总比什么都没有的好很多,不需要因为苍穹外卖有任何心理负担关于简历的任何部分都欢迎大家提意见,十分感谢大家,祝大家找实习+秋招顺利上岸,offer拿到手软#简历中的项目经历要怎么写##我的上岸简历长这样##最后再改一次简历##简历##简历被挂麻了,求建议#
点赞 评论 收藏
转发
点赞 收藏 评论
分享
正在热议
# 牛客帮帮团来啦!有问必答 #
1152069次浏览 17149人参与
# 通信和硬件还有转码的必要吗 #
11209次浏览 101人参与
# OPPO开奖 #
19225次浏览 267人参与
# 和牛牛一起刷题打卡 #
19023次浏览 1635人参与
# 实习与准备秋招该如何平衡 #
203436次浏览 3627人参与
# 大厂无回复,继续等待还是奔赴小厂 #
4979次浏览 30人参与
# 不去互联网可以去金融科技 #
20484次浏览 257人参与
# 通信硬件薪资爆料 #
265968次浏览 2484人参与
# 国企是理工四大天坑的最好选择吗 #
2229次浏览 34人参与
# 互联网公司评价 #
97718次浏览 1280人参与
# 简历无回复,你会继续海投还是优化再投? #
25039次浏览 354人参与
# 0offer是寒冬太冷还是我太菜 #
454917次浏览 5124人参与
# 国企和大厂硬件兄弟怎么选? #
53918次浏览 1012人参与
# 参加过提前批的机械人,你们还参加秋招么 #
14647次浏览 349人参与
# 硬件人的简历怎么写 #
82290次浏览 852人参与
# 面试被问第一学历差时该怎么回答 #
19401次浏览 213人参与
# 你见过最离谱的招聘要求是什么? #
28222次浏览 248人参与
# 学历对求职的影响 #
161255次浏览 1804人参与
# 你收到了团子的OC了吗 #
538781次浏览 6387人参与
# 你已经投递多少份简历了 #
344278次浏览 4963人参与
# 实习生应该准时下班吗 #
96988次浏览 722人参与
# 听劝,我这个简历该怎么改? #
63525次浏览 622人参与
牛客网
牛客企业服务