30个类手写Spring核心原理之Ioc顶层架构设计(2)

简介: Annotation的代码实现我们还是沿用Mini版本的,保持不变,复制过来便可。

本文节选自《Spring 5核心原理》

1 Annotation(自定义配置)模块

Annotation的代码实现我们还是沿用Mini版本的,保持不变,复制过来便可。

1.1 @GPService

@GPService代码如下:

package com.tom.spring.formework.annotation;
import java.lang.annotation.Documented;
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)
@Documented
public @interface GPService {
   String value() default "";
}

1.2 @GPAutowired

@GPAutowired代码如下:

package com.tom.spring.formework.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 自动注入
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface GPAutowired {
   String value() default "";
}

1.3 @GPController

@GPController代码如下:

package com.tom.spring.formework.annotation;
import java.lang.annotation.Documented;
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)
@Documented
public @interface GPController {
   String value() default "";
}

1.4 @GPRequestMapping

@GPRequestMapping代码如下:

package com.tom.spring.formework.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 请求URL
 */
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface GPRequestMapping {
   String value() default "";
}

1.5 @GPRequestParam

@GPRequestParam代码如下:

package com.tom.spring.formework.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * 请求参数映射
 */
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface GPRequestParam {
   String value() default "";
}

2 core(顶层接口)模块

2.1 GPFactoryBean

关于顶层接口设计,通过前面的学习我们了解了FactoryBean的基本作用,在此不做过多解释。

package com.tom.spring.formework.core;
public interface GPFactoryBean {
}

2.2 GPBeanFactory

作为所有IoC容器的顶层设计,前面也已经详细介绍了BeanFactory的作用。

package com.tom.spring.formework.core;
/**
 * 单例工厂的顶层设计
 */
public interface GPBeanFactory {
    /**
     * 根据beanName从IoC容器中获得一个实例Bean
     * @param beanName
     * @return
     */
    Object getBean(String beanName) throws Exception;
    public Object getBean(Class<?> beanClass) throws Exception;
}

3 beans(配置封装)模块

3.1 GPBeanDefinition

BeanDefinition主要用于保存Bean相关的配置信息。

package com.tom.spring.formework.beans.config;
//用来存储配置文件中的信息
//相当于保存在内存中的配置
public class GPBeanDefinition {
    private String beanClassName;  //原生Bean的全类名
    private boolean lazyInit = false; //标记是否延时加载
    private String factoryBeanName;  //保存beanName,在IoC容器中存储的key
    public String getBeanClassName() {
        return beanClassName;
    }
    public void setBeanClassName(String beanClassName) {
        this.beanClassName = beanClassName;
    }
    public boolean isLazyInit() {
        return lazyInit;
    }
    public void setLazyInit(boolean lazyInit) {
        this.lazyInit = lazyInit;
    }
    public String getFactoryBeanName() {
        return factoryBeanName;
    }
    public void setFactoryBeanName(String factoryBeanName) {
        this.factoryBeanName = factoryBeanName;
    }
}

3.2 GPBeanWrapper

BeanWrapper主要用于封装创建后的对象实例,代理对象(Proxy Object)或者原生对象(Original Object)都由BeanWrapper来保存。

package com.tom.spring.formework.beans;
public class GPBeanWrapper {
    private Object wrappedInstance;
    private Class<?> wrappedClass;
    public GPBeanWrapper(Object wrappedInstance){
        this.wrappedInstance = wrappedInstance;
    }
    public Object getWrappedInstance(){
        return this.wrappedInstance;
    }
    //返回代理以后的Class
    //可能会是这个 $Proxy0
    public Class<?> getWrappedClass(){
        return this.wrappedInstance.getClass();
    }
}

4 context(IoC容器)模块

4.1 GPAbstractApplicationContext

IoC容器实现类的顶层抽象类,实现IoC容器相关的公共逻辑。为了尽可能地简化,在这个Mini版本中,暂时只设计了一个refresh()方法。

package com.tom.spring.formework.context.support;
/**
 * IoC容器实现的顶层设计
 */
public abstract class GPAbstractApplicationContext {
    //受保护,只提供给子类重写
    public void refresh() throws Exception {}
}

