spring-aop的介绍和使用

目录

1:为什么我会使用这个框架

2:那怎么快速入手属于自己的spring-aop呢(或者说怎么在自己项目调用spring-aop这个框架呢)

1->环境(自己去建一个maven项目)

2->导入spring-aop框架包(pom.xml)

3->编撰项目启动类(spring-boot)

4->spring-aop主要的五个注解

5->spring-aop之@Before

6->spring-aop之@After

7->spring-aop之@AfterReturning

8->spring-aop之@Around

3:敲黑板

4:下一篇动态代理和静态代理

5:本章资源下载链接


先说说这篇文章看完你能学习到什么吧

1:你可以知道为什么我会用这个,2:你能从中学习到aop的使用。
1:为什么我会使用这个框架

a:安全和不想掰扯别人的代码,为什么我会这么说呢?首先有的项目其实已经完成了或者代码在别人的模块。你自己不好改动他的部分,但是需要你动手添加后续的需求或者完善他的代码(对对方方法进行增强(advice),而不动到他的代码)。你怕出问题,这时候aop就可以上场,他会帮你解决这个问题。

2:那怎么快速入手属于自己的spring-aop呢(或者说怎么在自己项目调用spring-aop这个框架呢)
1->环境(自己去建一个maven项目)
【jdk8】,【maven3.5】,【intellj idea2018】
2->导入spring-aop框架包(pom.xml)
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId><version>3.2.2</version></dependency>
</dependencies>
3->编撰项目启动类(spring-boot)
@SpringBootApplication
public class MainRunApplication {public static void main(String args[]){new SpringApplication(MainRunApplication.class).run();}
}
4->spring-aop主要的五个注解
@Before(方法前置处理),@After(方法后置处理),@Around(方法环绕处理),@AfterReturning(方法返回值处理),@AfterThrowing(异常后续处理)
5->spring-aop之@Before

a:编撰被切入的类,并添加匹配@Before规则的方法

@Component
public class MainTest {public Map<Integer,Integer> before(String input){System.out.println("这是匹配到aop@Before切面规则的方法");return null;}
}

b:编写切面类Aspect并添加@Before注解的方法

@Aspect
@Component
public class RoutingAspect {@Before(value = "execution(* *..before(String))")public void aopBefore(JoinPoint joinPoint){System.out.println(joinPoint.getArgs()[0]);}}

c:编写启动测试

@SpringBootApplication
public class MainRunApplication {public static void main(String args[]){ConfigurableApplicationContext run = new SpringApplication(MainRunApplication.class).run();MainTest mainTest = run.getBeanFactory().getBean("mainTest",MainTest.class);mainTest.before("这是前置@Before监听方法的参数");}
}
6->spring-aop之@After

a:编撰被切入的类,并添加匹配@After规则的方法

@Component
public class MainTest {public Map<Integer,Integer> before(String input){System.out.println("这是匹配到aop@Before切面规则的方法");return null;}public Map<Integer,Integer> after(Map<Integer,Integer> output){System.out.println("这是匹配到aop@After切面规则的方法");return output;}
}

b:编写切面类Aspect并添加@After注解的方法

@Aspect
@Component
public class RoutingAspect {@Before(value = "execution(* *..before(String))")public void aopBefore(JoinPoint joinPoint){System.out.println(joinPoint.getArgs()[0]);}@After(value = "execution(* *..after(java.util.Map)) && args(output)",argNames = "output")public void aopBefore(Map<Integer,Integer> output){System.out.println("修改了传进来的参数,原始长度为"+output.size());output.put(1,1);}}

c:编写启动测试

@SpringBootApplication
public class MainRunApplication {public static void main(String args[]){ConfigurableApplicationContext run = new SpringApplication(MainRunApplication.class).run();MainTest mainTest = run.getBeanFactory().getBean("mainTest",MainTest.class);mainTest.before("这是前置@Before监听方法的参数");Map<Integer, Integer> after = mainTest.after(new HashMap<>());System.out.println("@After修改了传进来的参数,后续长度为"+after.size());}
}
7->spring-aop之@AfterReturning

a:编撰被切入的类,并添加匹配@AfterReturning规则的方法

@Component
public class MainTest {public Map<Integer,Integer> before(String input){System.out.println("这是匹配到aop@Before切面规则的方法");return null;}public Map<Integer,Integer> after(Map<Integer,Integer> output){System.out.println("这是匹配到aop@After切面规则的方法");return output;}public Map<Integer,Integer> afterReturn(Map<Integer,Integer> output){System.out.println("这是匹配到aop@AfterReturn切面规则的方法");return output;}
}

b:编写切面类Aspect并添加@AfterReturning注解的方法

