一个没有' if '的Spring++框架就是这么'肝 '

开发 前端
人生没有 i f,所以这个框架来了,一直有写个框架的想法(每个程序员都有的冲动)!怎么也磨灭不了,这次终于得以了了心愿!肝了几天,熬了几夜,也算是没有白熬夜!!!

人生没有 i f,所以这个框架来了,一直有写个框架的想法(每个程序员都有的冲动)!怎么也磨灭不了,这次终于得以了了心愿!肝了几天,熬了几夜,也算是没有白熬夜!!!

首先先介绍一下这个框架的用途(先别吐槽,或许真的不是重复造轮子,技术这东西学到手,才是核心技术,否则被卡脖子——蹭个热度)

直接入主题吧!这也是打工人必备的品质!

简述:一个类似spring+spingBoot+mybatis 可耦可拆 的一个web 框架

主要涉及的技术有

  • cglib 动态代理
  • gradle 构建工具
  • netty
  • 各种设计模式 责任链 工程 模板 策略
  • 没有用" i f "
  • 只用lambda

具体实现的功能或者思想有

  1. ioc di 也就是大名鼎鼎的 依赖注入
  2. aop 面向切面编程
  3. mybatis 动态代理
  4. 拦截器
  5. 过滤器
  6. 循环依赖 区别于spring 的实现

说实话这是个面向函数式编程的工程,需要有函数式编程基础这也是重点

虽说还有些bug不过已经是好的开始!!!期待你的建议

先来个启动类

是的这和SpringBoot的启动是一样的简洁,也是我的追求

主体代码本着论文的思路先摘要

逻辑那不是一般的清晰 关键还有注释 或许我就是适合这样清晰代码

扯扯设计想法

注释很清晰

逻辑也很清晰 主要也是为了需要的人更好的理解 学的东西太多 可是都是一知半解

面向对象设计

考虑前期的不足也尽量预留后期升级 或许这就是如果(" i f ")

方便dug

简单说就是每次方法总会返回下一次运行的条件,中间没有缓存,目的为了更好的去理解框架本身。

没有冗长的调用链(那 谁谁的框架源码堆栈一堆)你的痛我懂的

注解都是你所熟悉的

正如开始类至简,所有的注解都是和Spring或者SpringBoot 或者Mybatis一致方便你理解源码

  1. Load.XXYY(arg) 

发现没有好多方法都是这个开头的 是的它再说 加载啦....加载啦.....加载啦....

喜欢这种 见名知意

不足的注释会定期增加的

有兴趣一起开荒 也许走着走着 就会跑了 跑了跑了 就会✈✈✈ 思想开小差啦 欢迎加入

不友好的地方

  • lambda 函数式编程不好调试
  • gradle 目前还不是web端的主流构建工具 可能跟着SpringBoot新版的路子啦停不下来了

下面是目录结构

  • hioove-core是框架的核心代码
  • hioove-demo是框架的测试代码

详细的还是看代码再说啦

