什么是AOP

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

AOP在Spring中的作用

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

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

AOP实现

【搭建环境(普通Maven项目)】

导入依赖

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

【编写业务类(示例)】

  • UserService接口
package cn.iris.service;

/**
* @author Iris 2021/8/12
*/
public interface UserService { void add();
void del();
void update();
void query();
}
  • UserService实现类
package cn.iris.service;

/**
* @author Iris 2021/8/12
*/
public class UserServiceImpl implements UserService{ @Override
public void add() {
System.out.println("增加一个用户");
} @Override
public void del() {
System.out.println("删除一个用户");
} @Override
public void update() {
System.out.println("更新一个用户");
} @Override
public void query() {
System.out.println("查询一个用户");
}
}
  • 日志类(示例)
    • 前置日志类
package cn.iris.log;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

/**
* @author Iris 2021/8/12
*/
public class Log implements MethodBeforeAdvice { /**
* 前置日志
* @param method 要执行的目标对象的方法
* @param args 参数
* @param target 目标对象
* @throws Throwable
*/
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+ method.getName()+"被执行");
}
}
    • 结尾日志类
package cn.iris.log;

import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;

/**
* @author Iris 2021/8/12
*/
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+ method.getName()+"被执行后返回"+returnValue);
}
}
  • 测试类
import cn.iris.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Iris 2021/8/12
*/
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationConfig.xml");
// 动态代理是代理的接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}

方式一:使用Spring的接口

【Spring API 接口实现】

  • Spring配置文件
<?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
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--注册Bean-->
<bean class="cn.iris.service.UserServiceImpl" id="userService"/>
<bean class="cn.iris.log.Log" id="log"/>
<bean class="cn.iris.log.AfterLog" id="afterLog"/> <!--方式一:使用原生Spring API接口-->
<!--配置aop(导入aop约束)-->
<aop:config>
<!--切入点 execution: 要执行的位置-->
<aop:pointcut id="point" expression="execution(* cn.iris.service.UserServiceImpl.*(..))"/> <!--执行环绕增加-->
<aop:advisor advice-ref="afterLog" pointcut-ref="point"/>
<aop:advisor advice-ref="log" pointcut-ref="point"/>
</aop:config> </beans>

方式二:使用自定义类实现

【重点:切面定义】

  • Class DiyPointCut
package cn.iris.diy;

/**
* @author Iris 2021/8/13
*/
public class DiyPointCut { public void before() {
System.out.println("-----Before Method-----");
} public void after() {
System.out.println("-----After Method-----");
}
}
  • applicationConfig.xml
<!--方式二:自定义类-->
<bean class="cn.iris.diy.DiyPointCut" id="diy"/>
<aop:config>
<!--自定义切面,ref引用自定义类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* cn.iris.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>

方式三:注解实现AOP

  • Class AnnotationPointCut
package cn.iris.diy;

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; /**
* 方式三:使用注解实现AOP
* @author Iris 2021/8/13
*/
@Aspect //标注该类为一个切面 public class AnnotationPointCut { @Before("execution(* cn.iris.service.UserServiceImpl.*(..))")
public void before() {
System.out.println("-----Before Method-----");
} @After("execution(* cn.iris.service.UserServiceImpl.*(..))")
public void after() {
System.out.println("-----After Method-----");
} @Around("execution(* cn.iris.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint pjp) {
System.out.println("-----Before Around Method-----");
try {
Signature signature = pjp.getSignature();
System.out.println("Signature : "+signature);
Object proceed = pjp.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
System.out.println("-----After Around Method-----");
}
}
  • applicationConfig.xml
<!--方式三:使用注解实现AOP-->
<bean class="cn.iris.diy.AnnotationPointCut" id="anno"/>
<!--开启注解支持 JDK(默认:proxy-target-class="false") cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy/>

Spring学习笔记--面向切面编程(AOP)的更多相关文章

  1. Spring学习笔记-面向切面(AOP)-04

    什么是面向切面编程 先大概了解一下部分术语 横切关注点:软件开发中,散布于多出的功能称为横切关注点(cross-cutting concern),简单的可以描述为可以影响应用多处的功能,比如日志.安全 ...

  2. spring学习 八 面向切面编程(AOP)概述

    注:本文大部分参考   --------------------- 本文来自 -望远- 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yanquan345/artic ...

  3. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

  4. Spring中的面向切面编程(AOP)简介

    一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...

  5. Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)

    一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...

  6. Spring框架学习笔记(2)——面向切面编程AOP

    介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...

  7. Spring学习手札(二)面向切面编程AOP

    AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...

  8. Spring之控制反转——IoC、面向切面编程——AOP

      控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...

  9. 04 Spring:01.Spring框架简介&&02.程序间耦合&&03.Spring的 IOC 和 DI&&08.面向切面编程 AOP&&10.Spring中事务控制

    spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的A ...

随机推荐

  1. Opencv 播放mp4文件和读取摄像头图以及可能会发生的一些异常问题解决方法

    学习内容 学习Opencv 读取并播放本地视频和打开摄像头图像以及可能会发生的一些异常问题解决方法 代码演示 电脑环境信息: OpenCV版本:4.5.2 ,vs2017 1.视频文件读取与播放 加载 ...

  2. [源码解析] 深度学习分布式训练框架 horovod (11) --- on spark --- GLOO 方案

    [源码解析] 深度学习分布式训练框架 horovod (11) --- on spark --- GLOO 方案 目录 [源码解析] 深度学习分布式训练框架 horovod (11) --- on s ...

  3. 同步工具——Exchanger

    本博客系列是学习并发编程过程中的记录总结.由于文章比较多,写的时间也比较散,所以我整理了个目录贴(传送门),方便查阅. 并发编程系列博客传送门 本文是转载文章,原文请见这里 一.Exchanger简介 ...

  4. MySql:mysql命令行导入导出sql文件

    命令行导入 方法一:未连接数据库时方法 #导入命令示例 mysql -h ip -u userName -p dbName < sqlFilePath (结尾没有分号) -h : 数据库所在的主 ...

  5. [心得体会]SpringMVC源码分析

    1. SpringMVC (1) springmvc 是什么? 前端控制器, 主要控制前端请求分配请求任务到service层获取数据后反馈到springmvc的view层进行包装返回给tomcat, ...

  6. 使用Hugo框架搭建博客的过程 - 页面模板

    前言 最初在制作友链界面时,没有学习Hugo框架,一头雾水.网上有关的教程甚少,只能去学一遍Hugo. 在学习Hugo的过程中,了解了列表模板,分类模板.开发了几个功能页面,如:留言板,友链,记忆分类 ...

  7. nodejs安装+vue安装

    一.nodejs安装 电脑win7的,nodejs V12.16.2以前的版本支持win7 nodejs下载地址: http://mirrors.nju.edu.cn/nodejs/v12.15.0/ ...

  8. Java基础00-集合基础15

    1. 集合基础 1.1 集合概述 1.2 ArrayList构造方法和添加方法 代码示例: 想通过指定索引添加到最后一个位置的话就填写,现有索引的+1,比如3个索引就是4.如果在指定索引的位置添加没有 ...

  9. nginx+waf防火墙

    1.官网下载nginx源码包(nginx-1.20.0.tar.gz) 新建nginx安装目录​mkdir -p /opt/nginx​新增nginx运行用户​useradd -s /sbin/nol ...

  10. 基于JSP的学生考勤管理系统(MySQL版)

    介绍:基于JSP的学生考勤管理系统(MySQL版)1.包含源程序,数据库脚本.代码和数据库脚本都有详细注释.2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善.开发环境:Eclipse ,MyS ...