Spring AOP 实现方式

本文详细介绍了 Spring AOP 的核心概念,包括连接点、切入点、切面等,并通过 XML 和注解两种方式展示了如何利用 Spring 实现 AOP 的过程。此外还探讨了基于代理的 AOP 实现。

一、Spring AOP 的相关术语

  • Joinpoint:连接点;指那些被拦截到的点。在Spring中,这些点指的是方法,因为Spring只支持方法类型的连接点。
  • Pointcut:切入点;指我们要对哪些“Joinpoint”进行拦截的定义。
  • Aspect:切面;是切入点和通知(引介)的集合;时间、地点和要发生的“故事”。
  • Advice:通知;指拦截到“Joinpoint”之后所要做的事情,
    • Before:前置通知
    • After-returning:后置通知
    • After-throwing:异常通知
    • Arround:环绕通知
  • Introdution:引介;引介是一种特殊的通知,在不修改类代码的前提下,Introdution可以在运行期为类动态的添加一些方法或属性。
  • Target:目标;目标对象,要增强的类。
  • Proxy:代理;一个类被AOP织入增强后,就产生一个结果代理类。
  • Weaving:织入;是把增强应用到目标的过程;把Advice应用到Target的过程。

二、Spring AOP 基本jar包

  • spring-beans-4.3.9.RELEASE.jar
  • spring-context-4.3.9.RELEASE.jar
  • spring-core-4.3.9.RELEASE.jar
  • spring-expression-4.3.9.RELEASE.jar
  • spring-aspects-4.3.9RELEASE.jar
  • aopalliance-1.0.jar
  • aspectjweaver-1.8.4.jar
  • commons-logging-1.1.1.jar
  • log4j-1.2.14.jar

三、aspectj的XML方式实现AOP

  • 被增强类与方法
package com.sitech.test11;

public class Book {

    public void look() {
        System.out.println(getClass().getName() + ".look()");
    }
}
  • 增强类与方法
package com.sitech.test11;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {

    // 前置通知
    public void before() {
        System.out.println(getClass().getName() + ".before()");
    }

    // 后置通知
    public void after() {
        System.out.println(getClass().getName() + ".after()");
    }

    // 环绕通知
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 方法之前
        System.out.println("方法之前");

        // 执行被增强的方法
        proceedingJoinPoint.proceed();

        // 方法之后
        System.out.println("方法之后");
    }
}

  • 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 http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->

    <!-- 配置对象 -->
    <bean id="book" class="com.sitech.test11.Book"></bean>
    <bean id="myBook" class="com.sitech.test11.MyBook"></bean>

    <!-- AOP 配置 -->
    <aop:config>
        <!-- 
           配置切入点
           expression:表达式 execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
               *: 访问修饰符,一般为*,代表所有修饰符
               com.sitech.test11.Book:类的全名
               add:方法名;也可写成*,代表所有方法;也可写成add*,代表所有add开头的方法
               ..:如果有参数也包含
           id:名字 
         -->
        <aop:pointcut expression="execution(* com.sitech.test11.Book.look(..))" id="pointcut1"/>

        <!--
            配置切面 
            ref:增强类
         -->
        <aop:aspect ref="myBook">
            <!-- 
              配置增强类型
              method:增强方法
              pointcut-ref:应用到哪个切入点上,这里的值也可以直接写execution表达式
             -->

            <!-- 前置通知 -->
            <aop:before method="before" pointcut-ref="pointcut1" />
            <!-- 后置通知 -->
            <aop:after-returning method="after" pointcut-ref="pointcut1"/>
            <!-- 环绕通知 -->
            <aop:around method="around" pointcut-ref="pointcut1"/>
        </aop:aspect>
    </aop:config>
</beans>
  • 测试类
package com.sitech.test11;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test11 {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test11.xml");
        Book book = (Book) applicationContext.getBean("book");
        book.look();
    }
}
  • 运行结果
com.sitech.test11.MyBook.before()
方法之前
com.sitech.test11.Book.look()
方法之后
com.sitech.test11.MyBook.after()

