peanut996 / blog-comment

0 stars 0 forks source link

mini-spring - PEANUT996 #26

Open peanut996 opened 1 year ago

peanut996 commented 1 year ago

https://peanut996.cn/notes/spring/mini-spring-doc

基础篇:IoC 最简单的bean容器 代码分支:simple-bean-container 定义一个简单的bean容器BeanFactory,内部包含一个map用以保存bean,只有注册bean和获取bean两个方法 public class BeanFactory { private Map<String, Object> beanMap = new HashMap<>(); public void registerBean(String name, Object bean) { beanMap.put(name, bean); } public Object getBean(String name) { return beanMap.get(name); } } 测试: public class SimpleBeanContainerTest { @Test public void testGetBean() throws Exception { BeanFactory beanFactory = new BeanFactory(); beanFactory.registerBean("helloService", new HelloService()); HelloService helloService = (HelloService) beanFactory.getBean("helloService"); assertThat(helloService).isNotNull(); assertThat(helloService.sayHello()).isEqualTo("hello"); } class HelloService { public String sayHello() { System.out.println("hello"); return "hello"; } } } BeanDefinition和BeanDefinitionRegistry 代码分支:bean-definition-and-bean-definition-registry