• SpringAop (二)


    基于配置的方式

    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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
        <!-- 加载bean -->
        <bean id="arithmeticCalculator" class="com.aspectsAopxml.bean.ArithmeticCalculatorImpl">
        </bean>
        <!-- 配置切面的 bean. -->
        <bean id="aopxx" class="com.aspectsAopxml.ArithmeticCalculatorAspects">
        </bean>
        <bean id="aopAround" class="com.aspectsAopxml.ArithmeticCalculatorAspectsAround">
        </bean>
        <!-- 配置AOP -->
        <aop:config>
            <!-- 配置切点表达式 -->
            <aop:pointcut
                expression="execution(* com.aspectsAopxml.bean.ArithmeticCalculator.*(..))"
                id="pointcut" />
            <!-- 配置切面及通知 -->
            <aop:aspect ref="aopxx" order="2">
                <aop:before method="beforeMethod" pointcut-ref="pointcut" />
                <aop:after method="afterMethod" pointcut-ref="pointcut" />
                <aop:after-throwing method="afterThrowing"
                    pointcut-ref="pointcut" throwing="e" />
                <aop:after-returning method="afterReturning"
                    pointcut-ref="pointcut" returning="result" />
            </aop:aspect>
            <aop:aspect ref="aopAround" order="1">
                <aop:around method="aroundMethods" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config>
    </beans>
            

    切面

    package com.aspectsAopxml;
    
    import java.util.Arrays;
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class ArithmeticCalculatorAspectsAround {
    
        public Object aroundMethods(ProceedingJoinPoint point) {
    
            Object result = null;
            Object args = point.getArgs();
            String methodName = point.getSignature().getName();
            try {
                // 1.前置通知
                System.out.println("环绕前置通知信息Method:" + methodName + "<参数>==》" + Arrays.asList(args));
                // 2.执行目标方法
                result = point.proceed();
                // 3.返回通知
                System.out.println("环绕后置通知信息Method:" + methodName + "<参数>==》" + Arrays.asList(args) + "》》结果:" + result);
            } catch (Throwable e) {
                // 4.异常通知
                System.out.println("环绕后置通知信息Method:" + methodName + "<参数>==》" + Arrays.asList(args) + "异常信息:" + e);
                throw new RuntimeException(e);
            }
            // 5.方法完成后的返回通知
            System.out.println("环绕方法执行完成通知信息Method:" + methodName + "<参数>==》" + Arrays.asList(args));
            return result;
        }
    }
    package com.aspectsAopxml;
    
    import java.util.Arrays;
    import org.aspectj.lang.JoinPoint;
    
    public class ArithmeticCalculatorAspects {
    
        public void beforeMethod(JoinPoint joinPoint) {
    
            String methodName = joinPoint.getSignature().getName();
            Object[] args = joinPoint.getArgs();
            System.out.println("Methods befor......." + methodName + "-->args=>" + Arrays.asList(args));
        }
    
        public void afterMethod(JoinPoint joinPoint) {
    
            String methodName = joinPoint.getSignature().getName();
            Object[] args = joinPoint.getArgs();
            System.out.println("Methods after......." + methodName + "-->args=>" + Arrays.asList(args));
        }
    
        public void afterReturning(JoinPoint joinPoint, Object result) {
    
            String methodName = joinPoint.getSignature().getName();
            System.out.println("Methods afterReturn......." + methodName + "===" + result);
        }
    
        public void afterThrowing(JoinPoint joinPoint, Exception e) {
    
            String methodName = joinPoint.getSignature().getName();
            System.out.println("Methods afterTh......." + methodName + "===异常信息:" + e);
        }
    }

    测试

    package com;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import com.aspectsAopxml.bean.ArithmeticCalculator;
    
    public class Main {
    
        public static void main(String[] args) {
    
            ApplicationContext ctx = new ClassPathXmlApplicationContext("application-aspects.xml");
            ArithmeticCalculator target = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");
            int result = target.add(1, 2);
            System.out.println("result--->:" + result);
            result = target.sub(20, 12);
            System.out.println("result--->:" + result);
            result = target.mul(8, 8);
            System.out.println("result--->:" + result);
            result = target.div(1000, 0);
            System.out.println("result--->:" + result);
        }
    }
  • 相关阅读:
    命名函数
    匿名函数
    Elixir基础
    Elixir特性
    linux下安装erlang
    更具先序、中序遍历生成二叉树
    第一章 TCP/IP协议族
    React Native开发之expo中camera的基本使用
    Vue中异步组件(结合webpack,转载)
    vue中将html字符串转换成html后的一些问题
  • 原文地址:https://www.cnblogs.com/smokerBig/p/11165551.html
Copyright © 2020-2023  润新知