[Spring] 如何实现一个低配版`Spring BeanFactory`?

2023-12-22 01:38

本文主要是介绍[Spring] 如何实现一个低配版`Spring BeanFactory`?,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

如何实现一个低配版Spring BeanFactory

目录

  • 如何实现一个低配版`Spring BeanFactory`?
    • 准备工作
    • 核心源码
    • 效果
    • 总结
    • 更多

手机用户请横屏获取最佳阅读体验,REFERENCES中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。

平台地址
CSDNhttps://blog.csdn.net/sinat_28690417
简书https://www.jianshu.com/u/3032cc862300
个人博客https://yiyuery.club

结合Spring BeanFactory实例扫描和注入思想进行深入编码实战:工厂化管理运行中实例对象

准备工作

  • 包扫描工具类定义
/** @ProjectName: 编程学习* @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.* @address:     http://xiazhaoyang.tech* @date:        2019/5/20 20:57* @email:       https:yiyuery.github.io/NoteBooks* @description: 本内容仅限于编程技术学习使用,转发请注明出处.*/
package com.example.swagger.common.component;import lombok.extern.slf4j.Slf4j;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;/*** <p>*	扫描指定路径下class* </p>** @author xiazhaoyang* @version v1.0.0* @date 2019-06-05 22:46* @modificationHistory=========================逻辑或功能性重大变更记录* @modify By: {修改人} 2019-06-05* @modify reason: {方法名}:{原因}* ...*/
@Slf4j
public class ClasspathPackageScanner {private String basePackage;private ClassLoader cl;/*** Construct an instance and specify the base package it should scan.* @param basePackage The base package to scan.*/public ClasspathPackageScanner(String basePackage) {this.basePackage = basePackage;this.cl = getClass().getClassLoader();}/*** Construct an instance with base package and class loader.* @param basePackage The base package to scan.* @param cl Use this class load to locate the package.*/public ClasspathPackageScanner(String basePackage, ClassLoader cl) {this.basePackage = basePackage;this.cl = cl;}/*** Get all fully qualified names located in the specified package* and its sub-package.** @return A list of fully qualified names.* @throws IOException*/public List<String> getFullyQualifiedClassNameList() throws IOException {log.info("Start Scan...", basePackage);return doScan(basePackage, new ArrayList<>());}/*** Actually perform the scanning procedure.** @param basePackage* @param nameList A list to contain the result.* @return A list of fully qualified names.** @throws IOException*/private List<String> doScan(String basePackage, List<String> nameList) throws IOException {// replace dots with splashesString splashPath = StringUtil.dotToSplash(basePackage);// get file pathURL url = cl.getResource(splashPath);String filePath = StringUtil.getRootPath(url);// Get classes in that package.// If the web server unzips the jar file, then the classes will exist in the form of// normal file in the directory.// If the web server does not unzip the jar file, then classes will exist in jar file.// contains the name of the class file. e.g., Apple.class will be stored as "Apple"List<String> names = null;if (isJarFile(filePath)) {// jar fileif (log.isDebugEnabled()) {log.debug("{} fina a jar file", filePath);}names = readFromJarFile(filePath, splashPath);} else {// directoryif (log.isDebugEnabled()) {log.debug("{} find a directory", filePath);}names = readFromDirectory(filePath);}for (String name : names) {if (isClassFile(name)) {nameList.add(toFullyQualifiedName(name, basePackage));} else {// this is a directory// check this directory for more classes// do recursive invocationdoScan(basePackage + "." + name, nameList);}}if (log.isDebugEnabled()) {for (String n : nameList) {log.debug("load {}", n);}}return nameList;}/*** Convert short class name to fully qualified name.* e.g., String -> java.lang.String*/private String toFullyQualifiedName(String shortName, String basePackage) {StringBuilder sb = new StringBuilder(basePackage);sb.append('.');sb.append(StringUtil.trimExtension(shortName));return sb.toString();}private List<String> readFromJarFile(String jarPath, String splashedPackageName) throws IOException {if (log.isDebugEnabled()) {log.debug("从JAR包中读取类: {}", jarPath);}JarInputStream jarIn = new JarInputStream(new FileInputStream(jarPath));JarEntry entry = jarIn.getNextJarEntry();List<String> nameList = new ArrayList<>();while (null != entry) {String name = entry.getName();if (name.startsWith(splashedPackageName) && isClassFile(name)) {nameList.add(name);}entry = jarIn.getNextJarEntry();}return nameList;}private List<String> readFromDirectory(String path) {File file = new File(path);String[] names = file.list();if (null == names) {return null;}return Arrays.asList(names);}private boolean isClassFile(String name) {return name.endsWith(".class");}private boolean isJarFile(String name) {return name.endsWith(".jar");}/*** For test purpose.*/public static void main(String[] args) throws Exception {ClasspathPackageScanner scan = new ClasspathPackageScanner("com.example.swagger");scan.getFullyQualifiedClassNameList();}
}class StringUtil{private StringUtil() {}/*** "file:/home/whf/cn/fh" -> "/home/whf/cn/fh"* "jar:file:/home/whf/foo.jar!cn/fh" -> "/home/whf/foo.jar"*/public static String getRootPath(URL url) {String fileUrl = url.getFile();int pos = fileUrl.indexOf('!');if (-1 == pos) {return fileUrl;}return fileUrl.substring(5, pos);}/*** "cn.fh.lightning" -> "cn/fh/lightning"* @param name* @return*/public static String dotToSplash(String name) {return name.replaceAll("\\.", "/");}/*** "Apple.class" -> "Apple"*/public static String trimExtension(String name) {int pos = name.indexOf('.');if (-1 != pos) {return name.substring(0, pos);}return name;}/*** /application/home -> /home* @param uri* @return*/public static String trimURI(String uri) {String trimmed = uri.substring(1);int splashIndex = trimmed.indexOf('/');return trimmed.substring(splashIndex);}
}
  • 自动注入注解定义AutoRegister: 类似于Spring@Component注解
/** @ProjectName: 编程学习* @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.* @address:     http://xiazhaoyang.tech* @date:        2019/5/20 20:57* @email:       https:yiyuery.github.io/NoteBooks* @description: 本内容仅限于编程技术学习使用,转发请注明出处.*/
package com.example.swagger.api;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** <p>*	自动注入注解定义`AutoRegister`: 类似于`Spring`的`@Component`注解* </p>** @author xiazhaoyang* @version v1.0.0* @date 2019-06-05 21:46* @modificationHistory=========================逻辑或功能性重大变更记录* @modify By: {修改人} 2019-06-05* @modify reason: {方法名}:{原因}* ...*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface AutoRegister {String name() default "";
}
  • 配置类属性填充注解定义PropNameSpace ,读取Properties前缀自动注入
/** @ProjectName: 编程学习* @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.* @address:     http://xiazhaoyang.tech* @date:        2019/5/20 20:57* @email:       https:yiyuery.github.io/NoteBooks* @description: 本内容仅限于编程技术学习使用,转发请注明出处.*/
package com.example.swagger.api;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;/*** <p>*	配置类属性填充注解定义`PropNameSpace` ,读取`Properties`前缀自动注入* </p>** @author xiazhaoyang* @version v1.0.0* @date 2019-06-05 21:43* @modificationHistory=========================逻辑或功能性重大变更记录* @modify By: {修改人} 2019-06-05* @modify reason: {方法名}:{原因}* ...*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface PropNameSpace {/*** 入参前缀* @return*/String prefix() default "";
}
  • 实例工厂定义