@Aspect
@Component
public class RoutingAspect {@AfterReturning(value = "execution(* *..afterReturn(java.util.Map))",returning = "output")public void aopAfterReturning(JoinPoint joinPoint, Map<Integer,Integer> output){output.put(1,3);System.out.println("@AfterReturning修改了返回值" + output.size());}@Before(value = "execution(* *..before(String))")public void aopBefore(JoinPoint joinPoint){System.out.println(joinPoint.getArgs()[0]);}@After(value = "execution(* *..after(java.util.Map)) && args(output)",argNames = "output")public void aopBefore(Map<Integer,Integer> output){System.out.println("修改了传进来的参数,原始长度为"+output.size());output.put(1,1);}}

c:编写启动测试

@SpringBootApplication
public class MainRunApplication {public static void main(String args[]){ConfigurableApplicationContext run = new SpringApplication(MainRunApplication.class).run();MainTest mainTest = run.getBeanFactory().getBean("mainTest",MainTest.class);mainTest.before("这是前置@Before监听方法的参数");Map<Integer, Integer> after = mainTest.after(new HashMap<>());System.out.println("@After修改了传进来的参数,后续长度为"+after.size());Map<Integer, Integer> afterReturn = mainTest.afterReturn(new HashMap<>());}
}
8->spring-aop之@Around

a:新建个注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RoutingWith {String value() default "master";
}

b:编撰被切入的类,并添加匹配@Around规则的方法,以及使用注解RoutingWith

@Component
public class MainTest {public Map<Integer,Integer> before(String input){System.out.println("这是匹配到aop@Before切面规则的方法");return null;}public Map<Integer,Integer> after(Map<Integer,Integer> output){System.out.println("这是匹配到aop@After切面规则的方法");return output;}public Map<Integer,Integer> afterReturn(Map<Integer,Integer> output){System.out.println("这是匹配到aop@AfterReturn切面规则的方法");return output;}@RoutingWith(value = "mama")public void around(){System.out.println("这是匹配到aop@Around切面规则的方法");}}

c:编写切面类Aspect并添加@Around注解的方法,匹配切面方法上有RoutingWith注解的方法

@Aspect
@Component
public class RoutingAspect {@Around("@annotation(routingWith)")public Object routingWithDataSource(ProceedingJoinPoint proceedingJoinPoint, RoutingWith routingWith) throws Throwable{String key = routingWith.value();System.out.println(key);return proceedingJoinPoint.proceed();}@AfterReturning(value = "execution(* *..afterReturn(java.util.Map))",returning = "output")public void aopAfterReturning(JoinPoint joinPoint, Map<Integer,Integer> output){output.put(1,3);System.out.println("@AfterReturning修改了返回值" + output.size());}@Before(value = "execution(* *..before(String))")public void aopBefore(JoinPoint joinPoint){System.out.println(joinPoint.getArgs()[0]);}@After(value = "execution(* *..after(java.util.Map)) && args(output)",argNames = "output")public void aopBefore(Map<Integer,Integer> output){System.out.println("修改了传进来的参数,原始长度为"+output.size());output.put(1,1);}}

d:编写启动测试

@SpringBootApplication
public class MainRunApplication {public static void main(String args[]){ConfigurableApplicationContext run = new SpringApplication(MainRunApplication.class).run();MainTest mainTest = run.getBeanFactory().getBean("mainTest",MainTest.class);mainTest.before("这是前置@Before监听方法的参数");Map<Integer, Integer> after = mainTest.after(new HashMap<>());System.out.println("@After修改了传进来的参数,后续长度为"+after.size());Map<Integer, Integer> afterReturn = mainTest.afterReturn(new HashMap<>());mainTest.around();}
}

3:敲黑板

1:为什么第一步为什么要再pom.xmlj加入那个包?

答:因为想要使用spring-aop,必须先导入他的框架包,才能使用

2:为什么导入的是spring-boot-starter-aop

答:因为导入这个可以直接使用spring-boot,而不需要重复再导入spring-boot的包,这里说下spring-boot-starter这个合集开头是啥意思--------spring boot本来就是可以开箱即用的全家桶框架集合,他们通过约定俗成的方式介入到spring boot,并把自己命名成spring-boot-starter这样子开头的名,开发者只要导入相关的spring-boot-starter的就可以用他们的框架,例如spring-boot-starter-aop,这里有aspectj的包,也有spring-aop,也有spring-boot-starter,这样我们只要导入一个这样的包就可以使用spring boot的模块

3:spring-boot项目启动类的@SpringBootApplication是有啥用

答:这个标明了这个类是spring-boot 的入口启动类,并且里面有集成EnableAutoConfiguration,ComponentScan,Configuration等注解,方便好用,而且想要使用spring-boot启动,只要再加个

public static void main(String args[]){new SpringApplication(MainRunApplication.class).run();
}

就可以用了

4:以下代码是获取一个bean为mainTest的方法吗,那我用@Autowired也可以注入吧