粗看看核心代码

 

  1. /** 
  2.  * hioove 核心实现 
  3.  */ 
  4. public class Load { 
  5.     final static Logger log = LoggerFactory.getLogger(Load.class); 
  6.  
  7.  
  8.     // 1. 加载启动类 分析启动类注解 
  9.     public static StartClassInfo startClass(Class<?> app, String[] args) { 
  10.         // 1.加载主程序入口的注解 
  11.         Annotation[] appAnnotations = app.getAnnotations(); 
  12.         // 判断是否具备启动的必要条件 
  13.         assert Arrays.stream(app.getAnnotations()).map(Annotation::annotationType).collect(Collectors.toList()).stream().anyMatch(clazz -> clazz.equals(SpringBootApplication.class)); 
  14.         return Builder.of(StartClassInfo::new) 
  15.                 .with(StartClassInfo::setAnnotations, Arrays.asList(app.getAnnotations())) 
  16.                 .with(StartClassInfo::setStartClass, app) 
  17.                 .build(); 
  18.     } 
  19.  
  20.     // 2. 加载配置信息 
  21.     public static ConfigInfor configInfo(StartClassInfo startClassConfig, String... args) { 
  22.         // 系统信息加载 
  23.         Properties systemEnv = System.getProperties(); 
  24.         Properties defaultInfo = new Properties(); 
  25.         try { 
  26.             defaultInfo.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(Commons.DEFAULT_INFO)); 
  27.         } catch (IOException e) { 
  28.             log.error("", e); 
  29.         } 
  30.         Yaml yaml = new Yaml(); 
  31.         InputStream resourceAsStream = Load.class.getClassLoader().getResourceAsStream(String.valueOf(defaultInfo.get("sql.path"))); 
  32.         LinkedHashMap sqlSupport = Objects.isNull(resourceAsStream) ? null : yaml.load(resourceAsStream); 
  33.         return Builder.of(ConfigInfor::new) 
  34.                 .with(ConfigInfor::setSystemEnv, new HashMap<>(systemEnv)) 
  35.                 .with(ConfigInfor::setDefaultInfo, new HashMap<>(defaultInfo)) 
  36.                 .with(ConfigInfor::setSqlInfo, sqlSupport) 
  37.                 .build(); 
  38.     } 
  39.  
  40.     // 3. 扫描待实例化的对象 将要实例化 
  41.     public static Map<String, ClassSupport> scanObjectsToBeInstantiated(StartClassInfo startClassInfo, ConfigInfor configInfor) { 
  42.         // 1.加载依赖了时是否具备条件 是否包含注解 EnableAutoConfiguration 
  43.         assert startClassInfo.getAnnotations().stream().map(Annotation::annotationType).collect(Collectors.toList()).stream().anyMatch(clazz -> clazz.equals(EnableAutoConfiguration.class)); 
  44.         // 2.扫描待实例化的对象 
  45.         String appPath = startClassInfo.getStartClass().getResource(Commons.NULL_CHARACTER).getFile(); 
  46.         File appfile = new File(appPath); 
  47.         String absolutePath = appfile.getAbsolutePath().replace(File.separator, Commons.DOT); 
  48.         String name = Commons.DOT + startClassInfo.getStartClass().getPackage().getName(); 
  49.         String uselessPrefix = absolutePath.substring(0, absolutePath.length() - name.length() + 1); 
  50.         ArrayList<File> fileCache = Builder.of(ArrayList<File>::new).build(); 
  51.         Arrays.stream(Objects.requireNonNull(appfile.listFiles())).forEach(f -> FileUtils.findClassFiles(f, fileCache, new FilenameFilter() { 
  52.             @Override 
  53.             public boolean accept(File dir, String name) { 
  54.                 return true
  55.             } 
  56.         })); 
  57.         List<String> collect = startClassInfo.getAnnotations().stream().filter(annotation -> annotation instanceof SpringBootApplication).map(annotation -> { 
  58.             return Arrays.asList(((SpringBootApplication) annotation).scanBasePackages()); 
  59.         }).flatMap(Collection::stream).collect(Collectors.toList()); 
  60.         // 3.过滤掉包含$的类名 
  61.         return fileCache.stream().filter(file -> !file.getAbsolutePath().contains(Commons.MONEY)).map(file -> { 
  62.             String classOriginalPath = file.getAbsolutePath().substring(uselessPrefix.length()); 
  63.             return Builder.of(ClassSupport::new) 
  64.                     .with(ClassSupport::setName, classOriginalPath.replace(File.separator, Commons.DOT).replace(Commons.CLASS_SUFFIX, Commons.NULL_CHARACTER)) 
  65.                     .with(ClassSupport::setFile, file) 
  66.                     .build(); 
  67.         }).collect(Collectors.toMap(ClassSupport::getName, Function.identity())); 
  68.     } 
  69.  
  70.     // 4. 加载配置类(主要  涉及带有配置类注解的类实例化 。eg. Component.class Bean.class (只涉及Bean的实例化) 
  71.     public static ConfigInstance configObj(StartClassInfo startClassInfo, 
  72.                                            ConfigInfor configInfor, Map<String, ClassSupport> tobeInstance) { 
  73.         // 所有的被扫描类进行分析 并生成 MethodSupport.class 
  74.         Map<String, ClassSupport> configurationClass = tobeInstance.keySet().stream().map(classReference -> { 
  75.             // 1.加载待实例化的类进行分析  a.注解 
  76.             Class<?> currentClass = ClassUtils.load(classReference); 
  77.             ClassSupport classSupport = tobeInstance.get(classReference); 
  78.             // 2.获取类上的方法和注解 
  79.             Map<String, MethodSupport> methodAnnotation = Arrays.stream(Objects.requireNonNull(currentClass).getDeclaredMethods()).map(method -> { 
  80.                 return Builder.of(MethodSupport::new) 
  81.                         .with(MethodSupport::setName, method.getName()) 
  82.                         .with(MethodSupport::setAnnotations, Arrays.asList(method.getAnnotations())) 
  83.                         .with(MethodSupport::setParamTypes, Arrays.asList(method.getParameterTypes())) 
  84.                         .with(MethodSupport::setMethod, method) 
  85.                         .with(MethodSupport::setClassSupport, classSupport) 
  86.                         .build(); 
  87.             }).collect(Collectors.toMap(methodSupport -> methodSupport.getName() + UUID.randomUUID(), Function.identity())); 
  88.             // 2.获取类上的属性和注解 
  89.             Map<String, FieldSupport> fieldAnnotation = Arrays.stream(Objects.requireNonNull(currentClass).getDeclaredFields()).map(field -> { 
  90.                 return Builder.of(FieldSupport::new) 
  91.                         .with(FieldSupport::setName, field.getName()) 
  92.                         .with(FieldSupport::setAnnotations, Arrays.asList(field.getAnnotations())) 
  93.                         .with(FieldSupport::setFieldType, field.getType()) 
  94.                         .with(FieldSupport::setField, field) 
  95.                         .with(FieldSupport::setClassSupport, classSupport) 
  96.                         .build(); 
  97.             }).collect(Collectors.toMap(fieldSupport -> fieldSupport.getName() + UUID.randomUUID(), Function.identity())); 
  98.             // 当前类 
  99.             Builder.of(classSupport) 
  100.                     .with(ClassSupport::setClazz, currentClass) 
  101.                     .with(ClassSupport::setAnnotations, Arrays.asList(Objects.requireNonNull(currentClass).getAnnotations())) 
  102.                     .with(ClassSupport::setMethodSupport, methodAnnotation) 
  103.                     .with(ClassSupport::setFieldSupport, fieldAnnotation) 
  104.                     .with(ClassSupport::setSuperClazz, ClassUtils.allSuperclass(currentClass)) 
  105.                     .with(ClassSupport::setInterfaces, Arrays.asList(currentClass.getInterfaces())) 
  106.                     .build(); 
  107.             return classSupport; 
  108.             // 3.过滤出类上包含Component 注解的类 
  109.         }).filter(classSupport -> { 
  110.             return classSupport.getAnnotations().stream() 
  111.                     .anyMatch(annotation -> { 
  112.                         return annotation instanceof Component || annotation instanceof Configuration; 
  113.                     }); 
  114.         }).collect(Collectors.toMap(ClassSupport::getName, Function.identity())); 
  115.         // 填充子类类型 
  116.         tobeInstance.values().forEach(classSupport -> { 
  117.             ArrayList<Class<?>> subclass = tobeInstance.values().stream().filter(clazzConfig -> { 
  118.                 return clazzConfig.getInterfaces().stream().anyMatch(interFace -> { 
  119.                     return interFace.equals(classSupport.getClazz()); 
  120.                 }); 
  121.             }).map(ClassSupport::getClazz).collect(Collectors.toCollection(ArrayList<Class<?>>::new)); 
  122.             classSupport.setSubClazz(subclass); 
  123.         }); 
  124.  
  125.  
  126.         //todo 4.开始实例化配置类 
  127.         Map<String, Object> beanCalss = configurationClass.values().stream().map(classSupport -> { 
  128.             // 5.查找存在Bean注解的 
  129.             Object componentInstance = null
  130.             try { 
  131.                 componentInstance = classSupport.getClazz().newInstance(); 
  132.             } catch (Exception e) { 
  133.                 e.printStackTrace(); 
  134.             } 
  135.             Object copyComponentInstance = componentInstance; 
  136.             // 直接保存 
  137.             // 6.实例化方法上定义的Bean注解 
  138.             return classSupport.getMethodSupport().values().stream().map(methodSupport -> { 
  139.                 // 7.对方法体的每个注解检索查看是否存在Bean 
  140.                 // 只是针对带有Bean 注解开始实例话 
  141.                 // 获取到当前的方法 
  142.                 return methodSupport.getAnnotations().stream().filter(annotation -> annotation instanceof Bean).map(annotation -> { 
  143.                     Object configInstance = null
  144.                     Map<String, Object> collect1 = null
  145.                     Method method = methodSupport.getMethod(); 
  146.                     try { 
  147.                         configInstance = method.invoke(copyComponentInstance, new Object[]{}); 
  148.                         // 如果未定义Bean.value 则这个类的检索名称时方法名称MethodName 
  149.                         // 如果Bean上面定义多个 value e.g @Bean(value={“v1,v2”}) 则会创建多个在缓存中创建多个key 指向同一个类 
  150.                         Object copyConfigInstance = configInstance; 
  151.                         collect1 = Arrays.stream(Optional.of(((Bean) annotation).value()).filter(strings -> strings.length != 0).orElse(new String[]{methodSupport.getName()})) 
  152.                                 .collect(HashMap<String, Object>::new, (hashMap, s) -> hashMap.put(s, copyConfigInstance), HashMap::putAll); 
  153.                     } catch (Exception e) { 
  154.                         log.debug("{} {} Cannot be instantiated!!!", classSupport.getClazz(), method.getName()); 
  155.                     } 
  156.                     return collect1; 
  157.                 }).collect(HashMap<String, Object>::new, HashMap::putAll, HashMap::putAll); 
  158.             }).collect(HashMap<String, Object>::new, HashMap::putAll, HashMap::putAll); 
  159.         }).collect(HashMap<String, Object>::new, HashMap::putAll, HashMap::putAll); 
  160.  
  161.         //todo 针对aop 处理 
  162.         Builder<MethodEnhance> methodEnhanceBuilder = Builder.of(MethodEnhance::new); 
  163.         // 
  164.         List<MethodSupport> collect = tobeInstance.values().stream().filter(classSupport -> { 
  165.             return classSupport.getAnnotations().stream().anyMatch(annotation -> { 
  166.                 return annotation instanceof Aspect; 
  167.             }); 
  168.         }).map(classSupport -> { 
  169.             Object aspectInstanc = null
  170.             // 实例化 拦截配置类 
  171.             try { 
  172.                 aspectInstanc = Objects.requireNonNull(ClassUtils.create(classSupport.getName())).newInstance(); 
  173.             } catch (Exception e) { 
  174.                 log.debug("Error creating aop object!!!"); 
  175.             } 
  176.             Object copyAspectInstanc = aspectInstanc; 
  177.             return classSupport.getMethodSupport().values().stream().filter(methodSupport -> { 
  178.                 return methodSupport.getAnnotations().stream().anyMatch(annotation -> { 
  179.                     // 只考虑 包含 After.class Before.class 注解的方法体 
  180.                     return annotation instanceof After || annotation instanceof Before || annotation instanceof Pointcut 
  181.                             || annotation instanceof AfterReturning || annotation instanceof AfterThrowing || annotation instanceof Around; 
  182.                 }); 
  183.             }).map(methodSupport -> { 
  184.                 return 
  185.                         Builder.of(methodSupport).with(MethodSupport::setInstance, copyAspectInstanc).build(); 
  186.             }).peek(methodSupport -> { 
  187.                 methodSupport.getAnnotations().stream().filter(annotation -> annotation instanceof Pointcut).forEach(annotation -> { 
  188.                     methodEnhanceBuilder.with(MethodEnhance::addPointcut, methodSupport); 
  189.                 }); 
  190.                 methodSupport.getAnnotations().stream().filter(annotation -> annotation instanceof After).forEach(annotation -> { 
  191.                     methodEnhanceBuilder.with(MethodEnhance::addAfter, methodSupport); 
  192.                 }); 
  193.                 methodSupport.getAnnotations().stream().filter(annotation -> annotation instanceof Before).forEach(annotation -> { 
  194.                     methodEnhanceBuilder.with(MethodEnhance::addBefore, methodSupport); 
  195.                 }); 
  196.                 methodSupport.getAnnotations().stream().filter(annotation -> annotation instanceof AfterReturning).forEach(annotation -> { 
  197.                     methodEnhanceBuilder.with(MethodEnhance::addAfterReturning, methodSupport); 
  198.                 }); 
  199.                 methodSupport.getAnnotations().stream().filter(annotation -> annotation instanceof AfterThrowing).forEach(annotation -> { 
  200.                     methodEnhanceBuilder.with(MethodEnhance::addAfterThrowing, methodSupport); 
  201.                 }); 
  202.                 methodSupport.getAnnotations().stream().filter(annotation -> annotation instanceof Around).forEach(annotation -> { 
  203.                     methodEnhanceBuilder.with(MethodEnhance::addAround, methodSupport); 
  204.                 }); 
  205.             }).collect(Collectors.toCollection(ArrayList<MethodSupport>::new)); 
  206.         }).flatMap(Collection::stream).collect(Collectors.toList()); 
  207.         //todo 针对Filter 处理 
  208.         Builder<FilterChain> filterchain = Builder.of(FilterChain::new); 
  209.         List<MethodSupport> copyMethodFilterBuilder = tobeInstance.values().stream().filter(classSupport -> { 
  210.             return classSupport.getAnnotations().stream().anyMatch(annotation -> annotation instanceof Filter); 
  211.         }).map(classSupport -> { 
  212.             Object aspectInstanc = null
  213.             // 实例化 拦截配置类 
  214.             try { 
  215.                 aspectInstanc = Objects.requireNonNull(ClassUtils.create(classSupport.getName())).newInstance(); 
  216.             } catch (Exception e) { 
  217.                 log.debug("Error creating Filter object!!!"); 
  218.             } 
  219.             Object copyAspectInstanc = aspectInstanc; 
  220.             return classSupport.getMethodSupport().values().stream().map(methodSupport -> { 
  221.                 filterchain.with(FilterChain::addFilters, methodSupport); 
  222.                 return Builder.of(methodSupport).with(MethodSupport::setInstance, copyAspectInstanc).build(); 
  223.             }).collect(Collectors.toCollection(ArrayList<MethodSupport>::new)); 
  224.         }).flatMap(Collection::stream).collect(Collectors.toList()); 
  225.         return Builder.of(ConfigInstance::new) 
  226.                 .with(ConfigInstance::addAnnotationInstance, Aspect.class, methodEnhanceBuilder.build()) 
  227.                 .with(ConfigInstance::addAnnotationInstance, Filter.class, filterchain.build()) 
  228.                 .with(ConfigInstance::addAnnotationInstance, Bean.class, beanCalss).build(); 
  229.     } 
  230.  
  231.     // 5. 初步实例化类对象 完成类增强 eg. RestController.class  Service.class  Repository.class (只涉及Bean的实例化 包括增强类) 
  232.     public static Map<Class<?>, Object> mvcObj(StartClassInfo startClassInfo, ConfigInfor configInfor, Map<String, ClassSupport> tobeInstance, ConfigInstance configInstance) { 
  233.         // 取出所有的路由信息 
  234.         tobeInstance.values().stream().filter(classSupport -> { 
  235.             return classSupport.getAnnotations().stream().anyMatch(annotation -> { 
  236.                 return annotation instanceof RestController; 
  237.             }); 
  238.         }).map(classSupport -> { 
  239.             ArrayList<RouteSupport> routeInfors = classSupport.getMethodSupport().values().stream().filter(methodSupport -> methodSupport.getAnnotations().stream().anyMatch(annotation -> annotation instanceof RequestMapping)) 
  240.                     .map(methodSupport -> { 
  241.                         return Builder.of(RouteSupport::new).with(RouteSupport::addRoute, ((RequestMapping) methodSupport.getMethod().getAnnotation(RequestMapping.class)).value(), methodSupport).build(); 
  242.                     }).collect(Collectors.toCollection(ArrayList<RouteSupport>::new)); 
  243.             classSupport.setRoutInfors(routeInfors); 
  244.             return routeInfors; 
  245.         }).flatMap(Collection::stream).collect(Collectors.toList()); 
  246.  
  247.         // 找出框架管理的类进行实例化 
  248.         return tobeInstance.values().stream().filter(classSupport -> { 
  249.             return classSupport.getAnnotations().stream().anyMatch(annotation -> { 
  250.                         return annotation instanceof RestController || annotation instanceof Service || annotation instanceof Repository; 
  251.                     } 
  252.             ); 
  253.         }).filter(classSupport -> !classSupport.getClazz().isInterface()).map(classSupport -> { 
  254.             MethodEnhance annotationInstanceObj = (MethodEnhance) configInstance.getAnnotationInstance().get(Aspect.class); 
  255.             // 面向接口编程 可以选择实现逻辑 
  256.             Proxy proxy = Builder.of(Proxy.INSTANCE) 
  257.                     .with(Proxy::setBefore, annotationInstanceObj.getBefore()) 
  258.                     .with(Proxy::setAfter, annotationInstanceObj.getAfter()) 
  259.                     .with(Proxy::setPointcut, annotationInstanceObj.getPointcut()) 
  260.                     .build(); 
  261.             Object proxyObj = proxy.create(classSupport.getClazz()); 
  262.             return Builder.of(HashMap<Class<?>, Object>::new).with(HashMap::put, classSupport.getClazz(), proxyObj).build(); 
  263.         }).collect(HashMap<Class<?>, Object>::new, HashMap::putAll, HashMap::putAll); 
  264.     } 
  265.  
  266.     // 6. 注入实例见对象 完成最终实例化 
  267.     public static AttributeInjection resolveInterdependence(StartClassInfo startClassInfo, ConfigInfor 
  268.             configInfo, Map<String, ClassSupport> tobeObj, ConfigInstance configObj, Map<Class<?>, Object> mvcObj) { 
  269.         // 到这里已经实例化的有   configurationObj 里面含 Bean.class  Aspect.class 注解 都初步实例化 
  270.         // 到这里已经实例化的有   instantiateClassObject 里面含 RestController.class  Service.class  Repository.class 注解 都初步实例化 
  271.         // 先获取所有的带有Autowired.class Resource.class类属性 
  272.         Collection<Object> values = configObj.getAnnotationInstance().values(); 
  273.         Builder<AttributeInjection> attributeInjectionBuilder = Builder.of(AttributeInjection::new); 
  274.         List<FieldSupport> collect = tobeObj.values().stream().map(classSupport -> { 
  275.             return classSupport.getFieldSupport().values().stream().filter(fieldSupport -> { 
  276.                 return fieldSupport.getAnnotations().stream().anyMatch(annotation -> { 
  277.                     return annotation instanceof Autowired || annotation instanceof Resource; 
  278.                 }); 
  279.             }).map(fieldSupport -> { 
  280.                 // 需要考虑 configurationObj instantiateClassObject 未完成注入的情况 
  281.                 Object currentObj = mvcObj.get(classSupport.getClazz()); 
  282.                 return 
  283.                         Builder.of(fieldSupport).with(FieldSupport::setInstance, currentObj).build(); 
  284.             }).peek(fieldSupport -> { 
  285.                 fieldSupport.getAnnotations().stream().filter(annotation -> annotation instanceof Autowired).forEach(annotation -> { 
  286.                     attributeInjectionBuilder.with(AttributeInjection::addToBeInjected, Autowired.class, fieldSupport); 
  287.                 }); 
  288.                 fieldSupport.getAnnotations().stream().filter(annotation -> annotation instanceof Resource).forEach(annotation -> { 
  289.                     attributeInjectionBuilder.with(AttributeInjection::addToBeInjected, Resource.class, fieldSupport); 
  290.                 }); 
  291.                 fieldSupport.getAnnotations().stream().filter(annotation -> annotation instanceof Value).forEach(annotation -> { 
  292.                     attributeInjectionBuilder.with(AttributeInjection::addToBeInjected, Value.class, fieldSupport); 
  293.                 }); 
  294.             }).collect(Collectors.toCollection(ArrayList<FieldSupport>::new)); 
  295.         }).flatMap(Collection::stream).collect(Collectors.toList()); 
  296.         AttributeInjection attributeInjection = attributeInjectionBuilder.build(); 
  297.         // 存储注入的对象 
  298.         HashMap<Class<?>, Object> newInjectedObject = Builder.of(HashMap<Class<?>, Object>::new).build(); 
  299.         // 保存初次实例化对象 
  300.         HashMap<Class<?>, Object> initInstance = Builder.of(HashMap<Class<?>, Object>::new).build(); 
  301.         // 保存初次实例化对象拷贝 针对非接口型参数注入 
  302.         HashMap<Object, Object> copyInitInstance = Optional.ofNullable(attributeInjection).orElseGet( 
  303.                 AttributeInjection::new).getToBeInjected().values().stream().map(fieldSupports -> { 
  304.             return fieldSupports.stream().filter(fieldSupport -> !fieldSupport.getFieldType().isInterface()).collect(Collectors.toList()); 
  305.         }).map(fieldSupports -> { 
  306.             return fieldSupports.stream().map(fieldSupport -> { 
  307.                 // 开启对私有变量进行注入 
  308.                 fieldSupport.getField().setAccessible(true); 
  309.                 ClassSupport classSupport = fieldSupport.getClassSupport(); 
  310.                 Class<?> currentClass = classSupport.getClazz(); 
  311.  
  312.                 //TODO 针对属性带有 Autowired.class 注解处理 
  313.                 fieldSupport.getAnnotations().stream().filter(annotation -> { 
  314.                     return annotation instanceof Autowired; 
  315.                 }).forEach(annotation -> {// 这里没有拆解是为了方便理解 
  316.                     // 针对属性进行注入 
  317.                     Object currentObj = fieldSupport.getInstance(); 
  318.                     try { 
  319.                         fieldSupport.getField().set(currentObj, Analysis.getClassByType(fieldSupport.getFieldType(), tobeObj, configObj, mvcObj, newInjectedObject)); 
  320.                     } catch (IllegalAccessException e) { 
  321.                         log.error("", e); 
  322.                     } 
  323.                     fieldSupport.setInstance(currentObj); 
  324.                     Builder.of(initInstance).with(HashMap::put, currentClass, currentObj).build(); 
  325.                 }); 
  326.                 //TODO 针对属性带有 Resource.class 注解处理 
  327.                 fieldSupport.getAnnotations().stream().filter(annotation -> { 
  328.                     return annotation instanceof Resource; 
  329.                 }).forEach(annotation -> { 
  330.                     // 针对属性进行注入 
  331.                     Object currentObj = fieldSupport.getInstance(); 
  332.                     try { 
  333.                         fieldSupport.getField().set(currentObj, Analysis.getClassByType(fieldSupport.getFieldType(), tobeObj, configObj, mvcObj, newInjectedObject)); 
  334.                     } catch (IllegalAccessException e) { 
  335.                         log.error("", e); 
  336.                     } 
  337.                     fieldSupport.setInstance(currentObj); 
  338.                     Builder.of(initInstance).with(HashMap::put, currentClass, currentObj).build(); 
  339.                 }); 
  340.                 //TODO 针对属性带有 Resource.class 注解处理 
  341.                 fieldSupport.getAnnotations().stream().filter(annotation -> { 
  342.                     return annotation instanceof Value; 
  343.                 }).forEach(annotation -> { 
  344.                     // 针对属性进行注入 
  345.                     Object currentObj = fieldSupport.getInstance(); 
  346.                     try { 
  347.                         fieldSupport.getField().set(currentObj, Analysis.getClassByType(fieldSupport.getFieldType(), tobeObj, configObj, mvcObj, newInjectedObject)); 
  348.                     } catch (IllegalAccessException e) { 
  349.                         log.error("", e); 
  350.                     } 
  351.                     fieldSupport.setInstance(currentObj); 
  352.                     Builder.of(initInstance).with(HashMap::put, currentClass, currentObj).build(); 
  353.                 }); 
  354.                 return initInstance; 
  355.             }).collect(HashMap::new, HashMap::putAll, HashMap::putAll); 
  356.         }).collect(HashMap::new, HashMap::putAll, HashMap::putAll); 
  357.  
  358.         // 接口型参数注入 这里主要涉及Mybatis模拟 
  359.         // 保存初次实例化对象 
  360.         HashMap<Class<?>, Object> initInterfaceInstance = Builder.of(HashMap<Class<?>, Object>::new).build(); 
  361.         HashMap<Class<?>, Object> copyInitInterfaceInstance = Optional.ofNullable(attributeInjection).orElseGet( 
  362.                 AttributeInjection::new).getToBeInjected().values().stream().map(fieldSupports -> { 
  363.             return fieldSupports.stream().filter(fieldSupport -> fieldSupport.getFieldType().isInterface()).collect(Collectors.toList()); 
  364.         }).map(fieldSupports -> { 
  365.             return fieldSupports.stream().map(fieldSupport -> { 
  366.                 // 开启对私有变量进行注入 
  367.                 fieldSupport.getField().setAccessible(true); 
  368.                 ClassSupport classSupport = fieldSupport.getClassSupport(); 
  369.                 Class<?> currentClass = classSupport.getClazz(); 
  370.                 //TODO 针对属性带有 Autowired.class 注解处理 
  371.                 fieldSupport.getAnnotations().stream().filter(annotation -> { 
  372.                     return annotation instanceof Autowired; 
  373.                 }).forEach(annotation -> {// 这里没有拆解是为了方便理解 
  374.                     // 针对属性进行注入 
  375.                     Object currentObj = fieldSupport.getInstance(); 
  376.                     try { 
  377.                         fieldSupport.getField().set(currentObj, Analysis.getMapperClassByType(fieldSupport.getFieldType(), configInfo, newInjectedObject)); 
  378.                     } catch (IllegalAccessException e) { 
  379.                         log.error("", e); 
  380.                     } 
  381.                     fieldSupport.setInstance(currentObj); 
  382.                     Builder.of(initInterfaceInstance).with(HashMap::put, currentClass, currentObj).build(); 
  383.                 }); 
  384.                 //TODO 针对属性带有 Resource.class 注解处理 
  385.                 fieldSupport.getAnnotations().stream().filter(annotation -> { 
  386.                     return annotation instanceof Resource; 
  387.                 }).forEach(annotation -> { 
  388.                     // 针对属性进行注入 
  389.                     Object currentObj = fieldSupport.getInstance(); 
  390.                     try { 
  391.                         fieldSupport.getField().set(currentObj, Analysis.getMapperClassByType(fieldSupport.getFieldType(), configInfo, newInjectedObject)); 
  392.                     } catch (IllegalAccessException e) { 
  393.                         log.error("", e); 
  394.                     } 
  395.                     fieldSupport.setInstance(currentObj); 
  396.                     Builder.of(initInterfaceInstance).with(HashMap::put, currentClass, currentObj).build(); 
  397.                 }); 
  398.                 return initInterfaceInstance; 
  399.             }).collect(HashMap<Class<?>, Object>::new, HashMap::putAll, HashMap::putAll); 
  400.         }).collect(HashMap::new, HashMap::putAll, HashMap::putAll); 
  401.         initInstance.putAll(initInterfaceInstance); 
  402.         // 取出所有的RestController.class 
  403.         HashMap<Class<?>, Object> controllerClass = initInstance.entrySet().stream().filter(classObjectEntry -> !Objects.isNull(classObjectEntry.getKey().getAnnotation(RestController.class))) 
  404.                 .collect(HashMap<Class<?>, Object>::new, (hashMap, entry) -> hashMap.put(entry.getKey(), entry.getValue()), HashMap::putAll); 
  405.         return Builder.of(attributeInjection) 
  406.                 .with(AttributeInjection::addSingleton, AttributeInjection.class, initInstance) 
  407.                 .with(AttributeInjection::addSingleton, RestController.class, controllerClass) 
  408.                 .with(AttributeInjection::addSingleton, SqlInfo.class, newInjectedObject).build(); 
  409.     } 
  410.  
  411.     // 7. 初始化调度器  filter dispatch 
  412.     public static HashMap<String, MethodSupport> initDispatch(Map<String, ClassSupport> tobeInstance, AttributeInjection attributeInjection) { 
  413.         // 关联代理对象和路径 
  414.         HashMap<Class<?>, Object> controllerClass = (HashMap<Class<?>, Object>) attributeInjection.getSingleton().get(RestController.class); 
  415.         HashMap<Class<?>, ClassSupport> classToConfig = tobeInstance.values().stream().map(classSupport -> { 
  416.             return Builder.of(HashMap<Class<?>, ClassSupport>::new).with(HashMap::put, classSupport.getClazz(), classSupport).build(); 
  417.         }).collect(HashMap<Class<?>, ClassSupport>::new, HashMap::putAll, HashMap::putAll); 
  418.         HashMap<String, MethodSupport> routeInfos = controllerClass.keySet().stream().map(clazz -> { 
  419.             ClassSupport classSupport = classToConfig.get(clazz); 
  420.             // 地址和方法 
  421.             HashMap<String, MethodSupport> routeInfo = classSupport.getRoutInfors().stream().map(RouteSupport::getRoute).collect(HashMap<String, MethodSupport>::new, HashMap::putAll, HashMap::putAll); 
  422.             routeInfo.values().forEach(methodSupport -> { 
  423.                 // 把最终的代理对象插入进去 
  424.                 methodSupport.setInstance(controllerClass.get(clazz)); 
  425.             }); 
  426.             return routeInfo; 
  427.         }).collect(HashMap<String, MethodSupport>::new, HashMap::putAll, HashMap::putAll); 
  428.         return routeInfos; 
  429.     } 
  430.  
  431.     // 8. 初始化过滤器和  filter 
  432.     public static FilterChain initFilterAnd(ConfigInstance configObj) { 
  433.         return (FilterChain) configObj.getAnnotationInstance().get(Filter.class); 
  434.     } 

 

责任编辑:未丽燕 来源: 今日头条
相关推荐

2017-11-06 16:30:33

开源

2013-06-24 10:12:27

Jego中国移动Just Easy G

2023-08-21 08:01:03

2021-02-26 10:21:35

比特币投资金融

2021-05-06 10:52:09

Java Spring Bo框架

2019-12-26 08:33:11

Filnkjava语言

2020-02-22 21:51:43

程序员Microsoft SServerSQL

2012-05-30 09:40:55

Linux锅炉

2011-03-24 09:34:41

SPRING

2020-06-17 11:42:50

异常解析器Spring MVC

2009-04-09 08:59:33

Windows 7微软操作系统

2015-09-10 14:40:32

大数据神奇

2020-07-09 14:57:24

HTTP浏览器协议

2017-02-24 13:20:13

搜索引擎数据结构架构

2020-04-20 10:47:57

Redis数据开发

2009-10-01 09:19:45

PHP框架ZendFramewoCake

2021-06-08 07:32:01

框架Mock测试

2018-08-15 10:51:01

JavaSpring MVC框架

2023-10-18 15:25:29

数据源数据库

2014-09-05 09:37:06

开源
点赞
收藏

51CTO技术栈公众号