zoukankan      html  css  js  c++  java
  • 使用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.

  • 相关阅读:
    [leetcode] Delete Operation for Two Strings
    [leetcode] Minimum ASCII Delete Sum for Two Strings
    [leetcode] Palindromic Substrings
    [leetcode] Student Attendance Record I
    [leetcode] Reverse String II
    [leetcode] Diameter of Binary Tree
    [leetcode] Climbing Stairs
    [leetcode] Range Sum Query
    Codeforces 1294A Collecting Coins
    团体程序设计天梯赛 L2-021 点赞狂魔 (25分)
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/9000484.html
Copyright © 2011-2022 走看看