IOC
IOC编写
- 加载配置文件
- 解析配置文件
- 存放到容器
这个高仿spring代码,类的命名和spring的一样,简化功能的实现。
com.cn.spring.context.ClassPathXmlApplicationContext
// 加载配置文件入口
public ClassPathXmlApplicationContext(String configLocation) throws Exception {
// configLocation 配置文件的地址 AutowireCapableBeanFactory 自动配置
// AutowireCapableBeanFactory后面分析.............
this(configLocation, new AutowireCapableBeanFactory());
}
com.cn.spring.context.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String, com.cn.spring.beans.factory.AbstractBeanFactory)
public ClassPathXmlApplicationContext(String configLocation, AbstractBeanFactory beanFactory) throws Exception {
super(beanFactory);
this.configLocation = configLocation;
refresh();
}
com.cn.spring.context.AbstractApplicationContext#AbstractApplicationContext
public AbstractApplicationContext(AbstractBeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
com.cn.spring.context.AbstractApplicationContext#refresh
public void refresh() throws Exception {
// 加载定义的bean
loadBeanDefinitions(beanFactory);
// 注册
registerBeanPostProcessors(beanFactory);
onRefresh();
}
// 加载配置文件开始
// 委托设计模式 有对应的实现类去实现
protected abstract void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception;
com.cn.spring.context.ClassPathXmlApplicationContext#loadBeanDefinitions
protected void loadBeanDefinitions(AbstractBeanFactory beanFactory) throws Exception {
// 获取xml解析器
XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(new ResourceLoader());
// 加载、解析、存入bean 该方法是总个框架的核心方法
xmlBeanDefinitionReader.loadBeanDefinitions(configLocation);
for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : xmlBeanDefinitionReader.getRegistry().entrySet()) {
beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue());
}
}
com.cn.spring.beans.xml.XmlBeanDefinitionReader#loadBeanDefinitions
public void loadBeanDefinitions(String location) throws Exception {
// 加载资源
InputStream inputStream = getResourceLoader().getResource(location).getInputStream();
// 解析xml
doLoadBeanDefinitions(inputStream);
}
com.cn.spring.beans.xml.XmlBeanDefinitionReader#doLoadBeanDefinitions
protected void doLoadBeanDefinitions(InputStream inputStream)throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.parse(inputStream);
// 解析bean
registerBeanDefinitions(doc);
inputStream.close();
}
解析xml 开始啦…>>>>>>>
protected void registerBeanDefinitions(Document doc) {
Element root = doc.getDocumentElement();
parseBeanDefinitions(root);
}
protected void parseBeanDefinitions(Element root) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
processBeanDefinition(ele);
}
}
}
处理属性…
protected void processProperty(Element ele, BeanDefinition beanDefinition) {
NodeList propertyNode = ele.getElementsByTagName("property");
for (int i = 0; i < propertyNode.getLength(); i++) {
Node node = propertyNode.item(i);
if (node instanceof Element) {
Element propertyEle = (Element) node;
String name = propertyEle.getAttribute("name");
String value = propertyEle.getAttribute("value");
if (value != null && value.length() > 0) {
beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name, value));
} else {
String ref = propertyEle.getAttribute("ref");
if (ref == null || ref.length() == 0) {
throw new IllegalArgumentException("Configuration problem: <property> element for property '"
+ name + "' must specify a ref or value");
}
BeanReference beanReference = new BeanReference(ref);
beanDefinition.getPropertyValues().addPropertyValue(new PropertyValue(name, beanReference));
}
}
}
}
protected void processBeanDefinition(Element ele) {
String name = ele.getAttribute("id");
String className = ele.getAttribute("class");
BeanDefinition beanDefinition = new BeanDefinition();
processProperty(ele, beanDefinition);
beanDefinition.setBeanClassName(className);
// 存入bean
getRegistry().put(name, beanDefinition);
}