Spring-AOP

AOP

1. 什么是AOP

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP

的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔

离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

2. AOP在Spring中的作用

AOP中的一些名词:

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 ,

    缓存 , 事务等等 ....

  • 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。

  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。

  • 目标(Target):被通知对象。

  • 代理(Proxy):向目标对象应用通知之后创建的对象。

  • 切入点(PointCut):切面通知 执行的 “地点”的定义。

  • 连接点(JointPoint):与切入点匹配的执行点。

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:

img

即 Aop 在 不改变原有代码的情况下 , 去增加新的功能 .

3. 通过Spring实现AOP

【重点】使用AOP织入,需要导入一个依赖包!

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.9.6</version>
</dependency>

1. 方式一: 使用Spring的API接口

1. execution详解

在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点"

1. 表达式示例
execution(* com.sample.service.impl..*.*(..))

详述:

  • execution(),表达式的主体
  • 第一个“*”符号,表示返回值类型任意;
  • com.sample.service.impl,AOP所切的服务的包名,即我们的业务部分
  • 包名后面的“..”,表示当前包及子包
  • 第二个“”,表示类名,即所有类
  • .*(..),表示任何方法名,括号表示参数,两个点表示任何参数类型
2. execution表达式语法格式
execution(<修饰符模式>?<返回类型模式><方法名模式>(<参数模式>)<异常模式>?)

除了返回类型模式方法名模式参数模式外,其它项都是可选的。

2. 接口

package com.wang.service;

public interface UserService {
    void add();
    void delete();
    void update();
    void query();
}

3. 实现类

package com.wang.service;

public class UserServiceImpl implements UserService{
    @Override
    public void add() {
        System.out.println("增加了一个用户");
    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("修改了一个用户");
    }

    @Override
    public void query() {
        System.out.println("查询了一个用户");
    }
}

4. 切入点添加的方法(这里模拟了log)

注意,这里要实行对应的通知的接口才可以使用!

  1. 前置通知
package com.wang.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {

    //method: 要执行的目标对象的方法
    //object: 参数
    //o:      目标对象
    @Override
    public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println(o.getClass().getName() + "的" + method.getName() + "被执行了");
    }
}
  1. 后置通知
package com.wang.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

public class AfterLog implements AfterReturningAdvice {
    //o: 返回值
    @Override
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
        System.out.println("执行了" + method.getName() + "方法, 返回结果为" + o);
    }
}

5. 在xml文件中配置Bean以及AOP

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--注册Bean-->
    <bean id="userService" class="com.wang.service.UserServiceImpl"/>
    <bean id="log" class="com.wang.log.Log"/>
    <bean id="aferLog" class="com.wang.log.AfterLog"/>

    <!--方式一: 使用原生Spring API接口-->
    <!--配置AOP-->
    <aop:config>
        <!--切入点: expression: 表达式;   execution(要执行的位置)-->
        <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImpl.*(..))"/>

        <!--执行环绕增加!-->
        <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="aferLog" pointcut-ref="pointcut"/>
    </aop:config>

</beans>

6. 测试

import com.wang.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    @Test
    public void Test1() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口,而不是实现类!
        UserService userService = context.getBean("userService", UserService.class);

        userService.add();
    }
}

7. 注意点

使用AOP后, getBean()方法由于使用了动态代理,要求返回一个接口而不是实体类!

2. 方式二: 使用自定义方式

主要是切面定义

1. 自定义通知

package com.wang.diy;

public class DiyPointCut {

    public void before() {
        System.out.println("================方法执行前================");
    }

    public void after() {
        System.out.println("================方法执行后================");
    }

}

2. XML中配置AOP

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--注册Bean-->
    <bean id="userService" class="com.wang.service.UserServiceImpl"/>
    <bean id="log" class="com.wang.log.Log"/>
    <bean id="aferLog" class="com.wang.log.AfterLog"/>

    <!--方式二:自定义类-->
    <bean id="diy" class="com.wang.diy.DiyPointCut"/>

    <aop:config>
        <!--自定义切面,  ref 要引用的类-->
        <aop:aspect ref="diy">
            <!--切入点-->
            <aop:pointcut id="pointcut" expression="execution(* com.wang.service.UserServiceImpl .*(..))"/>
            <!--通知-->
            <aop:before method="before" pointcut-ref="pointcut"/>
            <aop:after method="after" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>

</beans>

3. 方式三: 使用注解实现

1. 在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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--注册Bean-->
    <bean id="userService" class="com.wang.service.UserServiceImpl"/>
    <bean id="log" class="com.wang.log.Log"/>
    <bean id="aferLog" class="com.wang.log.AfterLog"/>

    <!--方式三-->
    <bean id="annotationPointCut" class="com.wang.diy.AnnotationPointCut"/>
    <!--开启注解支持-->
    <aop:aspectj-autoproxy/>

</beans>

2. 利用注解实现AOP

package com.wang.diy;

//使用注解方式实现AOP

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//标注这个类是一个切面
@Aspect
public class AnnotationPointCut {

    @Before("execution(* com.wang.service.UserServiceImpl .*(..))")
    public void before() {
        System.out.println("====================before execution====================");
    }

    @After("execution(* com.wang.service.UserServiceImpl .*(..))")
    public void after() {
        System.out.println("====================after execution====================");
    }

    //在环绕增强中,我们可以给定一个参数,达标我们要获取切入的点
    @Around("execution(* com.wang.service.UserServiceImpl .*(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("before around");

        //获得签名
        Signature signature = joinPoint.getSignature();
        System.out.println("signature " + signature);

        //执行方法
        Object proceed = joinPoint.proceed();

        System.out.println("after around");

        System.out.println(proceed);
    }
}
原文地址:https://www.cnblogs.com/wang-sky/p/13615060.html