模仿spring-aop的功能,利用注解搭建自己的框架。
入JAVA坑7月有余,也尝试自己手动搭建框架,最近对spring aop的这种切面很着迷,为此记录下自己目前搭出来的小小的demo,后续有时间也会继续改进自己的demo。望大神们不吝赐教。
主要还是运用反射和java自带的代理类。理论知识就不说了,因为我目前也不是很清楚,避免误导,还是避而不谈吧。好了,直接根据代码撸吧。
结构:

接口
Person.java
public interface Person {
void say();
}
接口实现类
Man.java
public class Man implements Person {
@Override
public void say() {
System.out.println("男人say:....");
}
}
自定义注解
@interface WaterAOP
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Documented
public @interface WaterAOP {
enum METHOD{before,after,afterthrowing}
METHOD method() default METHOD.after;
String Name() default "类全名";
}
自定义注解类
WaterLog.java
public class WaterLog {
@WaterAOP(Name = "com.water.aop.attempt3.Man",method = WaterAOP.METHOD.after)
public void afterAction(){
System.out.println("后置行为");
}
@WaterAOP(Name = "com.water.aop.attempt3.Man",method = WaterAOP.METHOD.before)
public void beforeAction(){
System.out.println("前置行为");
}
}
实现自定义代理类(就是在
Proxy.newProxyInstance()方法的第三个参数里做手脚。用了java8的lambda表达式。
)
ProxyFactory.java
public class ProxyFactory {
// 维持一个实现接口的被代理的对象,后面改为对象组,由浅入深
private Person person;
private WaterLog waterLog;
private Method beforeMethod=null,afterMethod=null;
public ProxyFactory(Person person,WaterLog waterLog){
this.person=person;
this.waterLog=waterLog;
}
public Object getProxyInstance(){
return Proxy.newProxyInstance(
person.getClass().getClassLoader(),
person.getClass().getInterfaces(),
// 第一个参数就是代理者,如果你想对代理者做一些操作可以使用这个参数;
// 第二个就是被执行的方法,
// 第三个是执行该方法所需的参数。
(Object proxyObj, Method method,Object[] args)->{
//如果没有传入aop 直接返回空
if(waterLog==null){
return null;
}
Class aop=waterLog.getClass();
Class c = person.getClass();
// 获取aop类的方法的注解并赋给自定义的一些变量,下面根据这些变量是否有值来确定是否有注解
getAnnotation(aop,c);
if(beforeMethod!=null){
beforeMethod.invoke(waterLog);
}
// 代理对象执行方法并且获得返回值
Object returnValue=method.invoke(person,args);
if(afterMethod!=null){
afterMethod.invoke(waterLog);
}
return returnValue;
}
);
}
private void getAnnotation(Class aop,Class proxy){
//如果有AOP的类
if(waterLog!=null){
// 获取切面类所有的方法
Method[] methodsAOP=aop.getMethods();
// 如果切入的日志类的方法不为空
if(methodsAOP!=null){
for(Method logMethod:methodsAOP){
// 取得WaterLog类的方法上WaterAOP注解
WaterAOP waterAOP=logMethod.getAnnotation(WaterAOP.class);
if(waterAOP!=null) {
// 如果AOP上的注解与传入的类名一致
if (proxy.toString().substring(6).equals(waterAOP.Name())) {
if (waterAOP.method() == WaterAOP.METHOD.before) {
// 赋值 ,后面再执行
beforeMethod=logMethod;
}else if(waterAOP.method() == WaterAOP.METHOD.after){
afterMethod=logMethod;
}
}
}
}
}
}
}
}
测试类
Test.java (junit是个测试包,也可以直接用main方法)
public class Test {
@org.junit.Test
public void waterAOP(){
Person person=new Man();
Person proxyPerson=(Person) new ProxyFactory(person,new WaterLog()).getProxyInstance();
proxyPerson.say();
}
}
大致的流程就是:传入要被代理的类和自定义的注解类,运用反射获取注解类里方法上的注解属性的值,然后进行比对,再进行相应的操作。
模仿spring-aop的功能,利用注解搭建自己的框架。的更多相关文章
- Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解
引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...
- Spring AOP配置简单记录(注解及xml配置方式)
在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...
- Spring AOP 实现功能权限校验功能
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 使用拦截器实现未登录时跳转到登录界面的功能 1 拦截器SecurityInterceptor 2spring-mvcxml拦 ...
- Spring AOP(5)-- 注解
applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...
- 【Spring AOP】Spring AOP之如何通过注解的方式实现各种通知类型的AOP操作进阶篇(3)
一.切入点表达式的各种类型 切入点表达式的作用:限制连接点的匹配(满足时对应的aspect方法会被执行) 1)execution:用于匹配方法执行连接点.Spring AOP用户可能最经常使用exec ...
- spring AOP (使用AspectJ的注解方式 的aop实现) (6)
目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...
- 循序渐进之Spring AOP(6) - 使用@Aspect注解
前面几节的示例看起来让人沮丧,要记忆如此多的接口.类和继承关系,做各种复杂的配置.好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精 ...
- Spring AOP 和 动态代理技术
AOP 是什么东西 首先来说 AOP 并不是 Spring 框架的核心技术之一,AOP 全称 Aspect Orient Programming,即面向切面的编程.其要解决的问题就是在不改变源代码的情 ...
- Spring基础知识之基于注解的AOP
背景概念: 1)横切关注点:散布在应用中多处的功能称为横切关注点 2)通知(Advice):切面完成的工作.通知定了了切面是什么及何时调用. 5中可以应用的通知: 前置通知(Before):在目标方法 ...
随机推荐
- 洛谷 P1019 单词接龙【经典DFS,温习搜索】
P1019 单词接龙 题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在 ...
- DFS+打表
N皇后问题 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- B. Secret Combination
B. Secret Combination time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Dora.Interception, 一个为.NET Core度身打造的AOP框架[3]:Interceptor的注册
在<不一样的Interceptor>中我们着重介绍了Dora.Interception中最为核心的对象Interceptor,以及定义Interceptor类型的一些约定.由于Interc ...
- java包装类简析
对于8个基本类型,java提供了他们相应的包装类: 基本类型 包装类 byte java.lang.Byte short java.lang.Short int java.lang.Integer l ...
- windows server 2008使用nginx转发API异常解决办法
公司比较传统,一直使用的JSP做项目,没有遇到过跨域问题. 最近因为公司接到一个微信spa项目,因为考虑到项目需要调用老接口,斗胆选择nginx(1.12.1)做接口转发服务, 开发环境使用的win1 ...
- 本地phpstudy时常停机连接失败,php.ini文件中9000端口问题
2018/01/05 13:35:07 [error] 20508#19380: *1 WSARecv() failed (10054: An existing connection was forc ...
- 查看php的配置文件Php.ini的位置
标签:php服务器 浏览器 配置文件 Linux local 近来,有不博友问php.ini存在哪个目录下?或者修改php.ini以后为何没有生效?基于以上两个问题,我觉得有必要教一下刚接触PHP的博 ...
- input标签元素,value属性取值问题,赋值
验证val:<input type="text" id="id" name="name" value="空值"&g ...
- Mac 下 搭建 svn 服务器
Mac自带了svn服务端和客户端,所以只需要简单配置一下就可以使用. 1.创建svn repository svnadmin create /Users/gaohf/svn/repository 2. ...