Spring的AOP切面编程
语言苍白无力,我们直接代码说话
package com.example.demo.aspect;
import org.springframework.stereotype.Component;
@Component
public class AtithmeticCalulator {public int add(int a,int b){return a+b;}public int sub(int a,int b){return a-b;}public int mul(int a,int b){return a*b;}public int div(int a,int b){return a/b;}}
这是一个类,以方法add为例,当我们想在每一个方面前面添加一个告诉自己方法名和参数的时候,你会怎么写?
public int add(int a,int b){System.out.println("method mane:add 参数["+a+","+b+"]");return a+b;}
有没有感觉很麻烦,如果我四个方法都要用,你就要写4遍
这个时候AOP派上用场
package com.example.demo.aspect; import java.util.Arrays;
import java.util.List; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; @Component//将组件加载到ioc容器,必须写,否则加载不到ioc容器
@Aspect//告诉ioc容器这是一个切面类,里面有切面方法
public class MyAspect { //切面表达式public int com.example.demo.aspect.AtithmeticCalulator.*(int,int) //包com.example.demo.aspect下的AtithmeticCalulator类所是public ,返回值是int,参数是(int,int)的方法 @Before("execution(public int com.example.demo.aspect.AtithmeticCalulator.*(int,int))")public void before(JoinPoint joinPoint) { String name=joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("----the method "+name +" is begin:"+args); } @After("execution(public int com.example.demo.aspect.AtithmeticCalulator.*(int,int))") public void after(JoinPoint joinPoint) { String name=joinPoint.getSignature().getName(); List<Object> args=Arrays.asList(joinPoint.getArgs()); System.out.println("----the method "+name +" is close:"+args); } }
运行结果所示:
Spring的AOP切面编程相关推荐
- Spring中AOP切面编程学习笔记
注解方式实现aop我们主要分为如下几个步骤: 1.在切面类(为切点服务的类)前用@Aspect注释修饰,声明为一个切面类. 2.用@Pointcut注释声明一个切点,目的是为了告诉切面,谁是它的服务对 ...
- Spring(4)——面向切面编程(AOP模块)
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
- Aop切面编程原理和Spring实现
Aop切面编程概念 AOP切面编程一般可以帮助我们在不修改现有代码的情况下,对程序的功能进行拓展,往往用于实现 日志处理,权限控制,性能检测,事务控制等 AOP实现的原理就是动态代理,在有接口的情况下 ...
- Spring中的AOP切面编程的三种实现方式
文章目录 Spring中的AOP切面编程的三种实现方式 1.最基本AOP的实现 a.引入jar包 b.编写通知类,这里以后置通知和环绕通知类为例子进行说明 c.在SpringIOC容器中配置 d.测试 ...
- java切面类整合_SpringBoot2.x【五】整合AOP切面编程
SpringBoot2.x[五]整合AOP切面编程 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方面. ...
- SpringBoot2.0 基础案例(11):配置AOP切面编程,解决日志记录业务
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.AOP切面编程 1.什么是AOP编程 在软件业,AOP为Asp ...
- SpringAop篇 (2) Spring中的切面编程技术 AspectJ
介绍 : AOP(Aspect Orient Programming) 既为面向切面编程. 它可以说是OOP编程的一种扩展与补充,可以较为友好的处理不同模块之间具有横向相关性质的一类问题,比如日志管理 ...
- spring框架学习 - 使用 Spring 的面向切面编程
接上一篇博客:https://blog.csdn.net/qq_43605444/article/details/122029896?spm=1001.2014.3001.5502 七.使用 Spri ...
- Spring MVC AOP切面失效原因与处理
Spring MVC AOP切面失效原因与处理 问题概述 AOP的两类实现 解决方案 方法一:用Autowired 注入自身的实例 方法二:从Spring上下文获取增强后的实例引用 方法三: 利用Ao ...
最新文章
- 为什么要读源代码,如何阅读源代码
- HTML基础_Day01
- 网络流最大流Edmonds-Karp算法(模板)
- CodeForces - 1333F Kate and imperfection(数论+贪心)
- Android从无知到有知——NO.7
- JavaScript | 如何为变量分配十进制,八进制和十六进制值?
- Android实现自定义相册,在Android Gallery App中创建自定义相册
- mysql某个表的列除以2_数据库mysql(二)
- 关于配置tomcat多版本同eclipse的配置问题
- 触发器应用 trigger
- 转帖:XHTML原型开发-用代码说话
- pd17虚拟机启动器生成方法
- [转载]CRC校验原理
- html点中弹出选择框,javascript点击弹出选择框
- DW CS5及CC的部分序列号总结
- Chrome浏览器占用CPU资源过高(Software Reporter Tool)
- Greenplum数据库巡检报告
- 计算机2.0培训心得,教育信息化20培训心得体会
- springboot 项目启动报错 url' attribute is not specified and no embedded datasource could be configured
- Excel中如何连续使用格式刷