zoukankan      html  css  js  c++  java
  • Spring10_AOP

    本教程源码请访问:tutorial_demo

    一、AOP概述

    1.1、概念

    AOP:全称是Aspect Oriented Programming,即:面向切面编程。

    通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

    上面是百度百科上的概念,看看就行了。

    简单的说AOP是把我们程序重复的代码抽取出来,在需要执行的时候,使用动态代理的技术,在不修改源码的基础上,对我们已有的方法进行增强。

    AOP用到了动态代理技术,这也是我们之前先学习动态代理的原因。

    1.2、AOP相关术语

    Joinpoint(连接点)

    连接点是指那些被拦截到的点。在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点。

    Pointcut(切入点)

    切入点是指我们要对哪些Joinpoint进行拦截的定义。

    Advice(通知/增强)

    通知是指拦截到Joinpoint之后所要做的事情就是通知 ,通知的类型:前置通知,后置通知,异常通知,最终通知,环绕通知。

    Introduction(引介)

    引介是一种特殊的通知在不修改类代码的前提下, Introduction 可以在运行期为类动态地添加一些方法或Field。

    Target(目标对象)

    代理的目标对象。

    Weaving(织入)

    是指把增强应用到目标对象来创建新的代理对象的过程。Spring采用动态代理织入,而 AspectJ采用编译期织入和类装载期织入。

    Proxy(代理)

    一个类被AOP织入增强后,就产生一个结果代理类。

    Aspect(切面)

    切入点和通知(引介)的结合。

    这些概念目前不需要全部掌握,只要知道连接点、切入点、通知/增强、目标对象、代理、切面就可以了。

    同时需要知道,在Spring中配置AOP就是配置在哪个切入点(哪个方法)上添加增强(通知),这其实就是配置切面,不理解没关系,后面学完案例,就明白了。

    1.3、使用AOP需要做的事

    • 编写核心业务代码;
    • 将公共的代码抽取出来,制作成通知;
    • 在配置中(XML或注解)中,声明切入点与通知之间的关系,即切面。

    二、基于XML的AOP配置

    需求:运行Service层方法时,打印日志信息,使用AOP进行配置。

    2.1、创建Maven工程

    pom.xml文件配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>org.codeaction</groupId>
        <artifactId>aop</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.13</version>
                <scope>test</scope>
            </dependency>
            <!-- AOP需要用到的包 -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.9.5</version>
            </dependency>
        </dependencies>
    </project>
    

    2.2、添加Service接口

    package org.codeaction.service;
    
    public interface IAccountService {
        void save();
        void update(int i);
        int delete();
    }
    

    注意这里方法的参数和返回值类型,后面不同类型的方法会有不同的配置。不要在意方法定义是否合理,这只是一个Demo程序。

    2.3、添加Service接口实现类

    package org.codeaction.service.impl;
    
    import org.codeaction.service.IAccountService;
    
    public class AccountServiceImpl implements IAccountService {
        @Override
        public void save() {
            System.out.println("save...");
        }
    
        @Override
        public void update(int i) {
            System.out.println("update...");
        }
    
        @Override
        public int delete() {
            System.out.println("delete...");
            return 0;
        }
    }
    

    不要在意方法定义是否合理,这只是一个Demo程序。

    2.4、添加日志类

    package org.codeaction.utils;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    /**
     * 用于记录日志的类,这些都是公共代码
     */
    public class Logger {
        /**
         * 用于打印日志:计划让其在切入点方法执行之前执行(切入点方法就是业务层方法)
         */
        public void printLog() {
            System.out.println("printLog....");
        }
    }
    

    2.5、添加Spring配置文件

    <?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
           https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 配置srping的Ioc,把service对象配置进来 -->
        <bean id="accountService" class="org.codeaction.service.impl.AccountServiceImpl"></bean>
    
        <!-- 配置Logger类 -->
        <bean id="logger" class="org.codeaction.utils.Logger"></bean>
        
        <!--配置AOP-->
        <aop:config>
            <!--配置切面 -->
            <aop:aspect id="logAdvice" ref="logger">
                <!-- 配置通知的类型,并且建立通知方法和切入点方法的关联-->
                <aop:before
                    method="printLog"
                    pointcut="execution(public void org.codeaction.service.impl.AccountServiceImpl.save())"></aop:before>
             </aop:aspect>
        </aop:config>
    </beans>
    

    beans.xml是这篇教程的重点,在这个文件中对Spring中的AOP进行了配置。

    Spring中基于XML的AOP配置步骤

    1. Spring中除了Service交给Spring管理,通知Bean也要交给Spring来管理;

    2. 使用aop:config标签开始AOP的配置;

    3. 使用aop:aspect标签配置切面。

      id属性:为切面提供一个唯一标识;

      ref属性:指定通知类bean的id;

    4. aop:aspect标签的内部使用对应标签来配置通知的类型,目前我们希望让printLog方法在切入点执行之前执行,所以是前置通知。

      aop:before:配置前置通知;

      method:指定哪个方法是前置通知;

      pointcut:指定切入点表达式,该表达式指的是对业务层中哪些方法增强。

    5. 切入点表达式的写法:

      关键字:execution(表达式);

      表达式:访问修饰符 返回值 包名.包名.包名...类名.方法名(参数列表);

      标准的表达式写法public void org.codeaction.service.impl.AccountServiceImpl.save()

      访问修饰符可以省略void org.codeaction.service.impl.AccountServiceImpl.save()

      返回值使用通配符,表示任意返回值* org.codeaction.service.impl.AccountServiceImpl.save()

      包名使用通配符,表示任意包。但是有几级包,就需要写几个*:* *.*.*.*.AccountServiceImpl.save()

      包名使用..表示当前包及其子包* *..AccountServiceImpl.save()

      类名和方法名都可以使用*来实现通配* *..*.*()

      参数列表:可以直接写数据类型,基本类型直接写名称(int),引用类型写包名.类名的方式(java.lang.String)。使用通配 符表示任意类型,但是必须有参数。使用..表示有无参数均可,有参数可以是任意类型。

      全通配写法* *..*.*(..)

      实际开发中切入点表达式的通常写法* org.codeaction.service.impl.*.*(..)

    2.6、添加测试方法

    package org.codeaction.test;
    
    import org.codeaction.service.IAccountService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:beans.xml")
    public class MyTest {
        @Autowired
        private IAccountService accountService;
    
        @Test
        public void testSave() {
            accountService.save();
        }
    }
    

    运行测试方法testSave,控制台输出如下:

    printLog....
    save...
    

    我们看到通过我们的配置,实现了在切入点上添加增强。

    我们还可以对切入点表达式进行修改,修改如下:

    <aop:before method="printLog" pointcut="execution(* *..*.*(int))"></aop:before>
    

    运行测试方法testSave,控制台输出如下:

    save...
    

    我们看到通过我们的配置,save方法上没有添加增强,原因是切入点表达式中参数为int类型和save方法不匹配。

    三、配置切入点及五种通知

    3.1、切入点

    如果配置多个通知,并且每个切入点表达式都是相同的,那么切入点表达式就会冗余,我们可以通过配置的方式单独定义切入点。

    标签aop:pointcut

    作用:用于配置切入点表达式,就是指定对哪些类的哪些方法进行增强。

    属性

    expression:用于定义切入点表达式;

    id:用于给切入点表达式提供一个唯一标识。

    <aop:pointcut id="pt1" expression="execution(* org.codeaction.service.impl.*.*(..))"/>
    

    此标签写在aop:aspect标签内部只能当前切面使用。它还可以写在aop:aspect外面,此时就变成了所有切面可用。

    3.2、四种常用通知

    3.2.1、前置通知

    标签aop:before

    作用:用于配置前置通知,指定增强的方法在切入点方法之前执行。

    属性

    method:指定通知类中的增强方法名称;

    pointcut-ref:指定切入点表达式的引用;

    pointcut:指定切入点表达式。

    执行时间点:切入点方法执行之前执行 。

    3.2.2、后置通知

    标签aop:after-returning

    作用:用于配置后置通知。

    属性

    method:指定通知类中的增强方法名称;

    pointcut-ref:指定切入点表达式的引用;

    pointcut:指定切入点表达式。

    执行时间点:切入点方法正常执行之后,它和异常通知只能有一个执行 。

    3.2.3、异常通知

    标签after-throwing

    作用:用于配置异常通知 。

    属性

    method:指定通知类中的增强方法名称;

    pointcut-ref:指定切入点表达式的引用;

    pointcut:指定切入点表达式。

    执行时间点:切入点方法执行产生异常后执行,它和后置通知只能执行一个 。

    3.2.4、最终通知

    标签aop:after

    作用:用于配置最终通知。

    属性

    method:指定通知类中的增强方法名称;

    pointcut-ref:指定切入点表达式的引用;

    pointcut:指定切入点表达式。

    执行时间点:无论切入点方法执行时是否有异常,它都会在其后面执行。

    3.2.5、案例

    本案例在“基于XML的AOP配置”基础上进行。

    3.2.5.1、修改日志类
    package org.codeaction.utils;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    
    /**
     * 用于记录日志的类,这些都是公共代码
     */
    public class Logger {
        /**
         * 前置通知
         */
        public void beforePrintLog() {
            System.out.println("beforePrintLog....");
        }
    
        /**
         * 后置通知
         */
        public void AfterReturningPrintLog() {
            System.out.println("AfterReturningPrintLog....");
        }
    
        /**
         * 异常通知
         */
        public void AfterThrowingPrintLog() {
            System.out.println("AfterThrowingPrintLog....");
        }
    
        /**
         * 最终通知
         */
        public void AfterPrintLog() {
            System.out.println("AfterPrintLog....");
        }
    }
    
    3.2.5.2、修改Spring的配置文件
    <?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
           https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 配置srping的Ioc,把service对象配置进来 -->
        <bean  id="accountService" class="org.codeaction.service.impl.AccountServiceImpl"></bean>
    
        <!-- 配置Logger类 -->
        <bean id="logger" class="org.codeaction.utils.Logger"></bean>
    
        <!--配置AOP-->
        <aop:config>
            <!-- 切入点 -->
            <aop:pointcut id="pt1" expression="execution(* org.codeaction.service.impl.*.*(..))"/>
            <!--配置切面 -->
            <aop:aspect id="logAdvice" ref="logger">
                <!-- 前置通知 -->
                <aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>
                <!-- 后置通知 -->
                <aop:after-returning method="AfterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>
                <!-- 异常通知 -->
                <aop:after-throwing method="AfterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>
                <!-- 最终通知 -->
                <aop:after method="AfterPrintLog" pointcut-ref="pt1"></aop:after>
             </aop:aspect>
        </aop:config>
    </beans>
    

    运行测试方法testSave,控制台输出如下:

    beforePrintLog....
    save...
    AfterReturningPrintLog....
    AfterPrintLog....
    

    前置通知、后置通知、最终通知、业务方法里面的内容都能够正常输出。

    3.2.5.3、修改Service实现类中的的save方法
    public void save() {
        System.out.println("save...");
        int i = 100 / 0;//人为的制造异常
    }
    

    运行测试方法testSave,控制台输出如下:

    beforePrintLog....
    save...
    AfterThrowingPrintLog....
    AfterPrintLog....
    
    java.lang.ArithmeticException: / by zero
    

    前置通知、异常通知、最终通知、业务方法里面的内容及异常信息都能够正常输出。

    3.3、环绕通知

    标签aop:around

    作用:用于配置环绕通知;

    属性

    method:指定通知类中的增强方法名称;

    pointcut-ref:指定切入点表达式的引用;

    pointcut:指定切入点表达式。

    说明:Spring框架为我们提供的一种可以在代码中手动控制增强代码什么时候执行的方式。

    注意:通常情况下,环绕通知都是独立使用的。

    3.3.1、修改日志类添加环绕通知代码

    /**
     * 环绕通知
     */
    public Object AroundPrintLog(ProceedingJoinPoint pjt) {
        Object returnValue = null;
        try {
            System.out.println("前置通知...");
            //获取参数
            Object[] args = pjt.getArgs();
            //调用业务层代码
            returnValue = pjt.proceed(args);
            System.out.println("后置通知...");
        } catch (Throwable t) {
            t.printStackTrace();
            System.out.println("异常通知...");
        } finally {
            System.out.println("最终通知...");
        }
        return returnValue;
    }
    

    我们在日志类中添加了环绕通知的代码。

    Spring框架为我们提供了一个接口:ProceedingJoinPoint。该接口有一个方法proceed(),此方法就相当于明确调用切入点方法。该接口可以作为环绕通知的方法参数,在程序执行时,Spring框架会为我们提供该接口的实现类供我们使用。

    3.3.2、修改Spring的配置文件

    <?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
           https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/aop
            https://www.springframework.org/schema/aop/spring-aop.xsd">
        <!-- 配置srping的Ioc,把service对象配置进来 -->
        <bean  id="accountService" class="org.codeaction.service.impl.AccountServiceImpl"></bean>
    
        <!-- 配置Logger类 -->
        <bean id="logger" class="org.codeaction.utils.Logger"></bean>
    
        <!--配置AOP-->
        <aop:config>
            <!-- 切入点 -->
            <aop:pointcut id="pt1" expression="execution(* org.codeaction.service.impl.*.*(..))"/>
            <!--配置切面 -->
            <aop:aspect id="logAdvice" ref="logger">
                <!-- 前置通知 -->
                <!--<aop:before method="beforePrintLog" pointcut-ref="pt1"></aop:before>-->
                <!-- 后置通知 -->
                <!--<aop:after-returning method="AfterReturningPrintLog" pointcut-ref="pt1"></aop:after-returning>-->
                <!-- 异常通知 -->
                <!--<aop:after-throwing method="AfterThrowingPrintLog" pointcut-ref="pt1"></aop:after-throwing>-->
                <!-- 最终通知 -->
                <!--<aop:after method="AfterPrintLog" pointcut-ref="pt1"></aop:after>-->
                <!-- 环绕通知 -->
                <aop:around method="AroundPrintLog" pointcut-ref="pt1"></aop:around>
             </aop:aspect>
        </aop:config>
    </beans>
    

    在Spring的配置文件中,我们添加了环绕通知,注释掉了其它四种通知,因为环绕通知通常情况下都是独立使用的。

    运行测试方法testSave,控制台输出如下:

    前置通知...
    save...
    后置通知...
    最终通知...
    

    按照3.2.5.3中的方式修改save方法,人为制造异常,控制台输出如下:

    前置通知...
    save...
    java.lang.ArithmeticException: / by zero
    	at org.codeaction.service.impl.AccountServiceImpl.save(AccountServiceImpl.java:9)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:344)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:100)
    	at org.codeaction.utils.Logger.AroundPrintLog(Logger.java:47)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
    	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
    	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
    	at com.sun.proxy.$Proxy15.save(Unknown Source)
    	at org.codeaction.test.MyTest.testSave(MyTest.java:18)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)
    	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)
    	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
    	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    	at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
    	at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
    	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    异常通知...
    最终通知...
    	at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    	at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
    
    

    四、基于XML和注解的AOP配置

    4.1、修改Service实现类

    package org.codeaction.service.impl;
    
    import org.codeaction.service.IAccountService;
    import org.springframework.stereotype.Service;
    
    @Service("accountService")
    public class AccountServiceImpl implements IAccountService {
        @Override
        public void save() {
            System.out.println("save...");
            //int i = 100 / 0;
        }
    
        @Override
        public void update(int i) {
            System.out.println("update...");
        }
    
        @Override
        public int delete() {
            System.out.println("delete...");
            return 0;
        }
    }
    

    添加@Service注解,把AccountServiceImpl交给Spring容器管理。

    4.2、修改日志类

    package org.codeaction.utils;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.stereotype.Component;
    
    @Component("logger")
    @Aspect
    public class Logger {
    
        @Pointcut("execution(* org.codeaction.service.impl.*.*(..))")
        private void pt(){}
    
        @Before("execution(* org.codeaction.service.impl.*.*(..))")
        public void beforePrintLog() {
            System.out.println("beforePrintLog....");
        }
    
        @AfterReturning("pt()")
        public void AfterReturningPrintLog() {
            System.out.println("AfterReturningPrintLog....");
        }
    
        @AfterThrowing("pt()")
        public void AfterThrowingPrintLog() {
            System.out.println("AfterThrowingPrintLog....");
        }
    
        @After("pt()")
        public void AfterPrintLog() {
            System.out.println("AfterPrintLog....");
        }
    
        //@Around("pt()")
        public Object AroundPrintLog(ProceedingJoinPoint pjt) {
            Object returnValue = null;
            try {
                //获取参数
                Object[] args = pjt.getArgs();
                //调用业务层代码
                System.out.println("前置通知...");
                returnValue = pjt.proceed();
                System.out.println("后置通知...");
            } catch (Throwable t) {
                t.printStackTrace();
                System.out.println("异常通知...");
            } finally {
                System.out.println("最终通知...");
            }
    
            return returnValue;
        }
    }
    

    添加@Component注解,把Logger交给Spring容器管理。

    @Aspect用于把当前类声明为切面类。

    @Pointcut用于指定切入点表达式。

    @Before、@AfterReturning、@AfterThrowing、 @After、@Around分别用来配置前置通知、后置通知、异常通知、最终通知、环绕通知,它们都有value属性,该属性用来指定切入点表达式,还可以指定切入点表达式的引用。

    4.3、修改Spring的配置文件

    <?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/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
        <!-- 配置spring创建容器时要扫描的包 -->
        <context:component-scan base-package="org.codeaction"></context:component-scan>
    
        <!-- 配置spring开启注解AOP的支持 -->
        <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>
    

    4.4、修改测试类

    package org.codeaction.test;
    
    import org.codeaction.service.IAccountService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:beans.xml")
    public class MyTest {
        @Autowired
        private IAccountService accountService;
    
        @Test
        public void testSave() {
            accountService.save();
        }
    }
    

    在不开启环绕通知的情况下,运行测试方法,控制台输出如下:

    beforePrintLog....
    save...
    AfterPrintLog....
    AfterReturningPrintLog....
    

    其他情况,大家可以自己测试一下。

    五、基于纯注解的AOP配置

    纯注解的情况,要完全抛弃Spring的XML配置文件。我们在上一节“基于XML和注解的AOP配置”代码的基础上进行配置,首先删除配置文件beans.xml

    5.1、添加配置类

    package org.codeaction.config;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    @Configuration
    @ComponentScan(basePackages = "org.codeaction")
    @EnableAspectJAutoProxy
    public class MainConfig {
    }
    

    @EnableAspectJAutoProxy配置Spring开启注解AOP的支持。

    5.2、修改测试类

    package org.codeaction.test;
    
    import org.codeaction.config.MainConfig;
    import org.codeaction.service.IAccountService;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = MainConfig.class)
    public class MyTest {
        @Autowired
        private IAccountService accountService;
    
        @Test
        public void testSave() {
            accountService.save();
        }
    }
    

    在不开启环绕通知的情况下,运行测试方法,控制台输出如下:

    beforePrintLog....
    save...
    AfterPrintLog....
    AfterReturningPrintLog....
    

    其他情况,大家可以自己测试一下。

    六、AOP实战

    在上一篇教程中,我们将Apache Commons DbUtils实现单表的CRUD操作的代码进行了修改,使用动态代理的方式使其支持事务操作。我们使用动态代理在不改变Service层实现类方法代码的前提下,对方法的功能进行了增强,接下来我们使用Spring提供的AOP的方式完成相同的功能,我们的代码在上一篇文章实战部分代码的基础上进行,首先删除org.codeaction.proxy包及其下面的所有内容

    6.1、修改JdbcConfig配置类

    package org.codeaction.util;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import javax.sql.DataSource;
    import java.sql.Connection;
    import java.sql.SQLException;
    
    @Component
    @Aspect
    public class JdbcUtils {
        private static DataSource dataSource;
        private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();
        //获取连接池对象
        public static DataSource getDataSource() {
            return dataSource;
        }
    
        @Autowired
        public void setDataSource(DataSource dataSource) {
            JdbcUtils.dataSource = dataSource;
        }
    
        @Pointcut("execution(* org.codeaction.service.impl.*.*(..))")
        public void pt() {
    
        }
    
        //获取连接
        public static Connection getConnection() throws SQLException {
            Connection conn = tl.get();
            if(conn == null) {
                return dataSource.getConnection();
            }
    	return conn;
        }
        //开启事务
        @Before("pt()")
        public static void beginTransaction() throws SQLException {
            Connection conn = tl.get();
            if(conn != null) {
                throw new SQLException("已经开启事务,不能重复开启");
    	}
    	conn = getConnection();
    	conn.setAutoCommit(false);
    	tl.set(conn);
        }
        //提交事务
        @AfterReturning("pt()")
        public static void commitTransaction() throws SQLException {
            Connection conn = tl.get();
    	if(conn == null) {
    	    throw new SQLException("连接为空,不能提交事务");
    	}
    	conn.commit();
    	conn.close();
    	tl.remove();
        }
        //回滚事务
        @AfterThrowing("pt()")
        public static void rollbackTransaction() throws SQLException {
            Connection conn = tl.get();
            if (conn == null) {
                throw new SQLException("连接为空,不能回滚事务");
    	}
    	conn.rollback();
    	conn.close();
    	tl.remove();
        }
    
        //@Around("pt()")
        public static Object around(ProceedingJoinPoint pjt) throws SQLException {
            Object returnValue = null;
    	
            try {
                JdbcUtils.beginTransaction();
                Object[] args = pjt.getArgs();
                returnValue = pjt.proceed(args);
                JdbcUtils.commitTransaction();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                JdbcUtils.rollbackTransaction();
            }
    	
            return returnValue;
        }
    }
    

    声明当前类为切面类,并配置通知。

    6.2、修改主配置类

    package org.codeaction.config;
    
    import org.springframework.context.annotation.*;
    
    @Configuration
    @ComponentScan(basePackages = "org.codeaction")
    @PropertySource("classpath:jdbc.properties")
    @Import(JdbcConfig.class)
    @EnableAspectJAutoProxy
    public class MyConfig {
    
    }
    

    配置开启注解支持。

    运行转账测试方法,能够正常完成转账操作;认为制造异常,能够回滚。

  • 相关阅读:
    access生成sql脚本,通过VBA调用ADOX
    virtualbox 使用USB引导启动安装系统
    atom 调用g++编译cpp文件
    VPython 三维显示 —— hello word
    sql高级篇(一)
    sql基础篇
    struts2中的<s:select>默认选项
    关于SVN更新注意
    mysql中的substr()函数
    mysql中exists的用法介绍
  • 原文地址:https://www.cnblogs.com/codeaction/p/13047230.html
Copyright © 2011-2022 走看看