码迷,mamicode.com
首页 > 编程语言 > 详细

自己实现spring IOC框架(超详细)

时间:2015-10-21 15:50:54      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:

1.IOC简介:

  控制反转即IoC (Inversion of Control),所谓控制反转就是应用本身不负责依赖对象的创建及对象之间关系的维护,而是由外部容器负责的。这样控制权就由应用转移到了外部容器,控制权的转移就是所谓反转。

  IoC是一个很大的概念,可以用不同的方式来实现。例如:<1>依赖查找(Dependency Lookup):容器提供回调接口和上下文环境给组件。EJB和Apache Avalon都使用这种方式。<2>依赖注入(Dependency Injection):组件不做定位查询,只提供普通的Java方法让容器去决定依赖关系。后者是时下最流行的IoC类型。

 

2.自己实现IOC

整体框架:

技术分享

首先需要读取配置文件:注意这里的配置文件没有schema.

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans>
 3     <!-- 将A配置到配置文件中 -->
 4     <bean id="A" class="cn.itcast.bean.A">
 5         <!-- 配置A的属性,Spring会自动将A的属性值注入 -->
 6         <property name="name" value="tom"/>
 7     </bean>
 8     <bean id="B" class="cn.itcast.bean.B">
 9         <!--ref表示要讲Bean A注入  -->
10         <property name="a" ref="A"></property>
11     </bean>
12 </beans>

Java是面向对象的语言,读取配置文件时我们需要Java Bean来封装这些信息。

 1 public class Bean {
 2     /**
 3      * 封装
 4      * <bean id="A" class="cn.itcast.bean.A">
 5      * ....若干个<property>
 6      * </bean>
 7      */
 8     private String id;
 9     
10     private String className;
11     
12     private List<Property> properties = new ArrayList<Property>();
13     
14     //setter.getter......
15 
16     @Override
17     public String toString() {
18         return "Bean [id=" + id + ", className=" + className + ", properties="
19                 + properties + "]";
20     }
21     
22 }
 1 public class Property {
 2     /**
 3      * 封装<property>
 4      * <property name="name" value="tom"/>
 5      * <property name="a" ref="A"></property>
 6      */
 7     private String name;
 8     
 9     private String value;
10     
11     private String ref;
12     
13     //setter.getter......
14     @Override
15     public String toString() {
16         return "Property [name=" + name + ", value=" + value + ", ref=" + ref
17                 + "]";
18     }
19     
20 
21 }

至此,用于封装配置文件中的信息的类已经写好了。下面就需要创建解析XML文件的类

 1 public class ConfigManager {
 2     
 3     //读取配置文件,返回读取结果。
 4     public static Map<String,Bean> getConfig(String path){
 5         //创建一个用于返回的Map对象
 6         Map<String,Bean> map = new HashMap<String,Bean>();
 7         //dom4j实现
 8             //1.创建解析器
 9             SAXReader reader = new SAXReader();
10             //2,加载配置文件=>document元素
11             InputStream is = ConfigManager.class.getResourceAsStream(path);
12             Document doc = null;
13             try {
14                 doc = reader.read(is);
15             } catch (DocumentException e) {
16                 e.printStackTrace();
17                 throw new RuntimeException("请检查您的xml配置文件是否正确!");
18             }
19             //3.定义xpath表达式,获取所有bean元素()
20             //从整个文档查找bean元素
21             String xpath = "//bean";
22             //4.对bean元素进行遍历
23             List<Element> list = doc.selectNodes(xpath);
24             if(list!=null&&list.size()>0){
25                 for(Element beanEle:list){
26                     Bean bean = new Bean();
27                     //把bean元素的id/class属性封装到Bean对象中
28                     String id = beanEle.attributeValue("id");
29                     String className = beanEle.attributeValue("class");
30                     bean.setId(id);
31                     bean.setClassName(className);
32                     //获取bean元素下的所有property子元素,把属性name/value/ref封装到Property对象中。
33                     List<Element> children = beanEle.elements("property");
34                     if(children!=null){
35                         for(Element child : children){
36                             Property prop = new Property();
37                             String pName = child.attributeValue("name");
38                             String pValue = child.attributeValue("value");
39                             String pRef = child.attributeValue("ref");
40                             prop.setName(pName);
41                             prop.setValue(pValue);
42                             prop.setRef(pRef);
43                             //把Property对象封装到Bean对象中
44                             bean.getProperties().add(prop);
45                         }
46                     }
47                     //把Bean对象封装到Map中(用户返回的Map)
48                     map.put(id, bean);
49                 }
50             }
51             //5.返回Map结果
52             return map;
53     }
54 }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

自己实现spring IOC框架(超详细)

标签:

原文地址:http://www.cnblogs.com/winner-0715/p/4897875.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!