• 使用Spring框架入门三:基于XML配置的AOP的使用


    一、引入Jar包

            <!--测试1使用-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.0.5.RELEASE</version>
            </dependency>
            <!--测试2、3、4、5、6使用-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>5.0.5.RELEASE</version>
            </dependency>
            <!--测试Aop使用-->
            <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.1</version>
            </dependency>

    注意,如果不引入aspectjweaver包,会报找不到类的错误。

    二、测试步骤

    1、新建切入点类(JoinPoint):

    package aoptest1;
    
    public class MyWorker {
        public void aopPointMethod1() {
            System.out.println("this is aopPointMethod1 executed.");
        }
    }

    2、建立增强类(Advice)

    package aoptest1;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    public class MyWorkerExtension {
        public void aopInspectAtBefore() {
            System.out.println("this is aopInspectAtBefore method execute.");
        }
    
        public void aopInspectAtAfter() {
            System.out.println("this is aopInspectAtAfter method execute.");
        }
    
        public void aopAround(ProceedingJoinPoint proceedingJoinPoint) {
            try {
                System.out.println("aopAround1");
                proceedingJoinPoint.proceed();
                System.out.println("aopAround2");
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

    3、建立配置文件在resources下:applicationContextAopTest1.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
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
        <context:component-scan base-package="aoptest1"/>
        <!--配置实体-->
        <bean id="myworker1" class="aoptest1.MyWorker"/>
        <bean id="myworkerExtension1" class="aoptest1.MyWorkerExtension"/>
        <!--配置AOP-->
        <aop:config>
            <!--配置切入点-->
            <aop:pointcut expression="execution(* aoptest1.MyWorker.aopPointMethod1(..))" id="aopPointMethod1PointCut"/>
            <!--配置切面-->
            <aop:aspect ref="myworkerExtension1">
                <aop:before method="aopInspectAtBefore" pointcut-ref="aopPointMethod1PointCut"/>
                <aop:after method="aopInspectAtAfter" pointcut-ref="aopPointMethod1PointCut"/>
                <aop:around method="aopAround" pointcut-ref="aopPointMethod1PointCut"/>
            </aop:aspect>
    
        </aop:config>
    </beans>

    4、测试

    import aoptest1.MyWorker;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class AopTest {
        @Test
        public void aopTest1() {
            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContextAopTest1.xml");
            MyWorker mywoker = context.getBean(MyWorker.class);
            mywoker.aopPointMethod1();
        }
    }

    5、测试结果

    this is aopInspectAtBefore method execute.
    aopAround1
    this is aopPointMethod1 executed.
    aopAround2
    this is aopInspectAtAfter method execute.

  • 相关阅读:
    十分钟开发一个调用Activity的PhoneGap插件
    Mac下MAMP初试体验
    探索Android中的Parcel机制(上)
    两个栈实现队列+两个队列实现栈----java
    php实现工厂模式
    Hibernate Criterion
    Android用户界面概览
    秒杀多线程第四篇 一个经典的多线程同步问题
    Java串口通信具体解释
    逗比之——程序猿装逼手冊1(0基础版)
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/9000484.html
Copyright © 2020-2023  润新知