简单Spring源码解析(一) 容器启动

一、创建spring容器

首先建立Test类和service类

在Test类中创建spring容器

自定义两个注解@ComponmentScan和@Componment注解,提供扫描路径方法

java 复制代码
package com.spring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
    String value() default "";
}
java 复制代码
package com.spring;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ComponentScan {
    String value() default "";
}

创建spring容器类和appconfig工具类

二、手写模拟Spring扫描底层实现

创建一个UserService的bean,扫描工具类看是否有通过这个注解得到的类信息。

java 复制代码
//spring容器类
public class GaoApplicationContext {
    private  Class configClass;

    public GaoApplicationContext(Class configClass) {
        this.configClass = configClass;

        //扫描工具类看是否有通过这个注解得到的类信息
        //这段代码的意思是从一个类的注解中获取ComponentScan注解,并将其赋值给componentScanAnnotation变量。
        //具体来说,configClass是一个类对象,getAnnotation(ComponentScan.class)是通过反射获取configClass类上的ComponentScan注解。
        //如果configClass类上存在ComponentScan注解,则返回该注解的实例;否则返回null。
        //将返回的注解实例赋值给componentScanAnnotation变量,可以通过该变量来访问注解的属性值。
        //通过这段代码,我们可以在运行时动态地获取类上的注解,并进一步处理注解的属性值。
        //(ComponentScan)是一种强制类型转换的写法,
        //将configClass.getAnnotation(ComponentScan.class)返回的注解实例转换为ComponentScan类型。
        //在Java中,通过反射获取注解实例时,返回的是Annotation类型,而不是具体的注解类型。
        //因此,如果我们需要访问注解的属性值或者调用注解的方法,就需要将其转换为具体的注解类型。这就是使用(ComponentScan)进行类型转换的作用。
        //在这段代码中,configClass.getAnnotation(ComponentScan.class)返回的注解实例被转换为ComponentScan类型,
        //并赋值给componentScanAnnotation变量,以便后续访问注解的属性值
        if (configClass.isAnnotationPresent(ComponentScan.class)){
            ComponentScan componentScanAnnotation = (ComponentScan) configClass.getAnnotation(ComponentScan.class);

            //扫描路径 com.service(并不是扫描service目录下的文件,而是编译后.class文件即target文件夹service下的文件)
            String path = componentScanAnnotation.value();

            path = path.replace(".","/"); //  com/service

            //传相对路径到classLoader里面 D:\test\spring\target\classes\com\service
            ClassLoader classLoader = GaoApplicationContext.class.getClassLoader();
            URL resource = classLoader.getResource(path);

            File file = new File(resource.getFile());

            System.out.println(file);
            //判断resource里面传的是否是一个文件夹
            if (file.isDirectory()){
                //获得所有文件夹的名称
                File[] files = file.listFiles();

                //得到想要的.class文件
                for (File f : files){
                    //得到绝对路径的名字
                    String fileName = f.getAbsolutePath();
                    //判断是否是,class文件
                    //判断你的.class文件名是不是一个bean
                    //就是看你的对象有没有@Componment注解
                    //通过反射判断获是否有@Componment注解
                    if (fileName.endsWith(".class")){

                        //com\service\UserService
                        //获得类名
                        String className = fileName.substring(fileName.indexOf("com"), fileName.indexOf(".class"));
                        className = className.replace("\\",".");
                        System.out.println(className);

                        try {
                            Class<?> clazz = classLoader.loadClass(className);
                            if(clazz.isAnnotationPresent(Component.class)){
                                //  Bean

                            }
                        }catch (ClassNotFoundException e){
                            e.printStackTrace();;
                        }



                    }

                }

            }
        }


    }
    public  Object getBean(String beanName){
        return null;
    }

三、模拟BeanDefinition的生成

创建BeanDifinition类,生成getter()、setter()方法,通过判断是否含有scope注解来进行生成


第二步和第三步总的来说就是先扫描-->BeanDefinition-->beanDefinitionMap,扫描之后创建对象,将对象放入map容器当中,然后将所有的单例都查找出来并且创建对象实例化单例Bean存到单例池里面。

四、测试

我们可以测试一下,在test中获取UserService对象的时候,Userservice中不添加@Scope("prototype")和添加是否不同

可以很明显的看出单例和多例的区别

相关推荐
郝学胜-神的一滴20 分钟前
Effective STL 条款12:对容器线程安全性的现实期望
c语言·开发语言·c++·qt·网络协议·rpc·numpy
秋饼1 小时前
Spring AI 2.0 渐进式工具披露实战:ToolSearchToolCallingAdvisor 源码机制、四种 ToolIndex 选型与多租户踩坑
java·ai·技术分享·后端开发
头发还在的女程序员4 小时前
家政小程序系统源码IM已读记录:多端阅读状态怎样保持单调推进
java·spring boot·源码·家政·消息同步
Rain的Java大神之路4 小时前
别再背参数了:Kafka、RocketMQ、RabbitMQ 的设计取舍与生产级避坑指南
java·后端·rocketmq
Rain的Java大神之路5 小时前
LeetCode 42「接雨水」:面试官百出不厌的题,一文讲透从暴力到双指针 O(1)
java·后端·leetcode
urkay-6 小时前
Android 线程池工具类与监控
android·java·性能优化
艺杯羹6 小时前
请求结束内存不降?ThreadLocal弱引用Key与线程池串号排查
java·jvm·内存·并发编程
会编程的吕洞宾6 小时前
Kafka消费者组重平衡实战:重复消费与消费停顿排查
java·经验分享·后端
qq_4523962315 小时前
第三篇:Transactional Outbox 模式详解
java·kubernetes·rocketmq
IT空门:门主15 小时前
Spring AI 2.1.0 更新了什么:从 2.0 升上来的完整梳理
人工智能·python·spring