标签:
前导:开始学习Spring核心思想,通过一个山寨精简版Spriing代码结合学习。
内容:1. BeanDefinition-保存Bean及配置信息 2. BeanFactory-对Bean进行管理。
BeanDefinition:public class BeanDefinition { private Object bean; public BeanDefinition(Object object) { this.bean = object; } public Object getBean() { return this.bean; } }
BeanFactory:public class BeanFactory { private Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(); public Object getBean(String name) { return beanDefinitionMap.get(name).getBean(); } public void registerBeanDefinition(String name, BeanDefinition beanDefinition) { beanDefinitionMap.put(name, beanDefinition); } }
HelloWorldService:public class HelloWorldService { public void helloWorld() { System.out.println("Hello World"); } }
BeanFactoryTest:public class BeanFactoryTest { @Test public void test() { // 1. 初始化BeanFactory BeanFactory beanFactory = new BeanFactory(); // 2. 注入bean BeanDefinition beanDefinition = new BeanDefinition(new HelloWorldService()); beanFactory.registerBeanDefinition("helloWorldService", beanDefinition); // 3. 获取bean HelloWorldService helloWorldService = (HelloWorldService) beanFactory.getBean("helloWorldService"); helloWorldService.helloWorld(); } }
Spring核心学习(1)实现基本的容器-包括注入和获取功能
标签:
原文地址:http://blog.csdn.net/u011345136/article/details/45566325