spring框架的IOC
1.使用DOM4J读取xml配置文件
package com.dicmo.spring;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XMLPathApplicationContext implements BeanFactory{
Map <String,Object> container = new HashMap<String,Object>();
public XMLPathApplicationContext(String fileName) throwsDocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException{
File xmlFile = new File(fileName);
SAXReader reader = new SAXReader();
Document xmlDoc = reader.read(xmlFile);
List listRowSet = xmlDoc.selectNodes("//beans/bean");
for(Iterator i=listRowSet.iterator();i.hasNext();){
Element ele=(Element)i.next();
String id = ele.attributeValue("id");
String classs = ele.attributeValue("class");
Object clas = Class.forName(classs).newInstance();
container.put(id,clas);
}
}
public Object getBean(String id) {
return container.get(id);
}
}
2.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="spring" class="com.dicmo.spring.Spring"></bean>
<bean id="plane" class="com.dicmo.spring.Plane"></bean>
</beans>
3.test.java
package com.dicmo.spring;
import org.dom4j.DocumentException;
public class Test {
public static void main(String [] args) throws DocumentException, InstantiationException, IllegalAccessException, ClassNotFoundException{
XMLPathApplicationContext context = new XMLPathApplicationContext("G:/myeclpse6.5/spring/src/com/dicmo/spring/applicationContext.xml");
Spring spring = (Spring)context.getBean("spring");
spring.say();
Plane plane = (Plane)context.getBean("plane");
plane.say();
}
}
分享到:
相关推荐
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
这是个模拟spring ioc的学习例子,是个java工程。有助于对aop和ioc的理解
SpringIOC和AOP实现机制模拟,来自与网络。
NULL 博文链接:https://zhengshuo.iteye.com/blog/573192
Spring IOC AOP MVC 简单例子
NULL 博文链接:https://lgd-java2eye.iteye.com/blog/759878
自定义的ioc支持负责类型注入,对理解学习spring ioc 学习起到很好的作用!
模拟实现__Spring的Ioc 1、Spring主要两个作用:实例化Bean,动态装配Bean。并将所有的bean放到spring容器中,调用时从容器中取。Spring容器就是一个bean的Map:private Map, Object> beans = new HashMap, Object>...
基于xml注册,简单模拟spring ioc容器。快速理解ioc容器
自己写了一个Spring IoC的模拟,有详细注释,可供刚入门学习spring的童鞋加深理解.
手写springIOC 通过jdom读取xml文件 适合新手使用。
简单模拟spring ioc技术,一个小demo,希望对您理解ioc有所帮助.
该代码主要用于spring IOC的实现(墨者革离),模拟一个城门叩问的编剧场景
经过我整理了,写好了详细的注释,开发环境是myeclipse,希望对spring初学者能有帮助,大家相互帮助,谢谢!
练习共三部分: 1、XML读取,用于配置文件读取 2、模拟SPRING IOC 3、模拟STRUTS
基于java简单模拟实现spring_ioc
模拟实现spring IOC AOP的例子,如果你是eclipse开发工具,解压直接导入就可以,别忘了,导入dom4j的jar包。
NULL 博文链接:https://justsee.iteye.com/blog/1298861
/*模拟spring的实现 * 1.通过构造器获取配置文件 * 2.通过dom4j解析配置文件,得到list集合(存放Bean标签的id和class属性) * 3.通过反射实例化得到对应的实例化对象,放置在map中(map是键值对,可根据id获取值)...
模拟spring ioc aop的功能,理解spring ioc 和 aop 的实现原理和机制。