Spring学习笔记--面向切面编程(AOP)
什么是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)的更多相关文章
- Spring学习笔记-面向切面(AOP)-04
什么是面向切面编程 先大概了解一下部分术语 横切关注点:软件开发中,散布于多出的功能称为横切关注点(cross-cutting concern),简单的可以描述为可以影响应用多处的功能,比如日志.安全 ...
- spring学习 八 面向切面编程(AOP)概述
注:本文大部分参考 --------------------- 本文来自 -望远- 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yanquan345/artic ...
- Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)
在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...
- Spring中的面向切面编程(AOP)简介
一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...
- Spring学习笔记:面向切面编程AOP(Aspect Oriented Programming)
一.面向切面编程AOP 目标:让我们可以“专心做事”,避免繁杂重复的功能编码 原理:将复杂的需求分解出不同方面,将公共功能集中解决 *****所谓面向切面编程,是一种通过预编译方式和运行期动态代理实现 ...
- Spring框架学习笔记(2)——面向切面编程AOP
介绍 概念 面向切面编程AOP与面向对象编程OOP有所不同,AOP不是对OOP的替换,而是对OOP的一种补充,AOP增强了OOP. 假设我们有几个业务代码,都调用了某个方法,按照OOP的思想,我们就会 ...
- Spring学习手札(二)面向切面编程AOP
AOP理解 Aspect Oriented Program面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 但是,这种说法有些片面,因为在软件工程中,AOP的价值体现的并 ...
- Spring之控制反转——IoC、面向切面编程——AOP
控制反转——IoC 提出IoC的目的 为了解决对象之间的耦合度过高的问题,提出了IoC理论,用来实现对象之间的解耦. 什么是IoC IoC是Inversion of Control的缩写,译为控制 ...
- 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 ...
随机推荐
- layui--入门(helloWorld)
具体可参考官方文档:https://www.layui.com/doc/ 由于引入layui 需要用到node.js 安装过程可参考: https://www.cnblogs.com/liuchenx ...
- SpringCloud 微服务最佳开发实践
Maven规范 所有项目必须要有一个统一的parent模块 所有微服务工程都依赖这个parent,parent用于管理依赖版本,maven仓库,jar版本的统一升级维护 在parent下层可以有 co ...
- Linux:Ubuntu配置jdk环境变量
1.下载jdk 推荐官网:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html 下载tar.gz包即可. ...
- gitlab hostname修改
cd /var/opt/gitlab/gitlab-rails/etc vim gitlab.yml /home/git/gitlab/config/gitlab.yml production: &a ...
- FirstDay
昨天心血来潮,想着注册一博客,没想到今天再登时,审阅就通过了,多少有点庆辛.从今天起,我也算是有博客的人了! 为什么选博客园开通?好多IT论坛里都允许有博文,CSDN感觉过于高大上,其他系列论坛大多内 ...
- Word转PDF的VBA脚本
将以下内容复制粘贴在一个txt中,修改txt后缀为".vbs" On Error Resume Next Const wdExportFormatPDF = 17 Set oWor ...
- Java开源协同办公项目:数据中心,自定义查询语句使用教程
O2OA提供的数据管理中心,可以让用户通过配置的形式完成对数据的汇总,统计和数据分组展现,查询和搜索数据形成列表数据展现.也支持用户配置独立的数据表来适应特殊的业务的数据存储需求.本文主要介绍如何在O ...
- python -- 正则表达式&re模块(转载)
1. 正则表达式基础 1.1. 简单介绍 正则表达式并不是Python的一部分.正则表达式是用于处理字符串的强大工具,拥有自己独特的语法以及一个独立的处理引擎,效率上可能不如str自带的方法,但功能十 ...
- python -- 面向对象编程(继承、重写)
一.继承 子类可以继承父类的所有公有成员,但不能直接访问父类的私有成员,只能通过父类的公有方法间接访问私有属性或私有方法. 如: class DerviedClassName(BaseClassNam ...
- 算法leetcode_分治算法
一.基本概念 在计算机科学中,分治法是一种很重要的算法.字面上的解释是"分而治之",就是把一个复杂的问题分成两个或更多的相同或相似的子问题,再把子问题分成更小的子问题--直到最后子 ...