四、aspectj的注解方式实现AOP

  • 被增强类:使用注解生成实例
package com.sitech.test12;

import org.springframework.stereotype.Component;

@Component(value="book")
public class Book {

    public void look() {
        System.out.println(getClass().getName() + ".look()");
    }
}
  • 增强类:在类名上增加@Aspect注解表示开启AOP增强类
package com.sitech.test12;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component(value="myBook")
public class MyBook {

    // 前置通知
    @Before(value="execution(* com.sitech.test12.Book.look(..))")
    public void before() {
        System.out.println(getClass().getName() + ".before()");
    }

    // 后置通知
    @AfterReturning(value="execution(* com.sitech.test12.Book.look(..))")
    public void after() {
        System.out.println(getClass().getName() + ".after()");
    }

    // 环绕通知
    @Around(value="execution(* com.sitech.test12.Book.look(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        // 方法之前
        System.out.println("方法之前");

        // 执行被增强的方法
        proceedingJoinPoint.proceed();

        // 方法之后
        System.out.println("方法之后");
    }
}

  • 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/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">

    <!-- 
         开启注解扫描
         到包里扫描类、方法、属性是否有注解
         com.sitech只改包下的所有类,也可以写成com
     -->
    <context:component-scan base-package="com.sitech.test12"></context:component-scan>

    <!-- 开启AOP -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
  • 测试类
package com.sitech.test12;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test12 {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test12.xml");
        Book book = (Book) applicationContext.getBean("book");
        book.look();
    }
}
  • 运行结果:这里环绕通知会在前置通知之前执行,与XML配置结果不同
方法之前
com.sitech.test12.MyBook.before()
com.sitech.test12.Book.look()
方法之后
com.sitech.test12.MyBook.after()

五、基于代理的AOP实现

  • 代理接口
package com.sitech.test13;

public interface BaseBook {

    void look();
}
  • 实现类
package com.sitech.test13;

import org.springframework.stereotype.Component;

@Component(value="book")
public class Book
          implements BaseBook {

    public void look() {
        System.out.println(getClass().getName() + ".look()");
    }
}
  • 增强类:重点,要实现相应的接口
package com.sitech.test13;

import java.lang.reflect.Method;

import org.aspectj.lang.annotation.*;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;

@Aspect
@Component(value="myBook")
public class MyBook 
          implements MethodBeforeAdvice, AfterReturningAdvice {

    @Override
    public void before(Method arg0, Object[] arg1, Object arg2)
            throws Throwable {
        System.out.println(getClass().getName() + ".before()");
    }

    @Override
    public void afterReturning(Object arg0, Method arg1, Object[] arg2,
            Object arg3) throws Throwable {
        System.out.println(getClass().getName() + ".afterReturning()");
    }
}
  • 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"
    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/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">

    <!-- 
         开启注解扫描
         到包里扫描类、方法、属性是否有注解
         com.sitech只改包下的所有类,也可以写成com
     -->
    <context:component-scan base-package="com.sitech.test13"></context:component-scan>

    <!-- 定义切入点位置 -->  
    <bean id="lookPoincut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">  
        <property name="pattern" value=".*look"></property>  
    </bean>  

    <!-- 使切入点与通知相关联,完成切面配置 -->  
    <bean id="bookAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">  
        <property name="advice" ref="myBook"></property>         
        <property name="pointcut" ref="lookPoincut"></property>  
    </bean>  

    <!-- 设置代理 -->  
    <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
        <!-- 代理的对象 -->  
        <property name="target" ref="book"></property>  
        <!-- 使用切面 -->  
        <property name="interceptorNames" value="bookAdvisor"></property>  
        <!-- 代理接口 -->  
        <property name="proxyInterfaces" value="com.sitech.test13.BaseBook"></property>  
    </bean>  

</beans>
  • 测试类
package com.sitech.test13;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test13 {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("test13.xml");
        BaseBook book = (BaseBook) applicationContext.getBean("proxy");
        book.look();
    }
}
  • 运行结果
com.sitech.test13.MyBook.before()
com.sitech.test13.Book.look()
com.sitech.test13.MyBook.afterReturning()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值