Spring AOP-00-开篇

1、下面介绍与Spring AOP相关的一些概念。java

1. 横切关注点spring

一些具备横切多个不一样软件模块的行为,经过传统的软件开发方法不可以有效地实现模块化的一类特殊关注点。横切关注点能够对某些方法进行拦截,拦截后对原方法进行加强处理。模块化

2. 切面(Aspect)测试

切面就是对横切关注点的抽象,这个关注点可能会横切多个对象。spa

3. 链接点(JoinPoint)代理

链接点是在程序执行过程当中某个特定的点,好比某方法调用的时候或者处理异常的时候。因为Spring只支持方法类型的链接点,因此在Spring AOP中一个链接点老是表示一个方法的执行。code

4. 切入点(Pointcut)component

切入点是匹配链接点的拦截规则,在知足这个切入点的链接点上运行通知。切入点表达式如何和链接点匹配是AOP的核心,Spring默认使用AspectJ切入点语法。xml

5. 通知(Advice)对象

在切面上拦截到某个特定的链接点以后执行的动做。

6. 目标对象(Target Object)

目标对象,被一个或者多个切面所通知的对象,即业务中须要进行加强的业务对象。

7. 织入(Weaving)

织入是把切面做用到目标对象,而后产生一个代理对象的过程。

8. 引入(Introduction)

引入是用来在运行时给一个类声明额外的方法或属性,即不需为类实现一个接口,就能使用接口中的方法。

 

2、下面介绍加强的类型

AOP联盟为加强定义了org.aopalliance.aop.Advice接口,Spring支持5种类型的加强。如下显示用到的@Before、@After等注解是基于AspectJ实现的加强类型。其实Spring也支持不少加强类型,Spring AOP按照加强在目标类方法中的链接点位置能够分为5种。

• 前置加强(MethodBeforeAdvice):表示在目标方法执行前实施加强。

• 后置加强(AfterReturningAdvice):表示在目标方法执行后实施加强。

• 环绕加强(MethodInterceptor):表示在目标方法执行先后实施加强。

• 异常抛出加强(ThrowsAdvice):表示在目标方法抛出异常后实施加强。

• 引介加强(IntroductionInterceptor):表示在目标类中添加一些新的方法和属性。

AOP联盟的顶级接口是Advice,其类图以下:

3、用文字介绍概念太抽象了,如下用代码来表示各个概念

一、要加强的类

package com.test.aspectj.advicetype;

import org.springframework.stereotype.Component;

/**
 * 一个Spring Bean
 */
@Component
public class Person {

    /**
     * 说话的方法
     */
    public void say() {
        System.out.println("Hello spring5");
    }
}

二、切面类

package com.test.aspectj.advicetype;

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

/**
 * 包含各类加强类型的切面
 */
@Component
@Aspect
public class AllAspect {

    /**
     * 切入点
     */
    @Pointcut("execution(* com.test.aspectj.advicetype.*.*(..))")
    public void allAointCut() {

    }

    /**
     * 前置加强
     */
    @Before("allAointCut()")
    public void before() {
        System.out.println("before advice");
    }

    /**
     * 环绕加强
     * @param proceedingJoinPoint
     */
    @Around("allAointCut()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("around advice 1");
        proceedingJoinPoint.proceed();
        System.out.println("around advice 2");
    }

    /**
     * 后置加强
     */
    @AfterReturning("allAointCut()")
    public void afterReturning() {
        System.out.println("afterReturning advise");
    }

    /**
     * 异常抛出加强
     */
    @AfterThrowing("allAointCut()")
    public void afterThrowing() {
        System.out.println("afterThrowing advice");
    }

    /**
     * 后置加强
     */
    @After("allAointCut()")
    public void after() {
        System.out.println("after advise");
    }
}

三、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/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.test.aspectj.advicetype"/>
    <!-- 开启aop注解方式,此步骤不能少 -->
    <aop:aspectj-autoproxy/>
</beans>

四、测试代码

package com.test.aspectj.advicetype;

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

/**
 * 测试各类类型的加强
 */
public class AllAspectDemo {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-chapter3-aoptype.xml");
        Person person = (Person) context.getBean("person");
        person.say();
    }
}

五、运行结果

around advice 1
before advice
Hello spring5
around advice 2
after advise
afterReturning advise
相关文章
相关标签/搜索