AOP:Aspect Oriented Program 面向切面编程。如果说IoC是Spring的核心,那么面向切面编程就是Spring作为重要的功能之一了。

        在面向切面编程的思想里面,把功能分为核心业务功能和周边功能。核心业务功能就是系统开发最基本的业务功能,而周边功能则是附加业务功能上的增强功能,如:日志记录,事务管理,性能监控,权限检验等。

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

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

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

AOP中术语:

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

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

 

 逻辑代码

1.pojo目录下Landlord.java

@Component("landlord")
public class Landlord {

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

2.aspect目录下Broker.java

@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.测试

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.效果

 

 使用注解开发Spring AOP

第一步:选择连接点

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

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

第二步:创建切面

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

@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:异常通知,当连接点方法异常时调用。

第三步:定义切点

Spring通过正则表达式判断具体要拦截的是哪一个类的哪一个方法:

execution(* pojo.Landlord.service())

分析表达式:

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

如果使用多个相同的表达式,那么可以使用@Pointcut注解来定义一个切点避免这样的麻烦:

@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("交钥匙");
    }
}

环绕通知

环绕通知是Spring AOP中最强大的通知,因为它集成了前置通知和后置通知,还保留了连接点原有的方法的功能,既强大又灵活。

@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

  • aop:advisor:定义AOP的通知(一种很古老的方式,很少使用)。
  • aop:aspect:定义一个切面。
  • aop:before:定义前置通知。
  • aop:after:定义后置通知。
  • aop:around:定义环绕通知。
  • aop:after-returning:定义返回通知。
  • aop:after-throwing:定义异常通知。
  • aop:config:顶层的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>

 

 posted on 2019-11-01 11:33  会飞的金鱼  阅读(227)  评论(0)    收藏  举报