irpas技术客

手写模拟Spring框架核心逻辑_成胜文

大大的周 1496

手写模拟Spring框架核心逻辑 了解Spring框架工作大概流程创建AnnotationConfigApplicationContext启动类创建BeanDefinition类实现@Autowired的依赖注入实现BeanNameAware接口

了解Spring框架工作大概流程

模拟Spring框架核心代码并不是实现真正的Spring核心源码,而是为了后续看源码进行的一个铺垫,同时我也相信在以后的某个时间段的面试中会跟面试官扯到这个犊子 ,主要还是通过手写一遍了解Spring工作的大概流程,对于Spring不单单停止在应用上,更应该往深里学,废话到这,下面正式开始。。。 我们现在用的框架大部分都是SpringCloud或者SpringBoot去启动的,但是他们的启动都是会间接使用到ClassPathXmlApplicationContext去扫描xml配置,所以我们得了解Spring的启动类,如下图: 或者说使用注解的方式,AnnotationConfigApplicationContext(这个启动类会自动帮我们创建对象,使用了他,就不用我们自己去new一个对象了),可以直接理解为他就是一个容器,如下图: ??:这里再普及一点小知识,所有的Bean都是对象,但是对象不一定都是Bean,第二个就是为什么使用Spring,原因就是回到JDBC + Servlet版本的时候,项目中大量的创建、使用对象,造成依赖性特别强,不好维护,Spring就相当于项目的管理者,可以帮我们创建对象、给属性去赋值等等这些吃力不讨好的苦力活…

创建AnnotationConfigApplicationContext启动类

这里我们首先创建一个项目,不需要引入任何依赖 定义Component注入注解,然后创建AnnotationConfigApplicationContext启动类,

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

注入在我们的实体类上,但是现在是没有功能的

创建AnnotationConfigApplicationContext实体类,并提供一个getBean(String beanName)方法,然后在测试类里面去获取我们的bean

写到这里,空壳子就出来了,首先,我们知道AnnotationConfigApplicationContext是启动类,会帮我们去创建bean,所以我们要写一个createBean()方法,但是在创建之前要先扫描,所以我们这里要定义一个扫描的注解,去扫描需要创建的对象

@ComponentScan @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface ComponentScan { String value() default ""; } AppConfig,这里需要注意一个点,并非去扫描所有在com.csw.spring.service.impl下面的类,而是扫描加了@component注解的类,这里还有一个注意点就是AnnotationConfigApplicationContext并非会创建所有加了@component,而是会去加载非懒加载的单例bean @ComponentScan("com.csw.spring.service.impl") public class AppConfig { }

如下图:

??课堂小知识:什么是单例bean?什么是原型bean?

单例bean又分为懒加载和非懒加载,简单来说懒加载就是每次启动了之后不会马上去创建,而是会在调用了getBean()方法之后才会去创建,非懒加载就是启动了之后去创建,创建单例bean最直接的方式就是在类上加@Service或者@Component prototypeScope,即原型bean,每次请求时都会创建新的bean实例直接使用。创建原型Bean,就是在@Service或者@Component的基础上加个@Scope(“prototype”)。

在做完了以上的步骤,AnnotationConfigApplicationContext启动会去判断是不是加了@component注解和是单例bean还是原型bean,非懒加载单例bean还是懒加载单例bean,这时候我们就需要一个@Lazy的注解去区分

@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Lazy { }

AnnotationConfigApplicationContext扫描的步骤