4.2 GPDefaultListableBeanFactory

DefaultListableBeanFactory是众多IoC容器子类的典型代表。在Mini版本中我只做了一个简单的设计,就是定义顶层的IoC缓存,也就是一个Map,属性名字也和原生Spring保持一致,定义为beanDefinitionMap,以方便大家对比理解。

package com.tom.spring.formework.beans.support;
import com.tom.spring.formework.beans.config.GPBeanDefinition;
import com.tom.spring.formework.context.support.GPAbstractApplicationContext;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class GPDefaultListableBeanFactory extends GPAbstractApplicationContext{
    //存储注册信息的BeanDefinition
    protected final Map<String, GPBeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, GPBeanDefinition>();
}

4.3 GPApplicationContext

ApplicationContext是直接接触用户的入口,主要实现DefaultListableBeanFactory的refresh()方法和BeanFactory的getBean()方法,完成IoC、DI、AOP的衔接。

package com.tom.spring.formework.context;
import com.tom.spring.formework.annotation.GPAutowired;
import com.tom.spring.formework.annotation.GPController;
import com.tom.spring.formework.annotation.GPService;
import com.tom.spring.formework.beans.GPBeanWrapper;
import com.tom.spring.formework.beans.config.GPBeanPostProcessor;
import com.tom.spring.formework.core.GPBeanFactory;
import com.tom.spring.formework.beans.config.GPBeanDefinition;
import com.tom.spring.formework.beans.support.GPBeanDefinitionReader;
import com.tom.spring.formework.beans.support.GPDefaultListableBeanFactory;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
/**
 * 按之前源码分析的套路,IoC、DI、MVC、AOP
 */