/** @ProjectName: 编程学习* @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.* @address:     http://xiazhaoyang.tech* @date:        2019/5/20 20:57* @email:       https:yiyuery.github.io/NoteBooks* @description: 本内容仅限于编程技术学习使用,转发请注明出处.*/
package com.example.swagger.common.context;import java.util.HashMap;
import java.util.Map;/*** <p>** </p>** @author xiazhaoyang* @version v1.0.0* @date 2019-06-05 22:45* @modificationHistory=========================逻辑或功能性重大变更记录* @modify By: {修改人} 2019-06-05* @modify reason: {方法名}:{原因}* ...*/
public class AppBeanContext {private static class SingletonHolder {private static final AppBeanContext INSTANCE = new AppBeanContext();}public static final AppBeanContext getInstance() {return SingletonHolder.INSTANCE;}private static final Map<String, Object> HOLDER = new HashMap<>();/*** 注册实例* @param name* @param bean*/public void registerBean(String name, Object bean) {if (HOLDER.containsKey(name)) {throw new IllegalStateException("bean repetition register!");}HOLDER.putIfAbsent(name, bean);}/*** 获取实例* @param name* @param clazz* @param <T>* @return*/public <T> T getBean(String name, Class<T> clazz) {if (!HOLDER.containsKey(name)) {throw new IllegalArgumentException("bean repetition register!");}return clazz.cast(HOLDER.get(name));}
}

