• 使用注解方式和XML配置方式完成AOP编程


    在这里插入图片描述

    第一种方式:基于注解

    beanx10.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: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">
    
        <!--扫描指定包-->
        <context:component-scan
                base-package="com.elf.spring.aop.homework"/>
    
        <!--启用基于注解的AOP功能-->
        <aop:aspectj-autoproxy/>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    package com.elf.spring.aop.homework;
    /**
     * @author 45
     * @version 1.0
     */
    public interface Cal {
        public int cal1(int n);
        public int cal2(int n);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    package com.elf.spring.aop.homework;
    import org.springframework.stereotype.Component;
    /**
     * @author 45
     * @version 1.0
     */
    @Component //将Cal对象作为组件,注入到Spring容器
    public class MyCal implements Cal {
        @Override
        public int cal1(int n) {
            int res = 1;
            for (int i = 1; i <= n; i++) {
                res += i;
            }
            System.out.println("cal1 执行结果=" + res);
            return res;
        }
        @Override
        public int cal2(int n) {
            int res = 1;
            for (int i = 1; i <= n; i++) {
                res *= i;
            }
            System.out.println("cal2 执行结果=" + res);
            return res;
        }
    }
    
    
    • 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
    package com.elf.spring.aop.homework;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.Signature;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    /**
     * @author 45
     * @version 1.0
     */
    @Aspect //MyCalAOP 是一个切面类
    @Component //MyCalAOP/对象 作为组件注入到spring容器
    public class MyCalAOP {
    
        //前置通知
        //这里注意,如果目标类和切面类,在同一个包,可以省略包名
        //因为cal1和cal2方法,都要去输出开始执行时间,因此使用MyCal.*
        @Before(value = "execution(public int MyCal.*(int))")
        public void calStart(JoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            System.out.println(signature.getName() + " 执行, 开始执行时间=" + System.currentTimeMillis());
    
        }
    
        //返回通知
        //这里注意,如果目标类和切面类,在同一个包,可以省略包名
        //因为cal1和cal2方法,都要去输出开始执行时间,因此使用MyCal.*
        @AfterReturning(value = "execution(public int MyCal.*(int))")
        public void calEnd(JoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            System.out.println(signature.getName() + " 执行, 结束时间=" + System.currentTimeMillis());
        }
    }
    
    
    • 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
    package com.elf.spring.aop.homework;
    import org.junit.jupiter.api.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    /**
     * @author 45
     * @version 1.0
     */
    public class TestMyCalAOP {
        @Test
        public void testMyCalByAnnotation() {
            //得到spring容器
            ApplicationContext ioc =
                    new ClassPathXmlApplicationContext("beans10.xml");
            Cal cal = ioc.getBean(Cal.class);
            cal.cal1(10);
            System.out.println("===========");
            cal.cal2(5);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    第二种方式:基于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 https://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <!--配置MyCalAOP-bean  id就用类名首字母小写-->
        <bean class="com.elf.spring.aop.homework.xml.MyCalAOP" id="myCalAOP" />
        <!--配置MyCal-bean-->
        <bean class="com.elf.spring.aop.homework.xml.MyCal" id="myCal"/>
        <!--配置切面类-->
        <aop:config>
            <!--配置切入点表达式-->
            <aop:pointcut id="myPointCut" expression="execution(public int com.elf.spring.aop.homework.xml.MyCal.*(int))"/>
            <!--配置前置,返回-->
            <aop:aspect ref="myCalAOP" order="10">
                <aop:before method="calStart" pointcut-ref="myPointCut"/>
                <aop:after-returning method="calEnd" 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
    package com.elf.spring.aop.homework.xml;
    /**
     * @author 45
     * @version 1.0
     * 复制粘贴的时候也要注意包名
     */
    public interface Cal {
        public int cal1(int n);
        public int cal2(int n);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    package com.elf.spring.aop.homework.xml;
    /**
     * @author 45
     * @version 1.0
     */
    public class MyCal implements Cal {
        @Override
        public int cal1(int n) {
            int res = 1;
            for (int i = 1; i <= n; i++) {
                res += i;
            }
            System.out.println("cal1 执行结果=" + res);
            return res;
        }
    
        @Override
        public int cal2(int n) {
            int res = 1;
            for (int i = 1; i <= n; i++) {
                res *= i;
            }
            System.out.println("cal2 执行结果=" + res);
            return res;
        }
    }
    
    
    • 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
    package com.elf.spring.aop.homework.xml;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.Signature;
    /**
     * @author 45
     * @version 1.0
     */
    public class MyCalAOP {
    
        //前置通知
    
        public void calStart(JoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            System.out.println(signature.getName() + " 基于XML配置- 执行, 开始执行时间=" + System.currentTimeMillis());
    
        }
    
        //返回通知
        public void calEnd(JoinPoint joinPoint) {
            Signature signature = joinPoint.getSignature();
            System.out.println(signature.getName() + " 基于XML配置- 执行, 结束时间=" + System.currentTimeMillis());
    
        }
    }
    
    
    • 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
    package com.elf.spring.aop.homework.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 TestMyCalAOP {
        @Test
        public void testMyCalByXML() {
            //得到spring容器
            ApplicationContext ioc =
                    new ClassPathXmlApplicationContext("beans11.xml");
            Cal cal = ioc.getBean(Cal.class);
            cal.cal1(10);
            System.out.println("===========");
            cal.cal2(5);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
  • 相关阅读:
    Java8实战-总结47
    LeetCode-160. Intersection of Two Linked Lists [C++][Java]
    你觉得java与嵌入式学哪个好?
    Android 笔记: 字符串截取操作方法
    Android GUI系统之SurfaceFlinger(16)MessageBase解读
    Ubuntu20.04 配置 yolov5_ros 功能包记录
    SpringBoot3自定义Starter步骤
    数据库管理-第111期 Oracle Exadata 02-硬件构成(20231017)
    深入理解netty(二)Channel
    DM@命题公式范式@析取范式和合取范式@主范式基础
  • 原文地址:https://blog.csdn.net/weixin_45036508/article/details/133135337