• 基于XML配置的AOP


    在这里插入图片描述
    不用基于注解的方式,改用基于xml配置AOP的方式.

    beans09.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           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/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        
        
        <bean class="com.elf.spring.aop.xml.SmartAnimalAspect" id="smartAnimalAspect"/>
        
        <bean class="com.elf.spring.aop.xml.SmartDog" id="smartDog"/>
        
        <aop:config>
            
            <aop:pointcut id="myPointCut" expression="execution(public float com.elf.spring.aop.xml.SmartDog.getSum(float, float)))"/>
            
            <aop:aspect ref="smartAnimalAspect" order="10">
                
                <aop:before method="showBeginLog" pointcut-ref="myPointCut"/>
                
                <aop:after-returning method="showSuccessEndLog" pointcut-ref="myPointCut" returning="res"/>
                
                <aop:after-throwing method="showExceptionLog" pointcut-ref="myPointCut" throwing="throwable"/>
                
                <aop:after method="showFinallyEndLog" pointcut-ref="myPointCut"/>
                
                
            aop:aspect>
        aop:config>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    package com.elf v.spring.aop.xml;
    import org.junit.jupiter.api.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    /**
     * @author 45
     * @version 1.0
     */
    public class AopAspectjXMLTest {
        @Test
        public void testAspectByXML() {
    
            ApplicationContext ioc = new ClassPathXmlApplicationContext("beans09.xml");
            SmartAnimalable smartAnimalable =
                    ioc.getBean(SmartAnimalable.class);
    
            smartAnimalable.getSum(10, 2);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    package com.elf.spring.aop.xml;
    
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.Signature;
    
    import java.util.Arrays;
    
    /**
     * @author 45
     * @version 1.0
     *
     * 这是我们开发一个切面类, 但是不用注解,而是使用XML配置
     */
    public class SmartAnimalAspect {
    
    
        public void showBeginLog(JoinPoint joinPoint) {
            //通过连接点对象joinPoint 可以获取方法签名
            Signature signature = joinPoint.getSignature();
            System.out.println("SmartAnimalAspect-XML配置-切面类showBeginLog()[使用的myPointCut()]-方法执行前-日志-方法名-" + signature.getName() + "-参数 "
                    + Arrays.asList(joinPoint.getArgs()));
        }
    
        public void showSuccessEndLog(JoinPoint joinPoint, Object res) {
            Signature signature = joinPoint.getSignature();
            System.out.println("SmartAnimalAspect-XML配置-切面类showSuccessEndLog()-方法执行正常结束-日志-方法名-" + signature.getName() + " 返回的结果是=" + res);
        }
    
    
        public void showExceptionLog(JoinPoint joinPoint, Throwable throwable) {
            Signature signature = joinPoint.getSignature();
            System.out.println("SmartAnimalAspect-XML配置-切面类showExceptionLog()-方法执行异常-日志-方法名-" + signature.getName() + " 异常信息=" + throwable);
        }
    
        public void showFinallyEndLog(JoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            System.out.println("SmartAnimalAspect-XML配置-切面类showFinallyEndLog()-方法最终执行完毕-日志-方法名-" + signature.getName());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    package com.elf.spring.aop.xml;
    
    /**
     * @author 45
     * @version 1.0
     * 接口
     */
    public interface SmartAnimalable {
        //求和
        float getSum(float i, float j);
        //求差
        float getSub(float i, float j);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    package com.elf.spring.aop.xml;
    /**
     * @author 45
     * @version 1.0
     */
    //@Component //使用@Component 当spring容器启动时,将 SmartDog注入到容器
    public class SmartDog implements SmartAnimalable {
        @Override
        public float getSum(float i, float j) {
            float result = i + j;
            //result = 1 / 0; //模拟一个算术异常
            System.out.println("方法内部打印result = " + result);
            return result;
        }
    
        @Override
        public float getSub(float i, float j) {
            float result = i - j;
            System.out.println("方法内部打印result = " + result);
            return result;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    爱上开源之golang入门至实战-第二章语言基础-内存管理
    open graph协议
    GBase 8c数据类型-几何类型
    自定义starter
    【Java 进阶篇】Java XML组成部分:理解XML的结构
    中级程序员——uniapp和小程序面试题
    爬虫采集如何解决ip被限制的问题呢?
    1.1 大数据简介-hadoop-最全最完整的保姆级的java大数据学习资料
    wandb报错Network error (ProxyError), entering retry loop
    【JUC】交换器Exchanger详解
  • 原文地址:https://blog.csdn.net/weixin_45036508/article/details/133098712