一、启动分析
SpringBoot工程中由@SpringBootApplication注释描述的类为启动入口

1 package com.cy; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class CacheApplication { 8 9 public static void main(String[] args) { 10 SpringApplication.run(CacheApplication.class, args); 11 } 12 13 }
SpringBoot工程启动时其简易初始化过程,如图:
在启动过程中底层做了哪些事情,大致描述如下:
1)基于配置加载类(通过ClassLoader将指定位置的类读到内存->底层通过线程调用IO从磁盘读取到内存)。
2)对类进行分析(创建字节码对象-Class类型,通过反射获取器配置信息)。
3)对于指定配置(例如由spring特定注解描述)的对象存储其配置信息(借助BeanDefinition对象存储)。
4)基于BeanDefinition对象中class的配置构建类的实例(Bean对象),并进行bean对象的管理(可能会存储到bean池)。
二、API设计分析
基于业务描述,进行API及关系设计,如图所示:
1、定义DefaultCache类

1 package com.cy.pj.common.cache; 2 import org.springframework.stereotype.Component; 3 /** 4 * @Component 注解描述的类,表示此类交给Spring框架管理。 5 */ 6 @Component 7 public class DefaultCache { 8 }
2、定义DefaultCacheTests单元测试类

1 package com.cy.pj.common.cache; 2 import org.junit.jupiter.api.Test; 3 import org.springframework.beans.factory.annotation.Autowired; 4 @SpringBootTest 5 public class DefaultCacheTests { 6 /** 7 * @Autowired 注解描述的属性由spring框架按照一定规则为其注入值(赋值) 8 */ @Autowired 9 private DefaultCache defaultCache; 10 @Test 11 void testDefaultCache(){ 12 System.out.println(defaultCache.toString()); 13 //defaultCache变量引用的对象是由spring框架创建的,存储到了bean pool 14 } 15 }