简单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")和添加是否不同

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

相关推荐
源码集结号4 分钟前
一套智慧工地云平台源码,支持监管端、项目管理端,Java+Spring Cloud +UniApp +MySql技术开发
java·mysql·spring cloud·uni-app·源码·智慧工地·成品系统
EnCi Zheng6 分钟前
Spring Security 最简配置完全指南-从入门到精通前后端分离安全配置
java·安全·spring
程序员小假7 分钟前
为什么这些 SQL 语句逻辑相同,性能却差异巨大?
java·后端
泉城老铁1 小时前
springboot实现对接poi 导出excel折线图
java·spring boot·后端
金銀銅鐵1 小时前
[Java] 如何自动生成简单的 Mermaid 类图
java·后端
纵横八荒1 小时前
Java基础加强13-集合框架、Stream流
java·开发语言
稚辉君.MCA_P8_Java2 小时前
kafka解决了什么问题?mmap 和sendfile
java·spring boot·分布式·kafka·kubernetes
乄bluefox2 小时前
保姆级docker部署nacos集群
java·docker·容器
欣然~2 小时前
百度地图收藏地址提取与格式转换工具 说明文档
java·开发语言·dubbo
玩毛线的包子2 小时前
Android Gradle学习(十三)- 配置读取和文件写入
java