public class GPApplicationContext extends GPDefaultListableBeanFactory implements GPBeanFactory {
    private String [] configLoactions;
    private GPBeanDefinitionReader reader;
    //单例的IoC容器缓存
    private Map<String,Object> factoryBeanObjectCache = new ConcurrentHashMap<String, Object>();
    //通用的IoC容器
    private Map<String,GPBeanWrapper> factoryBeanInstanceCache = new ConcurrentHashMap<String, GPBeanWrapper>();
    public GPApplicationContext(String... configLoactions){
        this.configLoactions = configLoactions;
        try {
            refresh();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void refresh() throws Exception{
        //1. 定位,定位配置文件
        reader = new GPBeanDefinitionReader(this.configLoactions);
        //2. 加载配置文件,扫描相关的类,把它们封装成BeanDefinition
        List<GPBeanDefinition> beanDefinitions = reader.loadBeanDefinitions();
        //3. 注册,把配置信息放到容器里面(伪IoC容器)
        doRegisterBeanDefinition(beanDefinitions);
        //4. 把不是延时加载的类提前初始化
        doAutowrited();
    }
    //只处理非延时加载的情况
    private void doAutowrited() {
        for (Map.Entry<String, GPBeanDefinition> beanDefinitionEntry : super.beanDefinitionMap.entrySet()) {
           String beanName = beanDefinitionEntry.getKey();
           if(!beanDefinitionEntry.getValue().isLazyInit()) {
               try {
                   getBean(beanName);
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
        }
    }
    private void doRegisterBeanDefinition(List<GPBeanDefinition> beanDefinitions) throws Exception {
        for (GPBeanDefinition beanDefinition: beanDefinitions) {
            if(super.beanDefinitionMap.containsKey(beanDefinition.getFactoryBeanName())){
                throw new Exception("The “" + beanDefinition.getFactoryBeanName() + "” is exists!!");
            }
            super.beanDefinitionMap.put(beanDefinition.getFactoryBeanName(),beanDefinition);
        }
        //到这里为止,容器初始化完毕
    }
    public Object getBean(Class<?> beanClass) throws Exception {
        return getBean(beanClass.getName());
    }
    //依赖注入,从这里开始,读取BeanDefinition中的信息
    //然后通过反射机制创建一个实例并返回
    //Spring做法是,不会把最原始的对象放出去,会用一个BeanWrapper来进行一次包装
    //装饰器模式:
    //1. 保留原来的OOP关系
    //2. 需要对它进行扩展、增强(为了以后的AOP打基础)
    public Object getBean(String beanName) throws Exception {
        return null;
    }
    public String[] getBeanDefinitionNames() {
        return this.beanDefinitionMap.keySet().toArray(new String[this.beanDefinitionMap. size()]);
    }
    public int getBeanDefinitionCount(){
        return this.beanDefinitionMap.size();
    }
    public Properties getConfig(){
        return this.reader.getConfig();
    }
}

4.4 GPBeanDefinitionReader

根据约定,BeanDefinitionReader主要完成对application.properties配置文件的解析工作,实现逻辑非常简单。通过构造方法获取从ApplicationContext传过来的locations配置文件路径,然后解析,扫描并保存所有相关的类并提供统一的访问入口。

package com.tom.spring.formework.beans.support;
import com.tom.spring.formework.beans.config.GPBeanDefinition;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
//对配置文件进行查找、读取、解析
public class GPBeanDefinitionReader {
    private List<String> registyBeanClasses = new ArrayList<String>();
    private Properties config = new Properties();
    //固定配置文件中的key,相对于XML的规范
    private final String SCAN_PACKAGE = "scanPackage";
    public GPBeanDefinitionReader(String... locations){
        //通过URL定位找到其所对应的文件,然后转换为文件流
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(locations[0]. replace("classpath:",""));
        try {
            config.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(null != is){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        doScanner(config.getProperty(SCAN_PACKAGE));
    }
    private void doScanner(String scanPackage) {
        //转换为文件路径,实际上就是把.替换为/
        URL url = this.getClass().getClassLoader().getResource("/" + scanPackage.replaceAll ("\\.","/"));
        File classPath = new File(url.getFile());
        for (File file : classPath.listFiles()) {
            if(file.isDirectory()){
                doScanner(scanPackage + "." + file.getName());
            }else{
                if(!file.getName().endsWith(".class")){ continue;}
                String className = (scanPackage + "." + file.getName().replace(".class",""));
                registyBeanClasses.add(className);
            }
        }
    }
    public Properties getConfig(){
        return this.config;
    }
    //把配置文件中扫描到的所有配置信息转换为GPBeanDefinition对象,以便于之后的IoC操作
    public List<GPBeanDefinition> loadBeanDefinitions(){
        List<GPBeanDefinition> result = new ArrayList<GPBeanDefinition>();
        try {
            for (String className : registyBeanClasses) {
                Class<?> beanClass = Class.forName(className);
                if(beanClass.isInterface()) { continue; }
                result.add(doCreateBeanDefinition(toLowerFirstCase(beanClass.getSimpleName()), beanClass.getName()));
                Class<?> [] interfaces = beanClass.getInterfaces();
                for (Class<?> i : interfaces) {
                    result.add(doCreateBeanDefinition(i.getName(),beanClass.getName()));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    //把每一个配置信息解析成一个BeanDefinition
    private GPBeanDefinition doCreateBeanDefinition(String factoryBeanName,String beanClassName){
        GPBeanDefinition beanDefinition = new GPBeanDefinition();
        beanDefinition.setBeanClassName(beanClassName);
        beanDefinition.setFactoryBeanName(factoryBeanName);
        return beanDefinition;
    }
    //将类名首字母改为小写
    //为了简化程序逻辑,就不做其他判断了,大家了解就好
    private String toLowerFirstCase(String simpleName) {
        char [] chars = simpleName.toCharArray();
        //因为大小写字母的ASCII码相差32
        //而且大写字母的ASCII码要小于小写字母的ASCII码
        //在Java中,对char做算术运算,实际上就是对ASCII码做算术运算
        chars[0] += 32;
        return String.valueOf(chars);
    }
}

4.5 GPApplicationContextAware

相信很多“小伙伴”都用过ApplicationContextAware接口,主要是通过实现侦听机制得到一个回调方法,从而得到IoC容器的上下文,即ApplicationContext。在这个Mini版本中只是做了一个顶层设计,告诉大家这样一种现象,并没有做具体实现。这不是本书的重点,感兴趣的“小伙伴”可以自行尝试。

package com.tom.spring.formework.context;
/**
 * 通过解耦方式获得IoC容器的顶层设计
 * 后面将通过一个监听器去扫描所有的类,只要实现了此接口,
 * 将自动调用setApplicationContext()方法,从而将IoC容器注入目标类中
 */
public interface GPApplicationContextAware {
    void setApplicationContext(GPApplicationContext applicationContext);
}

本文为“Tom弹架构”原创,转载请注明出处。技术在于分享,我分享我快乐!

如果您有任何建议也可留言评论或私信,您的支持是我坚持创作的动力。


原创不易,坚持很酷,都看到这里了,小伙伴记得点赞、收藏、在看,一键三连加关注!如果你觉得内容太干,可以分享转发给朋友滋润滋润!

相关文章
|
10月前
|
缓存 Java 开发者
【Spring】原理:Bean的作用域与生命周期
本文将围绕 Spring Bean 的作用域与生命周期展开深度剖析,系统梳理作用域的类型与应用场景、生命周期的关键阶段与扩展点,并结合实际案例揭示其底层实现原理,为开发者提供从理论到实践的完整指导。
1104 22
|
10月前
|
人工智能 Java 开发者
【Spring】原理解析:Spring Boot 自动配置
Spring Boot通过“约定优于配置”的设计理念,自动检测项目依赖并根据这些依赖自动装配相应的Bean,从而解放开发者从繁琐的配置工作中解脱出来,专注于业务逻辑实现。
2979 0
|
9月前
|
监控 Cloud Native Java
Spring Boot 3.x 微服务架构实战指南
🌟蒋星熠Jaxonic,技术宇宙中的星际旅人。深耕Spring Boot 3.x与微服务架构,探索云原生、性能优化与高可用系统设计。以代码为笔,在二进制星河中谱写极客诗篇。关注我,共赴技术星辰大海!(238字)
1392 2
Spring Boot 3.x 微服务架构实战指南
|
9月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
9月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
784 2
|
10月前
|
Java 数据库 数据安全/隐私保护
Spring Boot四层架构深度解析
本文详解Spring Boot四层架构(Controller-Service-DAO-Database)的核心思想与实战应用,涵盖职责划分、代码结构、依赖注入、事务管理及常见问题解决方案,助力构建高内聚、低耦合的企业级应用。
1802 1
|
9月前
|
机器学习/深度学习 自然语言处理 监控
23_Transformer架构详解:从原理到PyTorch实现
Transformer架构自2017年Google发表的论文《Attention Is All You Need》中提出以来,彻底改变了深度学习特别是自然语言处理领域的格局。在短短几年内,Transformer已成为几乎所有现代大型语言模型(LLM)的基础架构,包括BERT、GPT系列、T5等革命性模型。与传统的RNN和LSTM相比,Transformer通过自注意力机制实现了并行化训练,极大提高了模型的训练效率和性能。
2097 0
|
10月前
|
Kubernetes Java 微服务
Spring Cloud 微服务架构技术解析与实践指南
本文档全面介绍 Spring Cloud 微服务架构的核心组件、设计理念和实现方案。作为构建分布式系统的综合工具箱,Spring Cloud 为微服务架构提供了服务发现、配置管理、负载均衡、熔断器等关键功能的标准化实现。本文将深入探讨其核心组件的工作原理、集成方式以及在实际项目中的最佳实践,帮助开发者构建高可用、可扩展的分布式系统。
851 0
|
Java Spring
Spring原理学习系列之一:注解原理解析
对于Spring注解大家肯定都不陌生,在日常开发工作中也会经常使用到注解。有时候提问小伙伴,注解的原理是什么,大部分都回答是利用了反射机制。但是继续深入提问,在Spring中是如何解析这些自带注解以及注解到底在什么时候起作用等问题时,很多人都会犯嘀咕。同样我在实际使用的过程中,也会有相同的困惑。所以一直想探究下注解实际的工作原理以及设计思想。用此文记录下自己对于注解原理的理解,也为有同样疑问的小伙伴提供些不同的理解角度。 原理解析 使用实例
Spring原理学习系列之一:注解原理解析
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
1433 0

热门文章

最新文章