Spring中的AOP实现思路
AOP是面向切面编程,为什么在切面中写一个注解方法@Before,这个方法会在目标方法前面执行呢
基于JDK动态代理实现上面说的情况
自定义注解
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface ExtBefore {
String value() default "";
}
切面类,里面定义了一个注解方法
public class Aspect {
@ExtBefore
public void before() {
System.out.println("beforeMethod ..... ");
}
}
测试类
private static Object getProxy(final Object target , Object aspect)
这个方法传入的是要代理的目标类,和切面类。这样就可以在目标方法前和后执行我们在切面类加了注解的方法
public class ProxyTest {
public static void main(String[] args) {
List target = new ArrayList<String>();
Collection proxy = (Collection)getProxy(target, new Aspect());
System.out.println(proxy.add("A"));
}
private static Object getProxy(final Object target , Object aspect) {
Object proxy = Proxy.newProxyInstance(target.getClass().getClassLoader(),
new Class[]{Collection.class}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//调用切面中before方法
Class<?> clazz = aspect.getClass();
Method[] methods = clazz.getDeclaredMethods();
for(Method m : methods) {
if(m.getDeclaredAnnotation(ExtBefore.class) != null) {
m.invoke(aspect);
}
}
//目标方法
Object retVal = method.invoke(target, args);
return retVal;
}
});
return proxy;
}
}
Spring中的AOP实现思路的更多相关文章
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- Spring中关于AOP的实践之概念
一.什么是AOP AOP:也称作面向切面编程 在分享几个概念执行我想先举个栗子(可能例子举得并不是特别恰当): 1.假如路人A走在大街上,被一群坏人绑架了: 2.警察叔叔接到报警迅速展开行动:收集情报 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...
- JavaWeb_(Spring框架)认识Spring中的aop
1.aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切 2.Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他 ...
- (五)Spring 中的 aop
目录 文章目录 AOP概念 AOP原理 AOP术语 **`Spring`** 中的 **`aop`** 的操作 使用 `AspectJ` 实现 `aop` 的两种方式 AOP概念 浅理解 aop :面 ...
- Spring 中基于 AOP 的 @AspectJ
Spring 中基于 AOP 的 @AspectJ @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格. 通过在你的基于架构的 XML ...
随机推荐
- Vue --- 指令练习
scores = [ { name: 'Bob', math: 97, chinese: 89, english: 67 }, { name: 'Tom', math: 67, chinese: 52 ...
- windows客户端
- 1-移远GSM/GPRS M26 模块 Mini板 开发板(使用说明)
板子预览 引脚说明 供电 关于串口电压匹配引脚: 上面一版朋友测试反应的问题 (上面的内容不删除,因为已经出售了1套) 1,源码开发完以后,烧录完成 PWRKEY按键不能使用了,需要断电上电,那么就需 ...
- 【后缀数组】【LuoguP2852】 [USACO06DEC]牛奶模式Milk Patterns
题目链接 题目描述 农夫John发现他的奶牛产奶的质量一直在变动.经过细致的调查,他发现:虽然他不能预见明天产奶的质量,但连续的若干天的质量有很多重叠.我们称之为一个"模式". J ...
- CSS3 之loading动画实现思路
效果大致如下: 主要实现方式: 该效果主要用到animation-timing-function中的steps()函数,该函数主要用于分步隐藏不同模块. 实现思路: 第一步动画: 第二步动画: 第三步 ...
- SpringSecurity的简单入门
以下是大体思路 1.导入坐标 <properties> <spring.version>4.2.4.RELEASE</spring.version> </pr ...
- DATEADD (Transact-SQL)
DATEADD (Transact-SQL) This function adds a specified number value (as a signed integer) to a specif ...
- 关于资源获取(请把https改为http)
所有demo以及资源获取,请把https改为http.
- vs2015编译OBS-Studio21.1.12
原文地址:http://www.freesion.com/article/37445100/ 参考:https://blog.csdn.net/su_vast/article/details/7498 ...
- [转]小D课堂 - 零基础入门SpringBoot2.X到实战_汇总
原文地址:https://www.cnblogs.com/wangjunwei/p/11392825.html 第1节零基础快速入门SpringBoot2.0 小D课堂 - 零基础入门SpringBo ...