Spring AOP 简介

  • Spring AOP 简介

如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用。

AOP 即 Aspect Oriented Program 面向切面编程

首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。

  • 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
  • 所谓的周边功能,比如性能统计,日志,事务管理等等

周边功能在 Spring 的面向切面编程AOP思想里,即被定义为切面

在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发,然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP

AOP 的目的

AOP能够将那些与业务无关,却为业务模块所共同调用的逻辑或责任(例如事务处理、日志管理、权限控制等)封装起来,便于减少系统的重复代码降低模块间的耦合度,并有利于未来的可拓展性和可维护性

AOP 当中的概念:

  • 切入点(Pointcut)
    在哪些类,哪些方法上切入(where
  • 通知(Advice)
    在方法执行的什么实际(when:方法前/方法后/方法前后)做什么(what:增强的功能)
  • 切面(Aspect)
    切面 = 切入点 + 通知,通俗点就是:在什么时机,什么地方,做什么增强!
  • 织入(Weaving)
    把切面加入到对象,并创建出代理对象的过程。(由 Spring 来完成)

为了更好的说明 AOP 的概念,我们来举一个实际中的例子来说明:

在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让关注点代码与业务代码分离!

我们来实际的用代码感受一下

 1 package pojo;
 2 
 3 import org.springframework.stereotype.Component;
 4 
 5 @Component("landlord")
 6 public class Landlord {
 7 
 8     public void service() {
 9         // 仅仅只是实现了核心的业务功能
10         System.out.println("签合同");
11         System.out.println("收房租");
12     }
13 }
 1 package aspect;
 2 
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Component
 9 @Aspect
10 class Broker {
11 
12     @Before("execution(* pojo.Landlord.service())")
13     public void before(){
14         System.out.println("带租客看房");
15         System.out.println("谈价格");
16     }
17 
18     @After("execution(* pojo.Landlord.service())")
19     public void after(){
20         System.out.println("交钥匙");
21     }
22 }

在 applicationContext.xml 中配置自动注入,并告诉 Spring IoC 容器去哪里扫描这两个 Bean:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7        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">
 8 
 9     <context:component-scan base-package="aspect" />
10     <context:component-scan base-package="pojo" />
11 
12     <aop:aspectj-autoproxy/>
13 </beans>

测试代码

 1 package test;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 import pojo.Landlord;
 6 
 7 public class TestSpring {
 8 
 9     public static void main(String[] args) {
10 
11         ApplicationContext context =
12                 new ClassPathXmlApplicationContext("applicationContext.xml");
13         Landlord landlord = (Landlord) context.getBean("landlord", Landlord.class);
14         landlord.service();
15 
16     }
17 }

执行结果

使用注解来开发 Spring AOP

使用注解的方式已经逐渐成为了主流,所以我们利用上面的例子来说明如何用注解来开发 Spring AOP

第一步:选择连接点

Spring 是方法级别的 AOP 框架,我们主要也是以某个类额某个方法作为连接点,另一种说法就是:选择哪一个类的哪一方法用以增强功能。

我们在这里就选择上述 Landlord 类中的 service() 方法作为连接点。

第二步:创建切面

选择好了连接点就可以创建切面了,我们可以把切面理解为一个拦截器,当程序运行到连接点的时候,被拦截下来,在开头加入了初始化的方法,在结尾也加入了销毁的方法而已,在 Spring 中只要使用 @Aspect 注解一个类,那么 Spring IoC 容器就会认为这是一个切面了:

 1 package aspect;
 2 
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Component
 9 @Aspect
10 class Broker {
11 
12     @Before("execution(* pojo.Landlord.service())")
13     public void before(){
14         System.out.println("带租客看房");
15         System.out.println("谈价格");
16     }
17 
18     @After("execution(* pojo.Landlord.service())")
19     public void after(){
20         System.out.println("交钥匙");
21     }
22 }
  • 注意: 被定义为切面的类仍然是一个 Bean ,需要 @Component 注解标注

有了上表,我们就知道 before() 方法是连接点方法调用前调用的方法,而 after() 方法则相反,这些注解中间使用了定义切点的正则式,也就是告诉 Spring AOP 需要拦截什么对象的什么方法,下面讲到。

第三步:定义切点

在上面的注解中定义了 execution 的正则表达式,Spring 通过这个正则表达式判断具体要拦截的是哪一个类的哪一个方法:

 1 execution(* pojo.Landlord.service()) 

依次对这个表达式作出分析:

  • execution:代表执行方法的时候会触发
  • * :代表任意返回类型的方法
  • pojo.Landlord:代表类的全限定名
  • service():被拦截的方法名称

通过上面的表达式,Spring 就会知道应该拦截 pojo.Lnadlord 类下的 service() 方法。上面的演示类还好,如果多出都需要写这样的表达式难免会有些复杂,我们可以通过使用 @Pointcut 注解来定义一个切点来避免这样的麻烦:

 1 package aspect;
 2 
 3 import org.aspectj.lang.annotation.After;
 4 import org.aspectj.lang.annotation.Aspect;
 5 import org.aspectj.lang.annotation.Before;
 6 import org.aspectj.lang.annotation.Pointcut;
 7 import org.springframework.stereotype.Component;
 8 
 9 @Component
10 @Aspect
11 class Broker {
12 
13     @Pointcut("execution(* pojo.Landlord.service())")
14     public void lService() {
15     }
16 
17     @Before("lService()")
18     public void before() {
19         System.out.println("带租客看房");
20         System.out.println("谈价格");
21     }
22 
23     @After("lService()")
24     public void after() {
25         System.out.println("交钥匙");
26     }
27 }

测试结果

原文地址:https://www.cnblogs.com/smellpawn/p/10789872.html