Skip to content

Commit

Permalink
0.2ver
Browse files Browse the repository at this point in the history
  • Loading branch information
s-zh committed Dec 5, 2021
1 parent 89748be commit 7900565
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 33 deletions.
40 changes: 29 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<center>接口只定义核心的功能,由抽象类实现并规范附加功能,直到某个实现类完善所有未实现的功能</center>



例如对于BeanFactory,其核心方法为`getBean`,那么不妨将其制成接口,将此方法交由抽象类AbstractBeanFactory实现,而AbstractBeanFactory本身又规范了附加功能;

我们希望通过名字就可以进行管理,那么<K,V>键值对就是个很好的选择,即存放到map中;
又例如对于BeanDefiinitionRegistry类信息注册表而言,其核心是提供`registerBeanDefinition`方法,故可以制成接口

我们希望其拥有较好的获取/查找的效率,那么可以选择用HashMap用于存放对象
又例如对于SingletonBeanRegistry单例注册表(单例缓存)而言,其核心是提供`getSingleton`方法,故也可以制成接口;

![image-20211205175044261](https://gitee.com/mrth4869/pic/raw/master/20211205175044.png)
18 changes: 3 additions & 15 deletions src/main/java/springframework/BeanFactory.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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);
}
23 changes: 17 additions & 6 deletions src/test/java/Test.java
Original file line number Diff line number Diff line change
@@ -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("两者内存地址不同");
}
}
2 changes: 1 addition & 1 deletion src/test/java/pojo/User.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package pojo;

public class User {
public void UserSpeak(){System.out.println("你好");}
public void userSpeak(){System.out.println("你好");}
}
Binary file removed target/classes/springframework/BeanDefinition.class
Binary file not shown.
Binary file modified target/classes/springframework/BeanFactory.class
Binary file not shown.
Binary file modified target/test-classes/Test.class
Binary file not shown.
Binary file modified target/test-classes/pojo/User.class
Binary file not shown.

0 comments on commit 7900565

Please sign in to comment.