博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring入门学习手册 3:AOP是个什么鬼?
阅读量:6966 次
发布时间:2019-06-27

本文共 5070 字,大约阅读时间需要 16 分钟。

一、AOP是什么?

AOP的全称是 Aspect Oriented Program(面向切面编程)

按照我的理解,AOP的思想通俗易懂的来说就是将程序的功能分为两类,一类是核心功能,另一类是辅助功能

在非面向切面编程里,核心功能辅助功能总是耦合在一起,比如说进行数据库操作时,日志记录行为的代码总是紧挨着数据库操作(不知道别的大神是什么样,反正我是这样)。这样一来,核心功能(数据库操作)的代码就和辅助功能(记录日志)的代码耦合在一起了。

而在AOP里,核心功能辅助功能的代码是完全分开的,它们可以各自独立修改,而完全不干扰对方,这实际上应该是应用了Java设计模式里的代理模式(改天记得复习一下)。

二、Spring如何应用AOP?

1.准备工作有哪些

  • 要加哪些包?

    spring的核心包就不用说了,除此之外还要加上和AOP有关的包,aspectJ,完整的maven配置文件如下:

    4.0.0
    SpringLearn
    SpringDemo
    1.0-SNAPSHOT
    org.springframework
    spring-aop
    4.3.11.RELEASE
    aopalliance
    aopalliance
    1.0
    org.springframework
    spring-beans
    4.3.11.RELEASE
    org.springframework
    spring-core
    4.3.11.RELEASE
    org.springframework
    spring-context
    4.3.11.RELEASE
    org.aspectj
    aspectjrt
    1.9.1
    org.aspectj
    aspectjweaver
    1.6.8
    commons-logging
    commons-logging
    1.2
    org.apache.maven.plugins
    maven-eclipse-plugin
    2.8
    true
    复制代码
  • 要加哪些类?

    LoggerAspect

    ProductService

    Category

    Product

目录结构如下:

2.核心功能 和 辅助功能 是怎么分开的?

创建一个核心功能的类 ProductService

package com.learn.service;import org.springframework.stereotype.Component;@Component("ps")public class ProductService {    public void doSomeService() {        System.out.println("假装这里是一些核心业务代码!");    }}复制代码

创建一个辅助功能的类 LoggerAspect

package com.learn.aspect;import org.springframework.stereotype.Component;import org.aspectj.lang.ProceedingJoinPoint;@Component("la")public class LoggerAspect {    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {        System.out.println("start log:" + joinPoint.getSignature().getName());        Object object = joinPoint.proceed();        System.out.println("end log:" + joinPoint.getSignature().getName());        return object;    }}复制代码

可以看到这里核心功能的代码和辅助功能的代码是完全分开的,那么怎么把它们联系到一起呢?

这就需要配置Spring的配置文件了,具体配置如下:

复制代码

配置文件中需要加入 <aop:config> 标签。

<aop:pointcut> 标签配置的是核心类,在这里指定了要织入的对象是哪个。

execution(* com.learn.service.ProductService.*(..))复制代码

上面这句表达式表示,所有ProductService下的方法都将被织入辅助功能,既在执行该方法时会调用辅助功能。 <aop:aspect> 配置的是辅助类

复制代码

上面标签中的 pointcut-ref 很显然就是要织入的核心类id, method 就是被织入的方法。

运行结果:

start log:doSomeService假装这里是一些核心业务代码!end log:doSomeService复制代码

运行结果呈现出这样的效果的是因为辅助类中log方法中是这样写的:

System.out.println("start log:" + joinPoint.getSignature().getName());Object object = joinPoint.proceed();System.out.println("end log:" + joinPoint.getSignature().getName());复制代码

log方法最终返回的是一个Object对象,就本例来说,返回的实际上是ProductService 对象,这里应该是用到了代理模式来实现。

三、用注解方式配置AOP

注解方式配置AOP也很简单,和注解方式配置bean是非常相似的。

首先,spring配置文件中把 <aop:config> 下的标签都删掉,换成 <aop:aspectj-autoproxy/> 标签来启用自动代理。

复制代码

LoggerAspect 类中也要做一些改变,加上@Aspect 和 @Around注解。

package com.learn.aspect;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.springframework.stereotype.Component;import org.aspectj.lang.ProceedingJoinPoint;@Aspect@Component("la")public class LoggerAspect {    @Around(value = "execution(* com.learn.service.ProductService.*(..))")    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {        System.out.println("start log:" + joinPoint.getSignature().getName());        Object object = joinPoint.proceed();        System.out.println("end log:" + joinPoint.getSignature().getName());        return object;    }}复制代码

运行结果:

start log:doSomeService假装这里是一些核心业务代码!end log:doSomeService复制代码

转载地址:http://tibsl.baihongyu.com/

你可能感兴趣的文章