关于spring:springaop

12次阅读

共计 6496 个字符,预计需要花费 17 分钟才能阅读完成。

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】下新建一个【Landlord】类(我百度翻译的包租婆的英文):

package pojo;

import org.springframework.stereotype.Component;

@Component("landlord")
public class Landlord {public void service() {
        // 仅仅只是实现了外围的业务性能
        System.out.println("签合同");
        System.out.println("收房租");
    }
} 

2. 在 Package【aspect】下新建一个中介商【Broker】类(我还是用的翻译 …):

package aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
class Broker {@Before("execution(* pojo.Landlord.service())")
    public void before(){System.out.println("带租客看房");
        System.out.println("谈价格");
    }

    @After("execution(* pojo.Landlord.service())")
    public void after(){System.out.println("交钥匙");
    }
} 

3. 在 applicationContext.xml 中配置主动注入,并通知 Spring IoC 容器去哪里扫描这两个 Bean:

<?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="aspect" />
    <context:component-scan base-package="pojo" />

    <aop:aspectj-autoproxy/>
</beans> 

4. 在 Package【test】下编写测试代码:

package test;

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

public class TestSpring {public static void main(String[] args) {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Landlord landlord = (Landlord) context.getBean("landlord", Landlord.class);
        landlord.service();}
} 

5. 执行看到成果:

这个例子应用了一些注解,当初看不懂没有关系,但咱们能够从下面能够看到,咱们在 Landlord 的 service() 办法中仅仅实现了外围的业务代码,其余的关注点性能是依据咱们设置的切面 主动补全 的。


应用注解来开发 Spring AOP

应用注解的形式曾经逐步成为了支流,所以咱们利用下面的例子来阐明如何用注解来开发 Spring AOP

第一步:抉择连接点

Spring 是办法级别的 AOP 框架,咱们次要也是以某个类额某个办法作为连接点,另一种说法就是:抉择哪一个类的哪一办法用以加强性能。

 ....
    public void service() {
        // 仅仅只是实现了外围的业务性能
        System.out.println("签合同");
        System.out.println("收房租");
    }
    .... 

咱们在这里就抉择上述 Landlord 类中的 service() 办法作为连接点。

第二步:创立切面

抉择好了连接点就能够创立切面了,咱们能够把切面了解为一个拦截器,当程序运行到连接点的时候,被拦挡下来,在结尾退出了初始化的办法,在结尾也退出了销毁的办法而已,在 Spring 中只有应用 @Aspect 注解一个类,那么 Spring IoC 容器就会认为这是一个切面了:

package aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Component
@Aspect
class Broker {@Before("execution(* pojo.Landlord.service())")
    public void before(){System.out.println("带租客看房");
        System.out.println("谈价格");
    }

    @After("execution(* pojo.Landlord.service())")
    public void after(){System.out.println("交钥匙");
    }
} 
  • 留神: 被定义为切面的类依然是一个 Bean,须要 @Component 注解标注

代码局部中在办法下面的注解看名字也能猜出个大略,上面来列举一下 Spring 中的 AspectJ 注解:

注解

阐明

@Before

前置告诉,在连接点办法前调用

@Around

盘绕告诉,它将笼罩原有办法,然而容许你通过反射调用原有办法,前面会讲

@After

后置告诉,在连接点办法后调用

@AfterReturning

返回告诉,在连接点办法执行并失常返回后调用,要求连接点办法在执行过程中没有产生异样

@AfterThrowing

异样告诉,当连接点办法异样时调用

有了上表,咱们就晓得 before() 办法是连接点办法调用前调用的办法,而 after() 办法则相同,这些注解两头应用了定义切点的正则式,也就是通知 Spring AOP 须要拦挡什么对象的什么办法,上面讲到。

第三步:定义切点

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

execution(* pojo.Landlord.service()) 

顺次对这个表达式作出剖析:

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

通过下面的表达式,Spring 就会晓得应该拦挡 pojo.Lnadlord 类下的 service() 办法。下面的演示类还好,如果多出都须要写这样的表达式难免会有些简单,咱们能够通过应用 @Pointcut 注解来定义一个切点来防止这样的麻烦:

package aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
class Broker {@Pointcut("execution(* pojo.Landlord.service())")
    public void lService() {}

    @Before("lService()")
    public void before() {System.out.println("带租客看房");
        System.out.println("谈价格");
    }

    @After("lService()")
    public void after() {System.out.println("交钥匙");
    }
} 

第四步:测试 AOP

编写测试代码,然而我这里因为 JDK 版本不兼容呈现了 BUG….(难堪 …)

这就通知咱们:环境配置很重要 … 不然莫名其妙的 BUG 让你解体 …

盘绕告诉

咱们来探讨一下盘绕告诉,这是 Spring AOP 中最弱小的告诉,因为它集成了前置告诉和后置告诉,它保留了连接点原有的办法的性能,所以它及弱小又灵便,让咱们来看看:

package aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
class Broker {

//  正文掉之前的 @Before 和 @After 注解以及对应的办法
//  @Before("execution(* pojo.Landlord.service())")
//  public void before() {//      System.out.println("带租客看房");
//      System.out.println("谈价格");
//  }
//
//  @After("execution(* pojo.Landlord.service())")
//  public void after() {//      System.out.println("交钥匙");
//  }

    //  应用 @Around 注解来同时实现前置和后置告诉
    @Around("execution(* pojo.Landlord.service())")
    public void around(ProceedingJoinPoint joinPoint) {System.out.println("带租客看房");
        System.out.println("谈价格");

        try {joinPoint.proceed();
        } catch (Throwable throwable) {throwable.printStackTrace();
        }

        System.out.println("交钥匙");
    }
} 

运行测试代码,后果依然正确:


应用 XML 配置开发 Spring AOP

注解是很弱小的货色,但基于 XML 的开发咱们依然须要理解,咱们先来理解一下 AOP 中能够配置的元素:

AOP 配置元素

用处

备注

aop:advisor

定义 AOP 的告诉其

一种很古老的形式,很少应用

aop:aspect

定义一个切面

——

aop:before

定义前置告诉

——

aop:after

定义后置告诉

——

aop:around

定义盘绕告诉

——

aop:after-returning

定义返回告诉

——

aop:after-throwing

定义异样告诉

——

aop:config

顶层的 AOP 配置元素

AOP 的配置是以它为开始的

aop:declare-parents

给告诉引入新的额定接口,加强性能

——

aop:pointcut

定义切点

——

有了之前通过注解来编写的教训,并且有了下面的表,咱们将下面的例子改写成 XML 配置很容易(去掉所有的注解):

<!-- 拆卸 Bean-->
<bean name="landlord" class="pojo.Landlord"/>
<bean id="broker" class="aspect.Broker"/>

<!-- 配置 AOP -->
<aop:config>
    <!-- where:在哪些地方(包. 类. 办法)做减少 -->
    <aop:pointcut id="landlordPoint"
                  expression="execution(* pojo.Landlord.service())"/>
    <!-- what: 做什么加强 -->
    <aop:aspect id="logAspect" ref="broker">
        <!-- when: 在什么机会(办法前 / 后 / 前后)-->
        <aop:around pointcut-ref="landlordPoint" method="around"/>
    </aop:aspect>
</aop:config> 

运行测试程序,看到正确后果:

作者:我没有三颗心脏
链接:https://www.jianshu.com/p/994…
起源:简书
著作权归作者所有。商业转载请分割作者取得受权,非商业转载请注明出处。

正文完
 0