Spring AOP 简介

1.AOP 百度简介

Aspect Oriented Programming(AOP)是较为热门的一个话题。AOP,国内大致译作“面向切面编程”。

“面向切面编程”,这样的名字并不是非常容易理解,且容易产生一些误导。有些人认为“OOP/OOD11即将落伍,AOP是新一代软件开发方式”。显然,发言者并没有理解AOP的含义。Aspect,的确是“方面”的意思。不过,汉语传统语义中的“方面”,大多数情况下指的是一件事情的不同维度、或者说不同角度上的特性,比如我们常说:“这件事情要从几个方面来看待”,往往意思是:需要从不同的角度来看待同一个事物。这里的“方面”,指的是事物的外在特性在不同观察角度下的体现。而在AOP中,Aspect的含义,可能更多的理解为“切面”比较合适。

可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加某种特定功能的一种技术。AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,提高代码的灵活性和可扩展性,AOP可以说也是这种目标的一种实现。

在Spring中提供了面向切面编程的丰富支持,允许通过分离应用的业务逻辑系统级服务(例如审计(auditing)和事务(transaction)管理)进行内聚性的开发。应用对象只实现它们应该做的——完成业务逻辑——仅此而已。它们并不负责(甚至是意识)其它的系统级关注点,例如日志或事务支持。

主要功能

日志记录,性能统计,安全控制,事务处理异常处理等等。

主要意图

将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。

AOP的相关术语

  • Joinpoint(连接点):

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

  • Pointcut(切入点):

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

  • Advice(通知/增强):

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

  • Introduction(引介):

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

  • Target(目标对象):

代理的目标对象。

  • Weaving(织入): 

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

  • Proxy(代理):

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

  • Aspect(切面):

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

2.AOP基于XML配置的入门案例:

整体文件列表:

1.首先导入IOC和AOP所需要的jar包

2.创建BookService,BookServiceImpl类,这个类完成核心功能操作

package com.man.service;

public interface BookService {
    void find();
    void save(int a);
    int del();
    int update();

}
package com.man.service.impl;

import com.man.service.BookService;

/**
 * 核心类
 */
public class BookServiceImpl implements BookService {

    @Override
    public void find() {
        System.out.println("查询所有");
    }

    @Override
    public void save(int a) {
        System.out.println("保存信息"+a);
    }

    @Override
    public int del() {
        System.out.println("删除信息");
        return 1;
    }

    @Override
    public int update() {
        System.out.println("修改信息");
        return 1;
    }
}

 3.创建Logger类,这个类是用来做功能的增强

package com.man.logger;

/**
 *增强类
 */
public class Logger {

    public void check() {
        System.out.println("前置通知/增强: 权限验证");
    }
    public void logPrint() {
        System.out.println("后置通知/增强: 日志输出");
    }
    public void exception() {
        System.out.println("异常通知/增强: 异常处理");
    }
    public void distroy() {
        System.out.println("最终通知/增强: 资源释放");
    }
}

4.在spring的ApplicationContext.xml(核心配置文件)中开始进行AOP的配置:

a.首先把核心类和增强类的bean配置到IOC的容器中

b.使用<aop:config>标签在进行AOP的配置,先通过aop:aspect标签标明谁是增强类。然后在标签中进行aop:before(前置)、aop:after-returning(后置)、aop:after-throwing(异常)、aop:after(最终)的配置,让增强类的某个方法对核心功能类的某一类方法进行功能增强。

<?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">

    <!--1.把所有类的对象交给IOC容器进行管理-->
    <bean id="logger" class="com.man.logger.Logger"/>
    <bean id="bookService" class="com.man.service.impl.BookServiceImpl"/>

    <!--2.AOP的配置:让增强类 的 哪个方法  动态进行何种增强   核心类 的 哪个方法-->
    <aop:config>
        <!--指定增强类并起个名字-->
        <aop:aspect id="aopLog" ref="logger">
            <!--前置通知/增强: 在核心类方法执行之前 进行 增强-->
            <aop:before method="check" pointcut="execution(* *..BookServiceImpl.*(..))"/>
            <aop:after-returning method="logPrint" pointcut="execution(* *..BookServiceImpl.*(..))"/>
            <aop:after-throwing method="exception" pointcut="execution(* *..BookServiceImpl.*(..))"/>
            <aop:after method="distroy" pointcut="execution(* *..BookServiceImpl.*(..))"/>
        </aop:aspect>

    </aop:config>

</beans>

5.在测试类中测试UserService的方法是否被增强

package com.man.servlet;

import com.man.service.BookService;
import com.man.service.impl.BookServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BookServlet {
    public static void main(String[] args) {
       
        BookService bookService = null;
        // 加载配置文件
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        // 代理对象获取方法
        bookService = context.getBean(BookService.class);
        // 方法执行
        bookService.find();
        System.out.println("===================");
        System.out.println();
        bookService.save(5);
        System.out.println("===================");
        System.out.println();
        bookService.del();
        System.out.println("===================");
        System.out.println();
        bookService.update();

    }
}

猜你喜欢

转载自blog.csdn.net/m0_70051776/article/details/129031787