ConfigurableApplicationContext run = new SpringApplication(MainRunApplication.class).run();
MainTest mainTest = run.getBeanFactory().getBean("mainTest",MainTest.class);

答:没错的,这个方法相当于@Autowired注入,只是这个是手动获取bean的IOC容器,也就是我们说的控制反转容器,这个容器管理了一大堆扫描到的bean,并在需要的时候(使用@Bean或者@Component等自动注入)

5:@Before(方法前置处理),@After(方法后置处理),@Around(方法环绕处理),@AfterReturning(方法返回值处理),@AfterThrowing(异常后续处理)这四个方法啥意思

答:简单来说就说在对象调用被切面的方法之前,之后,以及方法前后(环绕)进行相应的切面处理,环绕比较简单,就是可以自由处理方法,决定方法要不要调用,以及传参的处理,返回值的处理,AfterReturning是对返回值的处理,Around这个还可以对有相应注解的方法进行切片处理。这种一般用在ORM和日志等需要开启事务等开关上有用到。

6:spring-aop是aop,相对于oop?

答:没错,我们熟悉的oop是面向对象编程,aop就是面向切面编程,也就是Aspect

7:Around环绕可以对方法上有特定注解的方法进行切面,这个方法好像在spring中很好用也常见?

答:没错,我们orm框架中,那些@Service,@Dao相应的都有对应Around切面处理。

