IoC(Inversion of Control)控制反转,对象创建责任的反转,在spring中BeanFacotory是IoC容器的核心接口,负责实例化,定位,配置应用程序中的对象及建立这些对象间的依赖。XmlBeanFacotory实现BeanFactory接口,通过获取xml配置文件数据,组成应用对象及对象间的依赖关系。
spring中有三种注入方式,一种是set注入,一种是接口注入,另一种是构造方法注入。
注:本实例是在idea软件上执行的,
一.导包:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.11.Final</version>
</dependency>
二.实体类
1 package com.yztc.ioc.bean; 2 3 /** 4 * @author 5 */ 6 public class User { 7 private Integer uid; 8 private String name; 9 public void init(){ 10 System.out.println("初始化对象"); 11 } 12 13 14 public User() { 15 } 16 17 public User(Integer uid, String name) { 18 this.uid = uid; 19 this.name = name; 20 } 21 22 public Integer getUid() { 23 return uid; 24 } 25 26 public void setUid(Integer uid) { 27 this.uid = uid; 28 } 29 30 public String getName() { 31 return name; 32 } 33 34 public void setName(String name) { 35 this.name = name; 36 } 37 }
三.配置beans.xml文件
1 <beans> 2 <bean id="user" class="com.yztc.ioc.bean.User"> 3 4 <property name="name" value="11111"/> 5 </bean> 6 </beans>
四.BeanFactory类
1 package com.yztc.ioc.bean; 2 3 import org.dom4j.Attribute; 4 import org.dom4j.Document; 5 import org.dom4j.DocumentException; 6 import org.dom4j.Element; 7 import org.dom4j.io.SAXReader; 8 9 import java.lang.reflect.Field; 10 import java.util.HashMap; 11 import java.util.List; 12 import java.util.Map; 13 14 /** 15 * @author bindu 16 * 17 * 18 */ 19 public class BeanFactory { 20 public static final String DEFAULT_XML_NAME ="beans.xml"; 21 private String xmlName; 22 private Map<String ,Object> beans = new HashMap<>(); 23 24 public BeanFactory() { 25 this.xmlName =DEFAULT_XML_NAME; 26 init(); 27 28 } 29 30 public BeanFactory(String xmlName) { 31 this.xmlName = xmlName != null&& !"".equals(xmlName)? xmlName:DEFAULT_XML_NAME; 32 init(); 33 } 34 35 /** 36 * 1.xml解析 37 * 2.实例化注册bean 38 */ 39 private void init(){ 40 SAXReader reader = new SAXReader(); 41 try { 42 //获得dom操作对象 43 Document document = reader.read(this.getClass().getClassLoader().getResourceAsStream(xmlName)); 44 //获得xml文件的根元素 45 Element root = document.getRootElement(); 46 //获得根元素下的所有子元素 47 List<Element> elements = root.elements(); 48 //遍历所有bean子元素 49 for (Element element : elements) { 50 //获得bean元素下的id元素 51 Attribute idAttr = element.attribute("id"); 52 //获得id元素属性的值 53 String id = idAttr.getValue(); 54 //获取bean元素下的class属性 55 Attribute claAttr = element.attribute("class"); 56 //获取class属性的值 57 String value = claAttr.getValue(); 58 //class对象实例化的三种方式 59 Class<?> cls = Class.forName(value); 60 Object o = cls.newInstance(); 61 List<Element> properties = element.elements("property"); 62 for (Element property : properties) { 63 Attribute nameAttr = property.attribute("name"); 64 String value1 = nameAttr.getValue(); 65 Field field = cls.getDeclaredField(value1); 66 //取消jvm运行时的访问检测机制 67 field.setAccessible(true); 68 Attribute valueAttr = property.attribute("value"); 69 String value2 = valueAttr.getValue(); 70 //给属性赋值 71 field.set(o,value2); 72 } 73 beans.put(id,o); 74 75 76 } 77 78 79 } catch (DocumentException e) { 80 e.printStackTrace(); 81 } catch (ClassNotFoundException e) { 82 e.printStackTrace(); 83 } catch (IllegalAccessException e) { 84 e.printStackTrace(); 85 } catch (InstantiationException e) { 86 e.printStackTrace(); 87 } catch (NoSuchFieldException e) { 88 e.printStackTrace(); 89 } 90 } 91 public <T> T getBean(String name){ 92 return (T) beans.get(name); 93 } 94 public <T> T getBean(String name ,Class<T> cls){ 95 return (T) beans.get(name); 96 } 97 98 99 }
五.测试类
1 package com.yztc.ioc; 2 3 import com.yztc.ioc.bean.BeanFactory; 4 import com.yztc.ioc.bean.User; 5 6 /** 7 * @author 8 */ 9 public class TestMain { 10 public static void main(String[] args) { 11 BeanFactory factory = new BeanFactory(); 12 User user1 = factory.getBean("user", User.class); 13 14 System.out.println(user1); 15 } 16 17 18 }