版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012788601/article/details/72870940
首先可以看一下咱们在前4篇博客的基础上初始化bean并获取bean对象的代码:
//1.读取配置
XmlBeanDefinitionReaderxmlBeanDefinitionReader=newXmlBeanDefinitionReader(newResourceLoader());
xmlBeanDefinitionReader.loadBeanDefinitions("tinyioc.xml");
//2.初始化BeanFactory并注册bean
AbstractBeanFactorybeanFactory=newAutowireCapableBeanFactory();
for(Map.Entry<String,BeanDefinition>beanDefinitionEntry:xmlBeanDefinitionReader.getRegistry().entrySet()){
beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(),beanDefinitionEntry.getValue());
}
//3.获取bean
HelloWorldServicehelloWorldService=(HelloWorldService)beanFactory.getBean("helloWorldService");
helloWorldService.helloWorld();
简单总结一下就是通过读取xml配置文件,完成bean类的初始化工作,然后再获取bean对象的方法等。
在现在的开发过程中,好像很少看到这样的代码对不对?在没有spirng注解和set注入bean的情况下,我们获取bean好像都是这么获取的:
ApplicationContextapplicationContext=newClassPathXmlApplicationContext("tinyioc.xml");
HelloWorldServicehelloWorldService=(HelloWorldService)applicationContext.getBean("helloWorldService");
helloWorldService.helloWorld();
现在对比两段代码发现区别了吧!现在看第二段代码是不是特别熟悉?
下边就是ioc引入application的过程。
于是我们引入熟悉的ApplicationContext接口,并在AbstractApplicationContext的refresh()方法中进行bean的初始化工作。
- 类图
- 时序图
- 关键代码(在前一篇博客的基础之上)
3·1 接口ApplicationContext:
package us.codecraft.tinyioc.context;
import us.codecraft.tinyioc.beans.factory.BeanFactory;
/**
* @author
*/
public interface ApplicationContext extends BeanFactory {
}
3·2抽象类AbstractApplicationContext:
package us.codecraft.tinyioc.context;
import us.codecraft.tinyioc.beans.factory.AbstractBeanFactory;
/**
* @author [email protected]
*/
public abstract class AbstractApplicationContext implements ApplicationContext {
protected AbstractBeanFactory beanFactory;
public AbstractApplicationContext(AbstractBeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void refresh() throws Exception{
}
@Override
public Object getBean(String name) throws Exception {
return beanFactory.getBean(name);
}
}
3·3实现类ClassPathXmlApplicationContext:
package us.codecraft.tinyioc.context;
import us.codecraft.tinyioc.beans.BeanDefinition;
import us.codecraft.tinyioc.beans.factory.AbstractBeanFactory;
import us.codecraft.tinyioc.beans.factory.AutowireCapableBeanFactory;
import us.codecraft.tinyioc.beans.io.ResourceLoader;
import us.codecraft.tinyioc.beans.xml.XmlBeanDefinitionReader;
import java.util.Map;
/**
* @author [email protected]
*/
public class ClassPathXmlApplicationContext extends AbstractApplicationContext {
private String configLocation;
public ClassPathXmlApplicationContext(String configLocation) throws Exception {
this(configLocation, new AutowireCapableBeanFactory());
}
public ClassPathXmlApplicationContext(String configLocation, AbstractBeanFactory beanFactory) throws Exception {
super(beanFactory);
this.configLocation = configLocation;
refresh();
}
@Override
public void refresh() throws Exception {
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(new ResourceLoader());
xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getRegistry().entrySet()) {
beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue());
}
}
}
3·4 测试类ApplicationContexxtTest:
package us.codecraft.tinyioc.context;
import org.junit.Test;
import us.codecraft.tinyioc.HelloWorldService;
/**
* @author [email protected]
*/
public class ApplicationContextTest {
@Test
public void test() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("tinyioc.xml");
HelloWorldService helloWorldService = (HelloWorldService) applicationContext.getBean("helloWorldService");
helloWorldService.helloWorld();
}
}
以上是该部分内容的关键代码,完整代码请点击《下载》
- 总结
至此为止,我们的的tiny-spring的IoC部分可说完工了。这部分的类、方法命名和作用,都是对应Spring中相应的组件。虽然代码量只有400多行,但是已经有了基本的IoC功能!
自总:
这个学习tiny-spring的过程中,对于spring的ioc容器有了进一步的了解。接下来,便是tiny-spring和spring的真实映射了。包括xml配置到注解的发展变化等。## 标题 ##