Spring源码窥探之:AOP注解
AOP也就是我们日常说的@面向切面编程,看概念比较晦涩难懂,难懂的是设计理念,以及这样设计的好处是什么。在Spring的AOP中,常用的几个注解如下:@Aspect,@Before,@After,@AfterReturning,@AfterThrowing,@Around,@PointCut以及@EnableAspectJAutoProxy
1. 模拟业务类CalculateService
/**
* description:模拟业务类
*
* @author 70KG
* @date 2018/12/17
*/
public class CalculateService { public int calculate(int i, int j) {
int result = i / j;
System.out.println("---->CalculateService-业务方法执行。。。。。。");
return result;
} }
2. 编写切面类,切面类必须标注它为切面类(@Aspect),关于切入点表达式,可以参考官方文档,写法很多种,还有关于JoinPoint这个参数必须放在第一位,否则报错。
/**
* description:日志切面类,JoinPoint必须在参数的第一位
*
* @author 70KG
* @date 2018/12/17
*/
@Aspect // ---> 声明此类是一个切面类
public class LogAdvice { @Pointcut("execution(* com.nmys.story.springCore.springaop.sample01.CalculateService.*(..))")
public void pointCut() {
} /**
* 目标方法执行前执行
*/
@Before("pointCut()")
public void logBefore(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
Object[] args = joinPoint.getArgs();
System.out.println("---->" + signature + "方法执行前,传入的参数是:" + Arrays.asList(args));
} /**
* 目标方法执行后执行
*/
@After("pointCut()")
public void logAfter(JoinPoint joinPoint) {
Signature signature = joinPoint.getSignature();
Object[] args = joinPoint.getArgs();
System.out.println("---->" + signature + "方法执行后,传入的参数是:" + Arrays.asList(args)); } /**
* 方法发生异常时执行
*/
@AfterThrowing(value = "pointCut()", throwing = "ex")
public void logException(JoinPoint joinPoint, Exception ex) {
Signature signature = joinPoint.getSignature();
System.out.println("---->" + signature + "方法执行后,抛出了异常信息:" + ex.getMessage());
} /**
* 正常返回时执行
*/
@AfterReturning(value = "pointCut()", returning = "result")
public void logReturn(JoinPoint joinPoint, Object result) {
Signature signature = joinPoint.getSignature();
System.out.println("---->" + signature + "方法执行后,返回的结果是:" + result);
} }
3. 配置类
/**
* description
*
* @author 70KG
* @date 2018/12/17
*/
@Configuration
@EnableAspectJAutoProxy // ---> 开启切面的自动代理
public class MyConfig { @Bean
public CalculateService calculateService() {
return new CalculateService();
} // 将切面类也交给容器管理
@Bean
public LogAdvice logAdvice() {
return new LogAdvice();
} }
4. 测试类
/**
* description
*
* @author 70KG
* @date 2018/12/17
*/
public class Test01 { public static void main(String[] args) { // 加载配置文件
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(MyConfig.class);
CalculateService calc = (CalculateService) ac.getBean("calculateService");
calc.calculate(4,2);
} }
5. 结果
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行前,传入的参数是:[4, 2]
---->CalculateService-业务方法执行。。。。。。
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,传入的参数是:[4, 2]
---->int com.nmys.story.springCore.springaop.sample01.CalculateService.calculate(int,int)方法执行后,返回的结果是:2
Spring源码窥探之:AOP注解的更多相关文章
- Spring源码窥探之:注解方式的AOP原理
AOP入口代码分析 通过注解的方式来实现AOP1. @EnableAspectJAutoProxy通过@Import注解向容器中注入了AspectJAutoProxyRegistrar这个类,而它在容 ...
- Spring源码分析之AOP从解析到调用
正文: 在上一篇,我们对IOC核心部分流程已经分析完毕,相信小伙伴们有所收获,从这一篇开始,我们将会踏上新的旅程,即Spring的另一核心:AOP! 首先,为了让大家能更有效的理解AOP,先带大家过一 ...
- Spring源码情操陶冶-AOP之Advice通知类解析与使用
阅读本文请先稍微浏览下上篇文章Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器,本文则对aop模式的通知类作简单的分析 入口 根据前文讲解,我们知道通知类的 ...
- spring源码分析(二)Aop
创建日期:2016.08.19 修改日期:2016.08.20-2016.08.21 交流QQ:992591601 参考资料:<spring源码深度解析>.<spring技术内幕&g ...
- Spring源码情操陶冶-AOP之ConfigBeanDefinitionParser解析器
aop-Aspect Oriented Programming,面向切面编程.根据百度百科的解释,其通过预编译方式和运行期动态代理实现程序功能的一种技术.主要目的是为了程序间的解耦,常用于日志记录.事 ...
- spring源码学习之AOP(一)
继续源码学习,看了spring中基础的容器和AOP感觉自己也没有什么长进,哈哈,我也不知道到底有用没有,这可能是培养自己的一种精神吧,不管那么多,继续学习!AOP中 AOP中几个重要的概念:(1)Ad ...
- Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器
本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析 源码概览 对BeanDefinitionParser接口的 ...
- spring源码学习之AOP(二)
接着上一篇中的内容! 3.创建代理 在获取了所有的bean对应的增强器之后,便可以进行代理的创建了org.springframework.aop.framework.autoproxy包下的Abstr ...
- Spring源码分析-从@ComponentScan注解配置包扫描路径到IoC容器中的BeanDefinition,经历了什么(一)?
阅前提醒 全文较长,建议沉下心来慢慢阅读,最好是打开Idea,点开Spring源码,跟着下文一步一步阅读,更加便于理解.由于笔者水平优先,编写时间仓促,文中难免会出现一些错误或者不准确的地方,恳请各位 ...
- Spring源码情操陶冶-PropertyPlaceholderBeanDefinitionParser注解配置解析器
本文针对spring配置的context:property-placeholder作下简单的分析,承接前文Spring源码情操陶冶-自定义节点的解析 spring配置文件应用 <context: ...
随机推荐
- myeclipse的ctrl+f搜索面板功能详解
1.查找/替换方向:Direction Forward:向前 Backward:向后 2.范围:Scope All:全部(当前文件) Selected lines:选中的几行 3.选项:Options ...
- git对某个文件取消跟踪
git rm --cached readme1.txt 删除readme1.txt的跟踪,并保留在本地. git rm --f readme1.txt 删除readme1.txt的跟踪,并 ...
- Redis初识01 (简介、安装、使用)
一.Reids介绍 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...
- python_进程池以及线程池
可以重复利用的线程 直接上代码 from threading import Thread, current_thread from queue import Queue # 重写线程类 class M ...
- [cf 1236 E] Alice and the Unfair Game
题意: 给定一个长度为m的序列$A$,你有一个长度为n的棋盘,可以任选一个位置x作为起点. 在时刻$[1,m+1]$你可以向左或向右移动一格. 设时刻i你移动后所在的位置为$B_i$,你需要满足对于任 ...
- Drools入门
文章转载自:http://cwqcwq.iteye.com/blog/397869 一.背景知识: 1.什么是规则引擎 Java规则引擎起源于基于规则的专家系统,而基于规则的专家系统又是专家系统的 ...
- redis三种集群策略
主从复制 主数据库可以进行读写操作,当读写操作导致数据变化时会自动将数据同步给从数据库 从数据库一般都是只读的,并且接收主数据库同步过来的数据 一个master可以拥有多个slave,但是一个slav ...
- 1、C#多线程基础理论
系统为应用程序分配所需的内存以及其他资源,内存和资源的物理分离叫做进程. 进程是以线程为单位竞争CPU,那么什么是线程呢? 线程可看成一个可执行的指令单元,他使用进程中的数据,包含若干条指令,进程 ...
- easy ui 弹框叠加问题
1.框架用的是.net MVC,Index页面如下所示: @{ Layout = "~/Views/Shared/_CustomerLayout.cshtml"; ViewBag. ...
- python爬虫-房天下-登录
房天下-登录 本次爬取的网址为:https://passport.fang.com 一.分析请求 输入用户名和密码,点击登录按钮 请求的参数为: uid: 123456789 pwd: 64ccd42 ...