From 79005657d4d56697724e5e2d8a18322d4ace1200 Mon Sep 17 00:00:00 2001 From: szh <1220298025@qq.com> Date: Sun, 5 Dec 2021 19:13:48 +0800 Subject: [PATCH] 0.2ver --- README.md | 40 +++++++++++++----- .../java/springframework/BeanFactory.java | 18 ++------ src/test/java/Test.java | 23 +++++++--- src/test/java/pojo/User.java | 2 +- .../springframework/BeanDefinition.class | Bin 486 -> 0 bytes .../classes/springframework/BeanFactory.class | Bin 961 -> 178 bytes target/test-classes/Test.class | Bin 708 -> 1309 bytes target/test-classes/pojo/User.class | Bin 467 -> 467 bytes 8 files changed, 50 insertions(+), 33 deletions(-) delete mode 100644 target/classes/springframework/BeanDefinition.class diff --git a/README.md b/README.md index b94fcb9..6b8f390 100644 --- a/README.md +++ b/README.md @@ -16,26 +16,44 @@ -## 0.1ver +## 0.2ver -最简单的实现,只包含一个容器 +改动0.1ver,添加实现以下内容: -![image-20211205152755313](https://gitee.com/mrth4869/pic/raw/master/20211205152755.png) +1.**将对象的创建交给容器(实例化)** -先不考虑bean定义信息的获取、信息存放、实例化问题以及bean的存放等问题 +2.**以单例模式从缓存中获取对象** -只考虑: +3.**异常** -1.**托管对象**(托管指对象已经存在) - -2.**获取对象** +4.**更好的扩展性** #### 设计思路 -主要考虑托管的实现,即存在多个对象的时候必须有某种数据结构来存放; +**容器创建对象(实例化)可以通过反射来实现**: + +1. 将获取到的Class对象,封装成类信息BeanDefinition,注册(存放)到一个注册表BeanDefinitionRegistry中 +2. 从注册表中获取类信息,通过反射创建对象 + +**单例模式与缓存的实现:** + +使用HashMap作为缓存(单例注册表)SingletonBeanRegistry,存放实例化的对象; + +实例化对象前先去检查缓存,缓存有,则取缓存;反之则实例化新对象; + +**更好的扩展性:** + +![image-20211205174359137](https://gitee.com/mrth4869/pic/raw/master/20211205174359.png) + +
接口只定义核心的功能,由抽象类实现并规范附加功能,直到某个实现类完善所有未实现的功能
+ + + +例如对于BeanFactory,其核心方法为`getBean`,那么不妨将其制成接口,将此方法交由抽象类AbstractBeanFactory实现,而AbstractBeanFactory本身又规范了附加功能; -我们希望通过名字就可以进行管理,那么键值对就是个很好的选择,即存放到map中; +又例如对于BeanDefiinitionRegistry类信息注册表而言,其核心是提供`registerBeanDefinition`方法,故可以制成接口 -我们希望其拥有较好的获取/查找的效率,那么可以选择用HashMap用于存放对象 +又例如对于SingletonBeanRegistry单例注册表(单例缓存)而言,其核心是提供`getSingleton`方法,故也可以制成接口; +![image-20211205175044261](https://gitee.com/mrth4869/pic/raw/master/20211205175044.png) \ No newline at end of file diff --git a/src/main/java/springframework/BeanFactory.java b/src/main/java/springframework/BeanFactory.java index ae07dc3..3dd5044 100644 --- a/src/main/java/springframework/BeanFactory.java +++ b/src/main/java/springframework/BeanFactory.java @@ -1,24 +1,12 @@ package springframework; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; /** * @Author:吉口吉 * @Date:Created in 14:17 2021/12/5 - * @Description: 容器,用HashMap托管bean + * @Description: */ -public class BeanFactory { - - private Map beanMap = new ConcurrentHashMap<>(); - - public Object getBean(String name) {//bean的获取 - return beanMap.get(name); - } - - public void registerBean(String name, Object bean) {//托管bean - beanMap.put(name, bean); - } - +public interface BeanFactory {//容器 + public Object getBean(String name); } diff --git a/src/test/java/Test.java b/src/test/java/Test.java index 5357f74..1120653 100644 --- a/src/test/java/Test.java +++ b/src/test/java/Test.java @@ -1,14 +1,25 @@ import pojo.User; import springframework.BeanFactory; +import springframework.config.BeanDefinition; +import springframework.support.DefaultListableBeanFactory; public class Test { public static void main(String[] args){ - //1.初始化容器BeanFactory - BeanFactory beanFactory=new BeanFactory(); - //2.托管bean - beanFactory.registerBean("user",new User()); + //1.初始化容器 + DefaultListableBeanFactory factory=new DefaultListableBeanFactory(); + //2.注册bean的定义信息(class对象) + BeanDefinition beanDefinition=new BeanDefinition(User.class); + factory.registerBeanDefinition("user",beanDefinition); //3.获取bean - User u=(User)beanFactory.getBean("user"); - u.UserSpeak(); + User u1=(User)factory.getBean("user"); + u1.userSpeak(); + //4.再次获取bean + User u2=(User)factory.getBean("user"); + u2.userSpeak(); + + System.out.println(u1); + System.out.println(u2); + if (u1==u2) System.out.println("两者内存地址相同"); + else System.out.println("两者内存地址不同"); } } diff --git a/src/test/java/pojo/User.java b/src/test/java/pojo/User.java index 363ea68..e32ea8d 100644 --- a/src/test/java/pojo/User.java +++ b/src/test/java/pojo/User.java @@ -1,5 +1,5 @@ package pojo; public class User { - public void UserSpeak(){System.out.println("你好");} + public void userSpeak(){System.out.println("你好");} } diff --git a/target/classes/springframework/BeanDefinition.class b/target/classes/springframework/BeanDefinition.class deleted file mode 100644 index edf25d01d21255c4a09cbf4f8fb57c5ebc77fb30..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 486 zcmaJ-%TB^T6g{^TE44i2<<961FtHnV8W(C8w=~h1xbOq~DC22i zqaqsBlW89>ha69jL@){R*_xJ2MyT1@ni*MB`DS zX8I{k?)@`<;X;qHFcU`%cE&o*KtZ$9U5^&H*YP45>b}Xs8o%|2*(4quv^^BDA=v#t zPf+_iyP(kN4zSNf*<75!%A8nPtQ~CfEV1jew|Kj~cS!Hx4c1G}ad}p#1-7X7$St!1 u+DkT(GCS&WSD{-4)s?5uTliM&y~28CVUY%gXJ9S2@s@;@4ZokWwtoO5M_~&9 diff --git a/target/classes/springframework/BeanFactory.class b/target/classes/springframework/BeanFactory.class index 00a34a2d14b12d88494193a909e6a0651763db12..c7c52b924e10de0bb9c0486c9edf9c0473894ffe 100644 GIT binary patch delta 63 zcmX@ezKK!b)W2Q(7#J8#7&zG(*x4C4CVnuTJdIIq@)t%!IW|THMg}IJECT~03nK$F O0}F`7zzQUp7}x;d@(LgT literal 961 zcmaKqTW`}q5QWd$CMJnPLZRhSTFR|WQbb-rLP`}RDpfoL`VdsaoE@IWi?FuZcmf>QpiaWS#mU}Ml zyLcer4wVdF%85X|e;_}~P7(*>4ijC0@*o(6GETG-cyjXZRu+zBIO+^y9fYH9A+tL? zP=4I)kx|+T!XWMm*v;0SfU_OFBdy*KLiH*+94h@<4#&(a^dn!6_oNO?oF|=lKbQ)v z^rsV3;=PuK>QkgYbe_}nj`ZV5e`XDLq+*j1SZ@|wYl(F*nX(7LMh}v^g`&lh9eg${EVFOJMEwlxe|1%;MoVS@d5~$5nF&=iC6Fpv%_@2)<2r!*7+51rIj&oCeoAGrcqh8to&9_D@)Ek6?d?^EFjBxSH1u zo^yESa=H^-8(6>#F=sua(rBygZ*Vq`alyhDh8D#@i6RhS5KEaXNALhM*Z5!H uI&S2!T^h5gt);Ms$0*r&bK2D+eJ!QPwM;xi)^a2PxQS&J6u3p4g_XZy$l1gI diff --git a/target/test-classes/Test.class b/target/test-classes/Test.class index 1bac093edc64d4cdafc141d9d001e6ee2e3c714a..4aff457393db64c092997548ff1a304b43a856ef 100644 GIT binary patch literal 1309 zcmb7EOHUI~6#i~$nYIiMTR>2JfxI6h6-5D20r8Q7CPkFE&`TZaK-+1iQw=*dxYER> ziMlhoGEo;mBr&f29W?qUe0;YhJd}kt%{}*=dmi6+&b{;X=cmsA`fx3XedyHC6@*90 zZVf#_cwzW)LYaEi5(&bGlL5M)3gR^S{WzoKSw99e3~Cq>C>^rWmOCs^($+pL;2pK^ zn*x(2S$B}pDQTqzYTNEa=jB6b zBxO2b#9YTpCk6}IrIVn4c#mTNZBE8r+gTLoi)J$_X3mi*^O5Z=7};DVV>_-fX3oi6 z(v4bKSHT!DWqQ0A#p)HaROz7zSvGOB{*@mAu|QS8XDN}Vy^{)F+1nP&2dY$C!jjeqe$xrAuJI2e+mN4e+KP|m({j_ zs%q!h_#8p&7{+-W7jRL>2*Mghb&TPXhH)L2aYdkRW9{|#rRDtN<@~$n`IqnWua>^O zSj#_qqRy}GdA_msl;F%z7U>Fn;cz|cWq4E_}yhWMXR&uLwl|bQ&5RJm%M;S@z z#8QDKRHBvSBDC}q*DyWDiToB3-z8?np|6M&2hl*ykJ~te!(}HY*HiZ lM9@R6k>9lA^sR^I7gPB(G;3(l(5j)0e?Vh+I~_de_zk7zOEdrg literal 708 zcmaJVZm}-1ZQPRQ0q;w`N0PM{9?wKdYA@ z6{+IPk3yYwP*Vw1TiW;Dyq$ToUfjs@ziFJWI^$Y3DjDClHH$I)S~E=|P!%MY{#=DJG8$Fi3t=7&HdOh=aY z&B&qpo|yCCXx8Q^F(xA%CH*A$lqy5S>m*~-Q^(rcty)J1)`Slq^8%~?k)!c3HOTs_ z=VJ%EK3Zt|*t2}c3JNUF&h@d|S3N?$QISqFWvoGg-PZlH|AS0f`PXnGl}$N24{+M| zNi=BB+8HT%jt$tGwod^xfqAZvSOtuNv9xyy@iT8Pa4kP)-N9}1}O_3(u1O`6%Ho}INm)gxZd W_pIS5RyeIm&NDpE&9Aakz}g?yeU|(H diff --git a/target/test-classes/pojo/User.class b/target/test-classes/pojo/User.class index 8779bc5fc926622cd2b930e2a80e559dbb6a8e1e..af0b54c0677008895e68fc8e13536a337a275340 100644 GIT binary patch delta 12 Tcmcc2e3^MdIb-R@ifBdvAzcKo delta 12 Tcmcc2e3^MdIb-O?ifBdvAiV^S