8:@After(value = "execution这个execution是接受切面的表达式吗?

答:没错的,execution里面写的是切面的表达式,表示需要匹配的方法规则,例如

execution(* *..after(java.util.Map)) && args(output)表示的是返回类型任意,包名任意类名任意,方法名字为after,参数类型为Map的方法会被当下注解规则匹配到切面。这里重点说下第一个*表示方法返回类型,第二个星号表示包名任意,然后..(两个点)表示零次或者多次点,也就是包名例如(com.zzu.z)里面的点。

4:下一篇动态代理和静态代理
5:本章资源下载链接

https://download.csdn.net/download/weixin_36667844/88783992

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/648146.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Python全自动性能无人机

Python全自动性能无人机研发开发的重要性可以从以下几个方面进行阐述&#xff1a; 编程语言的灵活性&#xff1a;Python是一种高级编程语言&#xff0c;具有简单易学的特点&#xff0c;能够快速地实现想法并进行快速原型设计。这种灵活性使得Python成为开发无人机控制系统的理想…

Linux命令-apachectl命令(Apache服务器前端控制工具)

apachectl命令 是Apache的Web服务器前端控制工具&#xff0c;用以启动、关闭和重新启动Web服务器进程。 语法 apachectl (参数)参数 configtest&#xff1a;检查设置文件中的语法是否正确&#xff1b;fullstatus&#xff1a;显示服务器完整的状态信息&#xff1b;graceful&a…

引领未来:云原生在产品、架构与商业模式中的创新与应用

文章目录 一、云原生产品创新二、云原生架构设计三、云原生商业模式变革《云原生落地 产品、架构与商业模式》适读人群编辑推荐内容简介目录 随着云计算技术的不断发展&#xff0c;云原生已经成为企业数字化转型的重要方向。接下来将从产品、架构和商业模式三个方面&#xff0c…

最小覆盖子串(Leetcode76)

例题&#xff1a; 分析: 比如现在有字符串&#xff08;s&#xff09;&#xff0c;s "ADOBECODEBANC", 给出目标字符串 t "ABC", 题目就是要从原始字符串&#xff08;s&#xff09;中找到一个子串&#xff08;res&#xff09;可以覆盖目标字符串 t &…

微信小程序(十六)slot插槽

注释很详细&#xff0c;直接上代码 上一篇 温馨提醒&#xff1a;此篇需要自定义组件的基础&#xff0c;如果不清楚请先看上一篇 新增内容&#xff1a; 1.单个插槽 2.多个插槽 单个插糟 源码&#xff1a; myNav.wxml <view class"navigationBar custom-class">…

师如灯塔,照我前行:我在誉天的RHCA认证之旅

时光荏苒&#xff0c;岁月如梭。2022年10月&#xff0c;我踏上了通向RHCA&#xff08;Red Hat Certified Architect&#xff09;证书的征程。2023年11月&#xff0c;我成功拿到了RHCA证书&#xff0c;也给这段旅程画上了圆满的句号。 而在这充满挑战和成长的旅程中&#xff0c;…

防火墙ip配置

如图所示需要配置该拓扑的ip 1.首先在交换机7上创建vlan 2 3 [Huanwei]vlan batch 2 3 2.分别进入0/0/3 和0/0/2接口 [Huawei-GigabitEthernet0/0/3]port link-type access [Huawei-GigabitEthernet0/0/2]port link-type access 3.定义所属vlan [Huawei-GigabitEthernet0…

【python爬虫】爬虫编程技术的解密与实战

​&#x1f308;个人主页&#xff1a;Sarapines Programmer&#x1f525; 系列专栏&#xff1a; 爬虫】网络爬虫探秘⏰诗赋清音&#xff1a;云生高巅梦远游&#xff0c; 星光点缀碧海愁。 山川深邃情难晤&#xff0c; 剑气凌云志自修。 目录 &#x1f33c;实验目的 &#x1f…

ARM架构SOC运行Linux的典型启动流程

ARM架构SOC运行Linux的典型启动流程 对于运行linux的ARM架构的SOC来说,典型的启动流程应该从上电开始。通常经过:上电复位、Boot模式选择、Boot ROM加载、Boot Flash加载、Bootloader加载、Linux内核加载、Linux内核启动、用户空间初始化、用户空间运行。 一、上电复位 当…

LeetCode85. Maximal Rectangle——单调栈

文章目录 一、题目二、题解 一、题目 Given a rows x cols binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area. Example 1: Input: matrix [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”…

Canvas图像与视频基础,离屏19

/*除了可以把图像绘制到canvas中&#xff0c;还可以把自身或其它canvas、视频的某一帧绘制到当前绘图环境中。 --1--有时两个canvas的相互绘制需要创建离屏canvas&#xff0c;离屏技术实际上是用空间换时间的一种技术 创建离屏canvas元素&#xff0c;可以动态创建元素&…

python黑马lambda匿名函数

1、区别 deflambda 有名称 可以重复使用 没有名称 不可以重复使用 2、lambda 传入参数&#xff1a;函数体&#xff08;一行代码&#xff09; # 传统函数方式 def square(x):return x**2print(square(5)) # 输出&#xff1a;25 # 使用lambda表达式 square_lambda lambda …

数字孪生系统的难点

数字孪生系统的开发和实施涉及一些技术难点&#xff0c;这些难点需要综合应用多个领域的知识和技术来克服。以下是一些数字孪生系统开发中的技术难点&#xff0c;希望对大家有所帮助。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1…

【每日一题】4.LeetCode——杨辉三角

&#x1f4da;博客主页&#xff1a;爱敲代码的小杨. ✨专栏&#xff1a;《Java SE语法》 ❤️感谢大家点赞&#x1f44d;&#x1f3fb;收藏⭐评论✍&#x1f3fb;&#xff0c;您的三连就是我持续更新的动力❤️ &#x1f64f;小杨水平有限&#xff0c;欢迎各位大佬指点&…

监听元素宽高变化---new ResizeObserver

参考&#xff1a;ResizeObserver API详解-CSDN博客 有的时候需要监听某个元素的宽高变化&#xff0c;这个时候可以使用JS的 resizeObserver 钩子函数。 用于监视元素的大小变化。它可以观察一个或多个 DOM 元素&#xff0c;以便在元素的大小或形状发生变化时触发回调函数。R…

openssl3.2/test/certs - 058 - no subjectAltNames excluded by CA2.

文章目录 openssl3.2/test/certs - 058 - no subjectAltNames excluded by CA2.概述笔记END openssl3.2/test/certs - 058 - no subjectAltNames excluded by CA2. 概述 openssl3.2 - 官方demo学习 - test - certs 笔记 /*! * \file D:\my_dev\my_local_git_prj\study\open…

安全用电管理平台方案介绍——Acrelcloud-6000

安全用电管理平台是一个针对电力系统安全管理的平台&#xff0c;旨在提供对电力设备和用电行为进行监控、分析和管理的解决方案。该平台结合了物联网技术、数据分析和远程监控等技术手段&#xff0c;能够实时监测、分析和预警电力系统的安全状况&#xff0c;以便及时采取措施防…

广州工业元宇宙赋能新型工业化,推动工业制造业数字化转型发展

随着科技的飞速发展&#xff0c;新型工业化的概念逐渐成为全球关注的焦点。在数字化转型的浪潮中&#xff0c;工业制造业的发展面临着巨大的机遇和挑战。广州作为中国南方的重要工业基地&#xff0c;积极探索工业元宇宙的赋能作用&#xff0c;以推动工业制造业的数字化转型发展…

Backend - Django URL 路由 重定向 url编码解码

目录 一、url 的 <> 作用 &#xff08;一&#xff09;操作流程 &#xff08;二&#xff09;前端设置链接 1. 包括&#xff1a; 2. 比如 &#xff08;三&#xff09;后端匹配路由 1. 理解 2. 比如 &#xff08;三&#xff09;后端视图的 get( )的参数 1. 理解 …

pip清华源怎么换回来

怎么临时使用清华源 pip install scrapy -i https://pypi.Python.org/simple/怎么永久换源 pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple修改清华源后怎么换回来 删掉/home/XXX/.config/pip/pip.conf