入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的功能,利用注解搭建自己的框架。的更多相关文章

  1. Spring AOP中使用@Aspect注解 面向切面实现日志横切功能详解

    引言: AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一 ...

  2. Spring AOP配置简单记录(注解及xml配置方式)

    在了解spring aop中的关键字(如:连接点(JoinPoint).切入点(PointCut).切面(Aspact).织入(Weaving).通知(Advice).目标(Target)等)后进行了 ...

  3. Spring AOP 实现功能权限校验功能

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 使用拦截器实现未登录时跳转到登录界面的功能 1 拦截器SecurityInterceptor 2spring-mvcxml拦 ...

  4. Spring AOP(5)-- 注解

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...

  5. 【Spring AOP】Spring AOP之如何通过注解的方式实现各种通知类型的AOP操作进阶篇(3)

    一.切入点表达式的各种类型 切入点表达式的作用:限制连接点的匹配(满足时对应的aspect方法会被执行) 1)execution:用于匹配方法执行连接点.Spring AOP用户可能最经常使用exec ...

  6. spring AOP (使用AspectJ的注解方式 的aop实现) (6)

    目录 一.在 Spring 中启用 AspectJ 注解支持 二.AspectJ 支持 5 种类型的通知注解: 2.1.使用之前的 计算器接口和实现类 ArithmeticCalculator.jav ...

  7. 循序渐进之Spring AOP(6) - 使用@Aspect注解

    前面几节的示例看起来让人沮丧,要记忆如此多的接口.类和继承关系,做各种复杂的配置.好在这些只是一种相对过时的实现方式,现在只需要使用@Aspect注解及表达式就可以轻松的使用POJO来定义切面,设计精 ...

  8. Spring AOP 和 动态代理技术

    AOP 是什么东西 首先来说 AOP 并不是 Spring 框架的核心技术之一,AOP 全称 Aspect Orient Programming,即面向切面的编程.其要解决的问题就是在不改变源代码的情 ...

  9. Spring基础知识之基于注解的AOP

    背景概念: 1)横切关注点:散布在应用中多处的功能称为横切关注点 2)通知(Advice):切面完成的工作.通知定了了切面是什么及何时调用. 5中可以应用的通知: 前置通知(Before):在目标方法 ...

随机推荐

  1. django图书管理半成品(MySQL)

    本次需要用到MySQL数据库,所以先配置数据库,在seeting文件中配置: 数据库第一次使用需要配置: python manage.py makemigrations #生成配置文件 python ...

  2. AtCoder Grand Contest 013

    这场打得蛮菜的,很晚才出BC,还一堆罚时…… A - Sorted Arrays 题目大意:将给定数列划分成单调不增或单调不减的区间,求最少区间数. 贪心即可. #include<cstdio& ...

  3. POJ1635:Subway tree systems

    链接:http://poj.org/problem?id=1635 填坑树同构 题目给出的是除根外的括号序列表示. 其实只要跟你说hash大家都能写得出来…… hash函数取个效果别太差的就行了吧 # ...

  4. hdu_2670Girl Love Value(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2670 Girl Love Value Time Limit: 2000/1000 MS (Java/O ...

  5. mysql习题

    如图表创建数据库. create table class( cid int auto_increment primary key, caption ) )engine=innodb default c ...

  6. redis学习笔记(14)---redis基本命令总结

    http://doc.redisfans.com/ 网页,对所有redis命令的用法与示例进行了详细的描述 概述 Redis的键值可以使用物种数据类型:字符串,散列表,列表,集合,有序集合.本文详细介 ...

  7. Typescript学习笔记

    什么是 TypeScript TypeScript 是 JavaScript 的类型的超集,它可以编译成纯 JavaScript. 安装 TypeScript 命令行工具安装: npm install ...

  8. 将js进行到底:node学习笔记2

    node重要API之FS--CLI编程初体验 所谓的"fs"就是file system! 当下几乎任何一门编程语言都会提供对文件系统读写的API,比如c语言的open()函数. 而 ...

  9. 数据库连接池(c3p0)

    (一)问题的提出: 在使用开发基于数据库的web程序时,传统的数据库使用模式按照以下步骤: 在程序中建立数据库连接 进行sql操作 断开数据库连接 但是,这种模式存在着移动的问题: 传统连接模式每次向 ...

  10. eclipse导入包之后中文乱码

    windows ->preferences  ->workspace -> default ->GBK