peanut996/blog-comment

mini-spring - PEANUT996

Opened this issue · 0 comments

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