0%

11.AOP

11.AOP

11.1什么是AOP

在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

11.2Aop在Spring中的作用

提供声明式事务;允许用户自定义切面

  • 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 ….
  • 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
  • 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
  • 目标(Target):被通知对象。
  • 代理(Proxy):向目标对象应用通知之后创建的对象。
  • 切入点(PointCut):切面通知 执行的 “地点”的定义。
  • 连接点(JointPoint):与切入点匹配的执行点。

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

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

11.3使用Spring实现AOP

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

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>

方式一:使用Spring的接口

  1. 编写我们的业务接口和实现类

    1
    2
    3
    4
    5
    6
    7
    public interface UserService {
    public void add();
    public void delete();
    public void update();
    public void select();

    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class UserServiceImpl implements UserService {
    public void add() {
    System.out.println("增加了一个用户");
    }

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

    public void update() {
    System.out.println("更新了一个用户");
    }

    public void select() {
    System.out.println("查询了一个用户");
    }
    }
  2. 写我们的增强类 , 我们编写两个 , 一个前置增强 一个后置增强

    1
    2
    3
    4
    5
    public class AfterLog implements AfterReturningAdvice {
    public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
    System.out.println(method.getName()+"执行后,返回结果为"+o);
    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class Log implements MethodBeforeAdvice {
    //method:要执行的目标方法
    //object:参数
    //o :目标对象
    public void before(Method method, Object[] objects, Object o) throws Throwable {
    System.out.println(o.getClass().getName()+"的"+method.getName()+"被执行了");

    }
    }
  3. 最后去spring的文件中注册 , 并实现aop切入实现 , 注意导入约束 .

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <?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
    https://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.lwj.service.UserServiceImpl"></bean>
    <bean id="log" class="com.lwj.log.Log"></bean>
    <bean id="afterLog" class="com.lwj.log.AfterLog"></bean>
    <!--方式一:使用原生spring接口-->
    <!--配置aop 导入aop约束-->
    <aop:config>
    <!--切入点 execution:要执行的位置-->
    <aop:pointcut id="pointcut" expression="execution(* com.lwj.service.UserServiceImpl.*(..))"/>
    <!--执行环绕增强-->
    <aop:advisor advice-ref="log" pointcut-ref="pointcut"></aop:advisor>
    <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"></aop:advisor>
    </aop:config>

    </beans>
  4. 测试

    1
    2
    3
    4
    5
    6
    public static void main(String[] args) {
    ApplicationContext context= new ClassPathXmlApplicationContext("applicationContext.xml");
    //动态代理的是接口
    UserService userService=(UserService) context.getBean("userService");
    userService.add();
    }

    Aop的重要性 : 很重要 . 一定要理解其中的思路 , 主要是思想的理解这一块 .

    Spring的Aop就是将公共的业务 (日志 , 安全等) 和领域业务结合起来 , 当执行领域业务时 , 将会把公共业务加进来 . 实现公共业务的重复利用 . 领域业务更纯粹 , 程序猿专注领域业务 , 其本质还是动态代理 .

方式二:使用自定义类[主要是切面定义]

  1. 自定义一个类

    1
    2
    3
    4
    5
    6
    7
    8
    public class DiyPointCut {
    public void before(){
    System.out.println("方法执行前");
    }
    public void after(){
    System.out.println("方法执行后");
    }
    }
  2. xml配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <!--方式二-->
    <bean id="diy" class="com.lwj.diy.DiyPointCut"></bean>
    <aop:config>
    <!--自定义切面,ref引用的类-->
    <aop:aspect ref="diy">
    <!--切入点-->
    <aop:pointcut id="pointcut" expression="execution(* com.*.*.*.*(..))"/>
    <!--通知-->
    <aop:before method="before" pointcut-ref="pointcut"></aop:before>
    <aop:after method="after" pointcut-ref="pointcut"></aop:after>
    </aop:aspect>
    </aop:config>
  3. 测试代码和方式一形同

方式二三:使用注解实现

  1. 开启注解支持和配置bean

    1
    2
    3
    <bean id="annotationPointCut" class="com.lwj.diy.AnnotationPointCut"></bean>
    <!--开启注解支持-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
  2. 自定义类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    //方式三:只用注解实现aop
    @Aspect//标注这个类为切面
    public class AnnotationPointCut {
    @Before("execution(* com.lwj.*.*.*(..))")
    public void before(){
    System.out.println("方法执行前");
    }
    @After("execution(* com.lwj.*.*.*(..))")
    public void after(){
    System.out.println("方法执行后");
    }
  3. 测试方法与上面一致