思路

  • 首先通过指定package路径下的class文件扫描
  • 然后通过自定义注解完成,判断是否需要自动注入。
  • 如果含有AutoRegister注解,则通过反射生成实例并存放到实例工厂中
  • 如果含有PropNameSpace注解,则自动读取yaml文件中的属性来进行填充

核心源码

  • 启动初始化类
/** @ProjectName: 编程学习* @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.* @address:     https:yiyuery.club* @date:        2019/5/20 20:57* @email:       xiazhaoyang@live.com* @description: 本内容仅限于编程技术学习使用,转发请注明出处.*/
package com.example.swagger.common.component;import com.example.swagger.api.AutoRegister;
import com.example.swagger.api.ITransformPlugin;
import com.example.swagger.api.PropNameSpace;
import com.example.swagger.common.base.AbstractOfficeTransformPlugin;
import com.example.swagger.common.base.AbstractSwaggerTransformPlugin;
import com.example.swagger.common.configuration.ApplicationYamlLoader;
import com.example.swagger.common.configuration.OfficeTransformConfig;
import com.example.swagger.common.context.AppBeanContext;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;import java.lang.reflect.Field;
import java.util.List;/*** <p>*  启动初始化类* </p>** @author xiazhaoyang* @version v1.0.0* @date 2019-06-07 05:33* @modificationHistory=========================逻辑或功能性重大变更记录* @modify By: {修改人} 2019-06-07* @modify reason: {方法名}:{原因}* ...*/
@Slf4j
public class AppStarterInitial {static {log.info("start init...");beforeStart();log.info("end init...");}private static void beforeStart() {try {ClasspathPackageScanner scan = new ClasspathPackageScanner("com.example.swagger");List<String> classNames = scan.getFullyQualifiedClassNameList();for (String clazz : classNames) {Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(clazz);if (c.getAnnotations().length > 0) {//获取自动注入对象if (c.getAnnotationsByType(AutoRegister.class).length > 0) {Object bean = c.newInstance();//遍历属性中所有FieldString prefixKey = "";if (c.getAnnotationsByType(PropNameSpace.class).length > 0) {PropNameSpace[] annotationsByType = c.getAnnotationsByType(PropNameSpace.class);prefixKey = annotationsByType[0].prefix();}Field[] declaredFields = c.getDeclaredFields();for (Field field : declaredFields) {field.setAccessible(Boolean.TRUE);String filedKey = field.getAnnotationsByType(JsonProperty.class).length > 0 ? field.getAnnotationsByType(JsonProperty.class)[0].value() : field.getName();String propKey = prefixKey + "." + filedKey;field.set(bean, ApplicationYamlLoader.getPropsByKey(propKey,field.getType()));}String beanName = c.getAnnotationsByType(AutoRegister.class)[0].name();AppBeanContext.getInstance().registerBean(StringUtils.isNotBlank(beanName)?beanName: StringUtils.lowerCase(c.getSimpleName()).substring(0,1)+c.getSimpleName().substring(1),bean);}}}} catch (Throwable e) {log.error("init error", e);}}
}

这个主要为了完成实例自动扫描和注入、参数配置类自动扫描填充参数的功能。

项目启动类集成这个类即可在实例化之前触发对应实例并注入到实例工厂中以备使用。

/** @ProjectName: 编程学习* @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.* @address:     http://xiazhaoyang.tech* @date:        2019/5/20 20:57* @email:       https:yiyuery.github.io/NoteBooks* @description: 本内容仅限于编程技术学习使用,转发请注明出处.*/
package com.example.swagger;import com.example.swagger.common.component.AppStarterInitial;
import com.example.swagger.common.enums.FileTransformEnum;
import com.example.swagger.common.enums.SwaggerGenEnum;/*** <p>*  启动类* </p>** @author xiazhaoyang* @version v1.0.0* @date 2019-06-05 22:57* @modificationHistory=========================逻辑或功能性重大变更记录* @modify By: {修改人} 2019-06-05* @modify reason: {方法名}:{原因}* ...*/
public class SwaggerTransformApplication extends AppStarterInitial {public static void main(String[] args){//TODO ...}}

效果

如此一来,我们的低配版Spring BeanFactory就可以看到如下的效果了。

在这里插入图片描述

图片中打印的这个配置类:

/** @ProjectName: 编程学习* @Copyright:   2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.* @address:     http://xiazhaoyang.tech* @date:        2019/5/20 20:57* @email:       https:yiyuery.github.io/NoteBooks* @description: 本内容仅限于编程技术学习使用,转发请注明出处.*/
package com.example.swagger.common.configuration;import com.example.swagger.api.AutoRegister;
import com.example.swagger.api.PropNameSpace;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** <p>** </p>** @author xiazhaoyang* @version v1.0.0* @date 2019-06-05 21:50* @modificationHistory=========================逻辑或功能性重大变更记录* @modify By: {修改人} 2019-06-05* @modify reason: {方法名}:{原因}* ...*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@AutoRegister
@PropNameSpace(prefix = "enhance.config")
public class SwaggerEnhanceConfig {@JsonProperty("api-docs-url")private String apiDocsUrl;
}

yaml:

enhance:config:api-docs-url: http://localhost:9000/swagger-resources/v2/api-docs?group=UI

通过图片我们可以看到,已经成功实现了配置类实例的内部参数填充了。

总结

本文通过自定义注解和反射模拟了Spring BeanFactory的一个简单的实例动态注入和管理功能。有兴趣的小伙伴可以深入了解下Spring的三大核心思想:IOC(控制反转),DI(依赖注入),AOP(面向切面编程)。

更多

扫码关注“架构探险之道”,获取更多源码和文章资源

在这里插入图片描述

知识星球(扫码加入获取源码和文章资源链接)

在这里插入图片描述

这篇关于[Spring] 如何实现一个低配版`Spring BeanFactory`?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/522194

相关文章

C#高效实现在Word文档中自动化创建图表的可视化方案

《C#高效实现在Word文档中自动化创建图表的可视化方案》本文将深入探讨如何利用C#,结合一款功能强大的第三方库,实现在Word文档中自动化创建图表,为你的数据呈现和报告生成提供一套实用且高效的解决方... 目录Word文档图表自动化:为什么选择C#?从零开始:C#实现Word文档图表的基本步骤深度优化:C

SpringBoot整合AOP及使用案例实战

《SpringBoot整合AOP及使用案例实战》本文详细介绍了SpringAOP中的切入点表达式,重点讲解了execution表达式的语法和用法,通过案例实战,展示了AOP的基本使用、结合自定义注解以... 目录一、 引入依赖二、切入点表达式详解三、案例实战1. AOP基本使用2. AOP结合自定义注解3.

nginx跨域访问配置的几种方法实现

《nginx跨域访问配置的几种方法实现》本文详细介绍了Nginx跨域配置方法,包括基本配置、只允许指定域名、携带Cookie的跨域、动态设置允许的Origin、支持不同路径的跨域控制、静态资源跨域以及... 目录一、基本跨域配置二、只允许指定域名跨域三、完整示例四、配置后重载 nginx五、注意事项六、支持

Qt实现对Word网页的读取功能

《Qt实现对Word网页的读取功能》文章介绍了几种在Qt中实现Word文档(.docx/.doc)读写功能的方法,包括基于QAxObject的COM接口调用、DOCX模板替换及跨平台解决方案,重点讨论... 目录1. 核心实现方式2. 基于QAxObject的COM接口调用(Windows专用)2.1 环境

MySQL查看表的历史SQL的几种实现方法

《MySQL查看表的历史SQL的几种实现方法》:本文主要介绍多种查看MySQL表历史SQL的方法,包括通用查询日志、慢查询日志、performance_schema、binlog、第三方工具等,并... 目录mysql 查看某张表的历史SQL1.查看MySQL通用查询日志(需提前开启)2.查看慢查询日志3.

Java实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

使用Python实现局域网远程监控电脑屏幕的方法

《使用Python实现局域网远程监控电脑屏幕的方法》文章介绍了两种使用Python在局域网内实现远程监控电脑屏幕的方法,方法一使用mss和socket,方法二使用PyAutoGUI和Flask,每种方... 目录方法一:使用mss和socket实现屏幕共享服务端(被监控端)客户端(监控端)方法二:使用PyA

MyBatis-Plus逻辑删除实现过程

《MyBatis-Plus逻辑删除实现过程》本文介绍了MyBatis-Plus如何实现逻辑删除功能,包括自动填充字段、配置与实现步骤、常见应用场景,并展示了如何使用remove方法进行逻辑删除,逻辑删... 目录1. 逻辑删除的必要性编程1.1 逻辑删除的定义1.2 逻辑删php除的优点1.3 适用场景2.

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

C#借助Spire.XLS for .NET实现在Excel中添加文档属性

《C#借助Spire.XLSfor.NET实现在Excel中添加文档属性》在日常的数据处理和项目管理中,Excel文档扮演着举足轻重的角色,本文将深入探讨如何在C#中借助强大的第三方库Spire.... 目录为什么需要程序化添加Excel文档属性使用Spire.XLS for .NET库实现文档属性管理Sp