手写模拟spring扫描底层实现


前言

最近也是开始学习了Spring的一些底层,学习的最好方法就是自己实现,所以也是跟着视频自己手写一个模拟spring,其中对一些知识也是会总结和讲解自己的理解。

1、ApplicationContext和AppConfig

我们首先需要模拟的是spring的自动扫描机制,我们来想一下,当spring容器启动,自动扫描的时候,首先是肯定不能缺少启动类的,也就是我们的ApplicationContext,所以我们先创建一个ApplicationContext。

回想一下,我们之前手动去配置spring.xml的时候,是要通过启动配置ApplicationContext将配置文件进行读取,所以我们先要写一个spring的配置类 AppConfig,当然,我们这里并不进行一些复杂的配置。

/**
 * @author zal
 * @date 2022年08月30日 16:23
 * @Description Spring容器配置类
 */
@ComponentScan("com.zal.service")
public class AppConfig {
}

然后将这个AppConfig注入到我们的启动类ZalApplicationContext中,实现基本的配置读取。

/**
 * @author zal
 * @date 2022年08月30日 16:21
 * @Description 模拟Spring容器启动类
 */
public class ZalApplicationContext {

    private Class configClass;

	/**
     * 创建一个Spring容器
     *
     * @param configClass
     */
    public ZalApplicationContext(Class configClass) {
        this.configClass = configClass;
    }
}

2、@Component和@ComponentScan注解

我们知道,Spring的扫描是通过类上是否加上了注解@Component来进行判断的,而@ComponentScan 注解则是配置扫描包的路径的,所以我们要实现一下这两个注解。

/**
 * @author zal
 * @date 2022年08月30日 16:20
 * @Description 模仿spring的bean注入的注解
 * @Target(ElementType.TYPE)  表示只能作用在类的上面
 * @Retention(RetentionPolicy.RUNTIME) 表示作用在运行时
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
    String value() default "";
}
/**
 * @author zal
 * @date 2022年08月30日 16:20
 * @Description 模仿spring路径扫描的注解
 * @Target(ElementType.TYPE)  表示只能作用在类的上面
 * @Retention(RetentionPolicy.RUNTIME) 表示作用在运行时
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ComponentScan {
    String value() default "";
}

3、UserService和Test

拥有了Spring启动类以及相关扫描的注解之后,我们需要一个被扫描的服务以及对应的测试方法,所以我们这里自定义一个UserService,给这个类加上相应的注解后进行扫描逻逻辑的编写。

@Component("userService")
public class UserService {
	// 代码
}
public class Test {
    public static void main(String[] args) {
        ZalApplicationContext applicationContext = new ZalApplicationContext(AppConfig.class);
        // 我们拿到spring容器后,肯定是要进行获取bean的操作的
        // 获取bean的操作我们放在下篇文章讲解
        applicationContext.getBean("userService");
    }
}

最后是ZalApplicationContext进行扫描的逻辑实现,如下代码

public class ZalApplicationContext {

    private Class configClass;
    /**
     * 创建一个Spring容器
     *
     * @param configClass
     */
    public ZalApplicationContext(Class configClass) {
        this.configClass = configClass;
        // 扫描
        // 判断传入的配置类上是否添加了ComponentScan注解
        if (configClass.isAnnotationPresent(ComponentScan.class)) {
            // 获取注解的信息
            ComponentScan componentScanAnnotation = (ComponentScan) configClass.getAnnotation(ComponentScan.class);
            // 获取扫描路径(就是注解上添加的包路径),如com.zal.service
            String path = componentScanAnnotation.value();
            // 处理路径字符串,将.替换为/,如com/zal/service
            path = path.replace(".", "/");

            //获取容器的类加载器,获取class文件的绝对路径 如F:/web/zal-spring/out/production/zal-spring/com/zal/service
            ClassLoader classLoader = ZalApplicationContext.class.getClassLoader();
            URL resource = classLoader.getResource(path);
            // 将class文件封装为一个文件或者是文件夹
            File file = new File(resource.getFile());

            if (file.isDirectory()) {
                // 拿到当前文件夹中的所有文件
                File[] files = file.listFiles();
                for (File f : files) {
                    // 拿到文件的绝对路径
                    String fileName = f.getAbsolutePath();
                    // 判断文件是否以class结尾
                    if (fileName.endsWith(".class")) {
                        // 获取class文件的全限定类名,如com.zal.service.UserService
                        String className = fileName
                                .substring(fileName.indexOf("com"), fileName.indexOf(".class"))
                                .replace("\\", ".");
                        try {
                            // 如何获取class对象呢? 这就涉及到类的加载
                            Class<?> clazz = classLoader.loadClass(className);
                            // 判断这个类是否是一个bean,条件是类上是否有@Component注解
                            if (clazz.isAnnotationPresent(Component.class)) {
                              	//bean的创建操作
                            }
                        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

以上实现只是一个简单的模拟spring底层扫描的逻辑代码,表示spring扫描的一个简单的逻辑,并不是真正的spring底层扫描实现,只是为了更好的让我们去了解底层实现的一个思想。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值