package com.csw.spring.context; import com.csw.spring.anno.ComponentScan; import java.lang.annotation.Annotation; /** * @author csw * @date 2021/12/19 01:12:45 * @version 1.0 */ public class CswApplicationContext { private Class configClass; public CswApplicationContext(Class configClass) { this.configClass = configClass; //判断扫描是不是存在 if (configClass.isAnnotationPresent(ComponentScan.class)) { //获取ComponentScan的信息 ComponentScan componentScanAnnotation = (ComponentScan) configClass.getAnnotation(ComponentScan.class); String path = componentScanAnnotation.value();//获取扫描路径 System.out.println(path); } } public Object getBean(String beanName) { return null; } }

启动测试类后得到,如下图: 但是我们这里真正需要获取的路径是编译后的class文件,如下图: 课堂小知识??:类加载是在本地文件夹的绝对路径上加载的 根据以上我们可以通过getClassLoader方法获取类加载后的文件路径

package com.csw.spring.context; import com.csw.spring.anno.Component; import com.csw.spring.anno.ComponentScan; import com.csw.spring.anno.Lazy; import com.csw.spring.anno.Scope; import java.io.File; import java.lang.annotation.Annotation; import java.net.URL; /** * @author csw * @date 2021/12/19 01:12:45 * @version 1.0 */ public class CswApplicationContext { private Class configClass; public CswApplicationContext(Class configClass) { this.configClass = configClass; //判断扫描是不是存在 if (configClass.isAnnotationPresent(ComponentScan.class)) { //获取ComponentScan的信息 ComponentScan componentScanAnnotation = (ComponentScan) configClass.getAnnotation(ComponentScan.class); String path = componentScanAnnotation.value();//获取扫描路径,com.csw.spring.service.impl //替换com.csw.spring.service.impl为com/csw/spring/service/impl path = path.replace(".","/"); //获取类加载后文件路径 ClassLoader classLoader = CswApplicationContext.class.getClassLoader(); URL resource = classLoader.getResource(path); File file = new File(resource.getFile()); //对获取对文件路径进行循环遍历 for (File f : file.listFiles()) { //获取path路径:/Users/shengwencheng/Desktop/cloud/SpringCore/spring-core/Spring/target/classes/com/csw/spring/service/impl/UserServiceImpl.class String s = f.getAbsolutePath(); //只要class结尾的文件 if(s.endsWith(".class")) { //截取com到.class之间到路径 s = s.substring(s.indexOf("com"),s.indexOf(".class")); //把转义替换成. s = s.replace("\\","."); try { Class clazz = classLoader.loadClass(s); //判断类上是否有Component注解 if (clazz.isAnnotationPresent(Component.class)) { Component componentAnnotation = (Component) clazz.getAnnotation(Component.class); String beanName = componentAnnotation.value(); //判断是否是非懒加载的bean if (clazz.isAnnotationPresent(Lazy.class)) { } //判断是否是原型bean if (clazz.isAnnotationPresent(Scope.class)) { Scope scopeAnnotation = (Scope) clazz.getAnnotation(Scope.class); String value = scopeAnnotation.value(); } else { //单例 } } } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } //创建非懒加载的单例bean } public Object getBean(String beanName) { return null; } }

通过以上代码我们拿到了具体是哪个类需要去解析,但是我们不可能通过getBean获取到bean,然后每次都要去解析,再判断他是单例的还是原型,这样的话性能太低了,于是就有了BeanDefinition这个概念。。。

创建BeanDefinition类

BeanDefinition,简单来说就是bean的定义,相当与把类解析后的结果缓存起来,用于声明bean的作用域

BeanDefinition package com.csw.spring.framework; /** * @author csw * @version 1.0 * @date 2021/12/19 7:58 下午 * * bean的定义,声明作用域 */ public class BeanDefinition { private String scope; //原型的 private boolean isLazy; //懒加载 private Class beanClass; //bean的类型 public String getScope() { return scope; } public void setScope(String scope) { this.scope = scope; } public boolean isLazy() { return isLazy; } public void setLazy(boolean lazy) { isLazy = lazy; } public Class getBeanClass() { return beanClass; } public void setBeanClass(Class beanClass) { this.beanClass = beanClass; } }

课堂小知识??:BeanDefinition与Bean对象的关系,先有Bean的定义才有Bean对象 这里继续完善扫描 - getBean和createBean方法

package com.csw.spring.context; import com.csw.spring.anno.Component; import com.csw.spring.anno.ComponentScan; import com.csw.spring.anno.Lazy; import com.csw.spring.anno.Scope; import com.csw.spring.framework.BeanDefinition; import com.sun.tools.javac.util.StringUtils; import java.io.File; import java.lang.annotation.Annotation; import java.lang.reflect.InvocationTargetException; import java.net.URL; import java.util.HashMap; import java.util.Map; /** * @author csw * @date 2021/12/19 01:12:45 * @version 1.0 */ public class CswApplicationContext { private Class configClass; private Map<String,BeanDefinition> beanDefinitionMap = new HashMap<String, BeanDefinition>(); //存放解析后的bean缓存池 private Map<String,Object> singletonObj = new HashMap<String, Object>(); //单例池 public CswApplicationContext(Class configClass) { this.configClass = configClass; //扫描 --得到BeanDefinition对象 scan(configClass, beanDefinitionMap); //创建非懒加载的单例bean createNonLazySingleton(beanDefinitionMap); } private void createNonLazySingleton(Map<String, BeanDefinition> beanDefinitionMap) { for (String beanName : beanDefinitionMap.keySet()) { BeanDefinition beanDefinition = beanDefinitionMap.get(beanName); if(beanDefinition.getScope().equals("singleton") && !beanDefinition.isLazy()) { //创建bean Object valueBean = createBean(beanDefinition); singletonObj.put(beanName,valueBean); } } } private Object createBean(BeanDefinition beanDefinition) { //通过反射获取到bean对象 Class beanClass = beanDefinition.getBeanClass(); //getDeclaredConstructor:获取到无参到构造方法 try { Object newInstance = beanClass.getDeclaredConstructor().newInstance(); return newInstance; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } return null; } private static void scan(Class configClass, Map<String, BeanDefinition> beanDefinitionMap) { if (configClass.isAnnotationPresent(ComponentScan.class)) { //获取ComponentScan的信息 ComponentScan componentScanAnnotation = (ComponentScan) configClass.getAnnotation(ComponentScan.class); String path = componentScanAnnotation.value();//获取扫描路径,com.csw.spring.service.impl //替换com.csw.spring.service.impl为com/csw/spring/service/impl path = path.replace(".","/"); //获取类加载后文件路径 ClassLoader classLoader = CswApplicationContext.class.getClassLoader(); URL resource = classLoader.getResource(path); System.out.println("根据类加载获取到完整路径:" + resource); File file = new File(resource.getFile()); //对获取对文件路径进行循环遍历 for (File f : file.listFiles()) { //获取path路径:/Users/shengwencheng/Desktop/cloud/SpringCore/spring-core/Spring/target/classes/com/csw/spring/service/impl/UserServiceImpl.class String s = f.getAbsolutePath(); //只要class结尾的文件 if(s.endsWith(".class")) { //截取com到.class之间到路径 s = s.substring(s.indexOf("com"),s.indexOf(".class")); //把转义替换成. s = s.replace("/","."); System.out.println("通过字符串截取和转义替换或取到的类:" + s); try { Class clazz = classLoader.loadClass(s); //判断类上是否有Component注解 if (clazz.isAnnotationPresent(Component.class)) { BeanDefinition beanDefinition = new BeanDefinition(); //声明bean类型 beanDefinition.setBeanClass(clazz); Component componentAnnotation = (Component) clazz.getAnnotation(Component.class); System.out.println("获取到加了Component注解到类:" + componentAnnotation); String beanName = componentAnnotation.value(); System.out.println("获取Component注解里面的value:" + beanName); //判断是否是非懒加载的bean if (clazz.isAnnotationPresent(Lazy.class)) { beanDefinition.setLazy(true); } //判断是否是原型bean if (clazz.isAnnotationPresent(Scope.class)) { Scope scopeAnnotation = (Scope) clazz.getAnnotation(Scope.class); String value = scopeAnnotation.value(); beanDefinition.setScope(value); } else { //单例 beanDefinition.setScope("singleton"); } //这里包括了项目中所有bean的定义,根据beanName在缓存里面去查找有没有传入的这个bean对象 beanDefinitionMap.put(beanName,beanDefinition); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } } public Object getBean(String beanName) { if(!beanDefinitionMap.containsKey(beanName)) { throw new NullPointerException(); } else { BeanDefinition beanDefinition = beanDefinitionMap.get(beanName); System.out.println("存放解析后的bean缓存池:" + beanDefinition.getScope()); //判断是原型bean还是单例bean if(beanDefinition.getScope().equals("prototype")) { //创建一个bean Object bean = createBean(beanDefinition); return bean; } else if(beanDefinition.getScope().equals("singleton")) { //单例池里面获取单例bean Object o = singletonObj.get(beanName); return o; } } return null; } }

测试结果: 到这里,spring基本核心逻辑已经完成了,但是业务层还缺少了通过注解注入方式。。。

实现@Autowired的依赖注入

@Autowired,默认先根据byType查询bean的依赖类型是否存在,存在则再根据byName进行注入

package com.csw.spring.anno; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD,ElementType.CONSTRUCTOR,ElementType.METHOD,ElementType.PARAMETER,ElementType.ANNOTATION_TYPE}) @Documented public @interface Autowired { boolean required() default true; }

实现: 课堂小知识??:单例模式和单例bean的区别 单例模式:只能有一个单例 单例bean:通过@Bean定义的bean对象,bean名不同即可

UserServiceImpl则如下内容,因为容器只实现了byName查找方式,没有具体写byType查找方式,所以直接根据bean查找

package com.csw.spring.service.impl; import com.csw.spring.anno.Autowired; import com.csw.spring.anno.Component; import com.csw.spring.anno.Scope; import com.csw.spring.service.OrderService; import com.csw.spring.service.UserService; @Component("userServiceImpl") @Scope("prototype") public class UserServiceImpl implements UserService { @Autowired private OrderServiceImpl orderServiceImpl; public String test() { return orderServiceImpl.getOrder(); } }

运行结果: 到这里,我们Spring的基本运行逻辑就模拟成功了,但是如果是如下内容,我们是无法通过@authwired或者@resource去实现,这里就涉及到Spring的扩展点了,BeanNameAware

@Autowired private String beanName; 实现BeanNameAware接口

BeanNameAware是Spring的扩展接口,主要是实现setBeanName()方法,内容很简单,如果有实现了BeanNameAware接口则回调

模拟定义BeanNameAware接口 package com.csw.spring.framework; /** * @author csw * @version 1.0 * @date 2021/12/19 11:20 下午 */ public interface BeanNameAware { void setBeanName(String name); }

实现BeanNameAware接口

package com.csw.spring.service.impl; import com.csw.spring.anno.Autowired; import com.csw.spring.anno.Component; import com.csw.spring.anno.Scope; import com.csw.spring.framework.BeanNameAware; import com.csw.spring.service.OrderService; import com.csw.spring.service.UserService; /** * @author csw * @version 1.0 * @date 2021/12/19 11:20 下午 */ @Component("userServiceImpl") @Scope("prototype") public class UserServiceImpl implements UserService, BeanNameAware { @Autowired private OrderServiceImpl orderServiceImpl; private String beanName; public String test() { System.out.println("beanName:" + beanName); return orderServiceImpl.getOrder(); } public void setBeanName(String name) { this.beanName = name; } }

模拟BeanNameAware的逻辑 运行结果: 这里再补充一个InitializingBean初始化的接口,主要是实现afterPropertiesSet方法,用于对已经注入了的属性进行认证的扩展接口,包括后面整合Spring + mybatis源码篇会说到,这里做一个铺垫

定义InitializingBean接口 package com.csw.spring.framework; /** * @author csw * @version 1.0 * @date 2021/12/19 11:45 下午 */ public interface InitializingBean { public void afterPropertiesSet(); } 实现InitializingBean并重写afterPropertiesSet方法 package com.csw.spring.service.impl; import com.csw.spring.anno.Autowired; import com.csw.spring.anno.Component; import com.csw.spring.anno.Scope; import com.csw.spring.framework.BeanNameAware; import com.csw.spring.framework.InitializingBean; import com.csw.spring.service.OrderService; import com.csw.spring.service.UserService; @Component("userServiceImpl") @Scope("prototype") public class UserServiceImpl implements UserService, BeanNameAware, InitializingBean { @Autowired private OrderServiceImpl orderServiceImpl; private String beanName; public String test() { System.out.println("beanName:" + beanName); return orderServiceImpl.getOrder(); } public void setBeanName(String name) { this.beanName = name; } public void afterPropertiesSet() { System.out.println("这里主要是对已经注入了的属性进行认证,内容随便写,扩展接口"); } }

具体实现逻辑: 运行结果: 这里再补充一个比较重要的点,BeanPostProcessor,Bean的后置处理器 - > 处理实例化后的bean对象,场景使用:

@Autowired是由AutowiredAnnotationBeanPostProcessor类实现的。@Resource是由CommonAnnotationBeanPostProcessor实现的。 包括我们可以自定义一个注解,实现依赖注入,这说明了Spring很灵活性

以上就是Spring核心逻辑的一个模拟,为后续学习Spring源码做一个铺垫,以上就是个人对Spring的一个理解 + 总结,并非Spring真正的写法,喜欢的朋友点个?关注 代码地址:手写模拟Spring框架核心逻辑代码地址


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。