• 【手写模拟Spring底层原理】


    模拟Spring底层详解

    前置准备:创建部分注解,具体如下

    /**
     * 依赖注入注解信息
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public @interface Autowired {
    }
    
    /**
     * 自定义一个注解是为了标识==>使用此注解之处的类资源需要交给Spring容器管理
     * 可自定义beanName
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface Component {
        String value() default "";
    }
    
    /**
     * 定于扫描类(bean)资源路径
     */
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface ComponentScan {
        String value() default "";
    }
    
    
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface Scope {
        String value() default "";
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    两个service类,用于测试:

    @Component
    @Scope()
    public class OrderService {
    }
    
    • 1
    • 2
    • 3
    • 4
    @Component
    public class UserService {
    
        private String name;
    
        @Autowired
        private OrderService orderService;
    
        public void testDemo(){
            System.out.println("Spring 创建 userService 实例成功");
            System.out.println("Spring 依赖注入 orderService 实例对象:"+orderService);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    一个测试类:

    public class TestSpringDemo {
    
        public static void main(String[] args) throws Exception{
    
            LyfApplicationContext context = new LyfApplicationContext(AppConfig.class);
    
            UserService userService = (UserService) context.getBean("userService");
            userService.testDemo();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1、结合配置类,扫描类资源

    Spring在创建对象前,需要去扫描,确定需要交给Spring管理的类资源,具体的实现步骤模拟代码如下:

    1.1、创建需要扫描的配置类AppConfig,如下:

    
    /**
     * 这个类主要是用于定义扫描资源的路径信息
     */
    @ComponentScan("com.practice.service")
    public class AppConfig {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    1.2、创建Spring容器对象LyfApplicationContext,如下

    /**
     * 这个类主要是用于模拟Spring容器的(bean的创建与获取bean等)
     */
    public class LyfApplicationContext {
    
    	public LyfApplicationContext(Class config) throws Exception{
    		
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.3、Spring容器对象LyfApplicationContext扫描资源

    在LyfApplicationContext容器含参构造中,需要结合传入扫描资源的配置类AppConfig,对资源进行扫描,具体实现代码如下:

    /**
     * 这个类主要是用于模拟Spring容器的(bean的创建与获取bean等)
     */
    public class LyfApplicationContext {
    
    	public LyfApplicationContext(Class config) throws Exception{
    		
            //判断传入的config类上是否有componentScan注解
            if (config.isAnnotationPresent(ComponentScan.class)) {
                //1、获取传入类上的注解ComponentScan信息==>主要是获取扫描路径
                ComponentScan componentScanAnnotation = (ComponentScan) config.getAnnotation(ComponentScan.class);
    
                //2、获取注解中的值
                String path = componentScanAnnotation.value();
    
                //3、将注解中的值"."换为"/"
                path = path.replace(".","/");
    
                //4、结合当前容器的类加载器,加载路径path下的class资源
                //4.1 先获取当前容器的类加载器
                ClassLoader classLoader = LyfApplicationContext.class.getClassLoader();
    
                //4.2 利用上一步获取的类加载器获取path路径下的class文件资源url信息
                URL resource = classLoader.getResource(path); //D:/JavaWorkSpace/2023/spring/spring-study-demo01/target/classes/com/practice/service
    
                //4.3 获取当前resource路径下的文件资源信息
                File file = new File(resource.getFile());
    
                //4.4 遍历file文件数据,获取file下的所有class文件资源
                if (file.isDirectory()) {
                    for (File f : file.listFiles()) {
                        String absolutePath = f.getAbsolutePath(); //获取到class资源的绝对路径信息==>目的是为了加载此类信息
    
                        // 4.4.1 将此类的绝对路径做处理,截取一部分
                        absolutePath = absolutePath.substring(absolutePath.indexOf("com"),absolutePath.indexOf(".class")).replace("\\",".");//com.practice.service.UserService
    
                        //4.4.2 用上述得到的类加载器加载上述的absolutePath路径下的class资源信息==>目的是为了检查当前遍历的class资源上是否包含@Component注解
                        try{
                            Class<?> clazz = classLoader.loadClass(absolutePath);
    
                            // 如果当前的对象实现了beanPostProcessor接口,需要将其加入beanPostProcessorList集合中
                            if (BeanPostProcessor.class.isAssignableFrom(clazz)) {
                                BeanPostProcessor instance = (BeanPostProcessor) clazz.getConstructor().newInstance();
                                beanPostProcessorList.add(instance);
                            }
                            if (clazz.isAnnotationPresent(Component.class)) {
                                //创建一个BeanDefinition对象,用于保存每个类的特征
                                BeanDefinition beanDefinition = new BeanDefinition();
                                beanDefinition.setType(clazz);
    
                                //4.4.2.1 获取当前注解Component的值==>之定义的beanName
                                Component annotation = clazz.getAnnotation(Component.class);
                                String beanName = annotation.value();
    
                                //如果当前传入的beanName为空的话==>利用默认的,也就是类名首字母小写作为beanName
                                if ("".equals(beanName)) {
                                    beanName = Introspector.decapitalize(clazz.getSimpleName());
                                }
    
                                //4.4.2.2 除此之外,还需要判断是否有Scope注解===>主要是判断当前的bean是否是单例或者是原型的
                                if (clazz.isAnnotationPresent(Scope.class)) {
                                    //获取注解中的值
                                    Scope scopeAnnotation = clazz.getAnnotation(Scope.class);
                                    String value = scopeAnnotation.value();
                                    beanDefinition.setScope(value);
                                }else {
                                    beanDefinition.setScope("singleton");
                                }
                                //将封装好的beanDefinition缓存到Map中
                                beanDefinitionMap.put(beanName,beanDefinition);
                            }
                        }catch (Exception e){
                            throw new Exception(absolutePath + "类加载失败",e);
                        }
                    }
                }
    
            }else {
                throw new Exception("缺少路径资源配置信息~~~");
            }
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    其过程如下:
    先结合传入扫描资源的配置类AppConfig,类上是否包含注解@ComponentScan,若包含注解,需要获取其注解中的参数信息(配置的扫描包路径),获取当前资源的类加载器,目的是为了获取target包下的class资源信息,获取到指定包路径下的class资源,利用其构造方法,创建对象,对对象中的属性以及对象上加入的注解信息进行遍历扫描,进行相关的逻辑处理,将其类元信息加入到BeanDefinition对象中,再将其封装为一个Map对象,在接下来的对象创建与获取的过程中做好基奠,其对象信息就是记录每一个类的特征,部分代码如下

    
    /**
     * 这个类主要是去记录下描述一个bean的特征
     */
    public class BeanDefinition {
    
        //类的类型
        private Class type;
    
        //创建类的方式==>单例还是原型等
        private String scope;
    
        public Class getType() {
            return type;
        }
    
        public void setType(Class type) {
            this.type = type;
        }
    
        public String getScope() {
            return scope;
        }
    
        public void setScope(String scope) {
            this.scope = scope;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    2、结合上一步的扫描,遍历其Map集合,创建对象

    /**
     * 这个类主要是用于模拟Spring容器的(bean的创建与获取bean等)
     */
    public class LyfApplicationContext {
    
    	public LyfApplicationContext(Class config) throws Exception{
    		
            //判断传入的config类上是否有componentScan注解
            if (config.isAnnotationPresent(ComponentScan.class)) {
                //1、获取传入类上的注解ComponentScan信息==>主要是获取扫描路径
                ComponentScan componentScanAnnotation = (ComponentScan) config.getAnnotation(ComponentScan.class);
    
                //2、获取注解中的值
                String path = componentScanAnnotation.value();
    
                //3、将注解中的值"."换为"/"
                path = path.replace(".","/");
    
                //4、结合当前容器的类加载器,加载路径path下的class资源
                //4.1 先获取当前容器的类加载器
                ClassLoader classLoader = LyfApplicationContext.class.getClassLoader();
    
                //4.2 利用上一步获取的类加载器获取path路径下的class文件资源url信息
                URL resource = classLoader.getResource(path); //D:/JavaWorkSpace/2023/spring/spring-study-demo01/target/classes/com/practice/service
    
                //4.3 获取当前resource路径下的文件资源信息
                File file = new File(resource.getFile());
    
                //4.4 遍历file文件数据,获取file下的所有class文件资源
                if (file.isDirectory()) {
                    for (File f : file.listFiles()) {
                        String absolutePath = f.getAbsolutePath(); //获取到class资源的绝对路径信息==>目的是为了加载此类信息
    
                        // 4.4.1 将此类的绝对路径做处理,截取一部分
                        absolutePath = absolutePath.substring(absolutePath.indexOf("com"),absolutePath.indexOf(".class")).replace("\\",".");//com.practice.service.UserService
    
                        //4.4.2 用上述得到的类加载器加载上述的absolutePath路径下的class资源信息==>目的是为了检查当前遍历的class资源上是否包含@Component注解
                        try{
                            Class<?> clazz = classLoader.loadClass(absolutePath);
    
                            // 如果当前的对象实现了beanPostProcessor接口,需要将其加入beanPostProcessorList集合中
                            if (BeanPostProcessor.class.isAssignableFrom(clazz)) {
                                BeanPostProcessor instance = (BeanPostProcessor) clazz.getConstructor().newInstance();
                                beanPostProcessorList.add(instance);
                            }
                            if (clazz.isAnnotationPresent(Component.class)) {
                                //创建一个BeanDefinition对象,用于保存每个类的特征
                                BeanDefinition beanDefinition = new BeanDefinition();
                                beanDefinition.setType(clazz);
    
                                //4.4.2.1 获取当前注解Component的值==>之定义的beanName
                                Component annotation = clazz.getAnnotation(Component.class);
                                String beanName = annotation.value();
    
                                //如果当前传入的beanName为空的话==>利用默认的,也就是类名首字母小写作为beanName
                                if ("".equals(beanName)) {
                                    beanName = Introspector.decapitalize(clazz.getSimpleName());
                                }
    
                                //4.4.2.2 除此之外,还需要判断是否有Scope注解===>主要是判断当前的bean是否是单例或者是原型的
                                if (clazz.isAnnotationPresent(Scope.class)) {
                                    //获取注解中的值
                                    Scope scopeAnnotation = clazz.getAnnotation(Scope.class);
                                    String value = scopeAnnotation.value();
                                    beanDefinition.setScope(value);
                                }else {
                                    beanDefinition.setScope("singleton");
                                }
                                //将封装好的beanDefinition缓存到Map中
                                beanDefinitionMap.put(beanName,beanDefinition);
                            }
                        }catch (Exception e){
                            throw new Exception(absolutePath + "类加载失败",e);
                        }
                    }
                }
    
            }else {
                throw new Exception("缺少路径资源配置信息~~~");
            }
    	}
    	 //创建对象
    	for (Map.Entry<String, BeanDefinition> definitionEntry : beanDefinitionMap.entrySet()) {
               //获取BeanDefinitionMap中的key和value
               String beanName = definitionEntry.getKey();
               BeanDefinition definition = definitionEntry.getValue();
               //判断当前的BeanDefinition对象是否是单例
               if ("singleton".equals(definition.getScope())) {
                   Object bean = creatBean(beanName, definition);
                   singletonMap.put(beanName,bean);
               }
           }
    	/**
         * 创建bean对象
         * @param beanName bean名称
         * @param definition 对象描述封装类
         * @return
         * @throws Exception
         */
        public Object creatBean(String beanName, BeanDefinition definition) throws Exception {
            //创建当前对象==>且放入单例池中(单例的Map中)
            Class clazz = definition.getType();
            try {
                Object instance = clazz.getConstructor().newInstance();
                //判断当前的对象中是否有@autowide(依赖注入)注解 ,如果包含这个注解,需要将其字段进行赋值
                for (Field field : clazz.getDeclaredFields()) {
                    if (field.isAnnotationPresent(Autowired.class)) {
                        field.setAccessible(true);
                        field.set(instance, getBean(field.getName()));
                    }
                }
                //回到方法==>beanNameAware
                if (instance instanceof BeanNameAware) {
                    ((BeanNameAware) instance).setBeanName(beanName);
                }
                //初始化前方法
                if (beanPostProcessorList.size() > 0) {
                    for (BeanPostProcessor beanPostProcessor : beanPostProcessorList) {
                        instance = beanPostProcessor.postProcessBeforeInitialization(instance, beanName);
                    }
                }
                //初始化
                if (instance instanceof InitializingBean) {
                    ((InitializingBean) instance).afterPropertiesSet();
                }
                //初始化后(切面AOP)
                if (beanPostProcessorList.size() > 0) {
                    for (BeanPostProcessor beanPostProcessor : beanPostProcessorList) {
                        instance = beanPostProcessor.postProcessAfterInitialization(instance, beanName);
                    }
                }
                return instance;
            } catch (Exception e){
                throw new Exception("创建对象失败~~~",e);
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138

    3、创建对象后,需要提供需要获取Bean的方法

    /**
         * 定于一个方法是获取bean资源的
         */
        public Object getBean(String beanName) throws Exception {
    
            //判断当前的BeanDefinitionMap中是否存在beanName为key的beanDefinition
            if (!beanDefinitionMap.containsKey(beanName)) {
                throw new Exception("当前beanName在BeanDefinitionMap中不存在~~~");
            }
            //从BeanDefinitionMap中获取到BeanDefinition信息==>判断其scope
            BeanDefinition beanDefinition = beanDefinitionMap.get(beanName);
    
            //单例
            if ("singleton".equals(beanDefinition.getScope())) {
                Object singletonBean = singletonMap.get(beanName);
                if (singletonBean == null) {
                    singletonBean = creatBean(beanName,beanDefinition);
                    singletonMap.put(beanName,singletonBean);
                }
                return singletonBean;
            }else {
                //原型
               Object prototypeBean = creatBean(beanName,beanDefinition);
               return prototypeBean;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    4、总结

    总的来说,在Spring创建对象的过程中,主要分为,结合传入的类路径信息,扫描需要创建的对象资源=>结合上一步的扫描结果创建对象=>将创建好的对象提供一个对外获取Bean接口,具体详细过程图示:
    在这里插入图片描述
    在这里插入图片描述

  • 相关阅读:
    java easyexcel 导出多级表头
    《C和指针》(1)快速上手
    力扣(LeetCode)容器装水问题
    Linux安装配置awscli命令行接口工具及其从aws上传下载数据
    Hash与ZSet的常用命令以及其底层数据结构
    【nacos】5.1 spring cloud + Nacos 实现统一配置管理
    单相3.5kw无变压器住宅并网光伏系统研究
    JAVA设计模式解决支付问题
    Oracle系列十九:Oracle的体系结构
    颜色标记txt和多根走线【Cadance进阶】
  • 原文地址:https://blog.csdn.net/weixin_42070243/article/details/134309699