Spring的主要功能是控制反转和面向切面编程,下面我们就来编写第一个spring的程序来体验一下控制反转
首先是加载配置文件
下面我们在程序中加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
然后新建类
package com.service.impl;import com.service.Service;public class ServiceBean implements Service {@Overridepublic void save(){System.out.println("save()");}}
抽取出类的接口 refactor----->extract interface
然后在测试方法中控制反转
package junit.test;import static org.junit.Assert.*;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.service.Service;public class SpringTest {@Testpublic void test() {ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");Service service = (Service)ac.getBean("service");service.save();}}
下面我们来看看是怎么控制反转的吧
首先新建类来解析配置文件,然后把配置文件中的bean中的属性提取出来创建对象,通过相应的方法刚返回给页面
1 package com.dom4j.test; 2 3 import java.io.File; 4 import java.util.HashMap; 5 import java.util.Iterator; 6 import java.util.Map; 7 import java.util.Set; 8 9 import org.dom4j.Attribute;10 import org.dom4j.Document;11 import org.dom4j.DocumentException;12 import org.dom4j.DocumentHelper;13 import org.dom4j.Element;14 import org.dom4j.io.SAXReader;15 16 import com.service.Service;17 import com.sun.beans.decoder.DocumentHandler;18 19 public class DomeStuTest {20 21 private Mapmap = new HashMap ();22 private Map objMap = new HashMap ();23 24 public DomeStuTest(String xml){25 String xmlPath = getClass().getClassLoader().getResource(xml).getPath();26 Document document = null;27 SAXReader reader = new SAXReader();28 29 try {30 document = reader.read(new File(xmlPath));31 } catch (DocumentException e) {32 e.printStackTrace();33 }34 35 //获取根节点36 Element root = document.getRootElement();37 38 //获取根节点下的子节点39 Iterator it = root.elementIterator("bean");40 while(it.hasNext()){41 Element ele = (Element)it.next();42 Attribute attr_name = ele.attribute("name");43 Attribute attr_class = ele.attribute("class");44 45 map.put(attr_name.getValue(), attr_class.getValue());46 }47 48 //实例化容器内的对象并存储49 Set set = map.keySet();50 Iterator its = set.iterator();51 while(its.hasNext()){52 String key = (String)its.next();53 String value = map.get(key);54 try {55 Class clazz = Class.forName(value);56 Object obj = clazz.newInstance();57 objMap.put(key, obj);58 } catch (ClassNotFoundException e) {59 e.printStackTrace();60 } catch (InstantiationException e) {61 e.printStackTrace();62 } catch (IllegalAccessException e) {63 e.printStackTrace();64 }65 66 }67 68 }69 70 //取得对象71 public Object get(String name){72 Object obj = null;73 Set set = objMap.keySet();74 Iterator it = set.iterator();75 while(it.hasNext()){76 String key = (String)it.next();77 if(key.equals(name)){78 obj = objMap.get(key);79 }80 }81 return obj;82 }83 84 }
然后在页面这样就可以获得对象
package junit.test;import org.junit.Test;import com.dom4j.test.DomeStuTest;import com.service.Person;import com.service.Service;public class SpringTest { @Test public void test() {// ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");// Service service = (Service)ac.getBean("service");// service.save(); DomeStuTest dst = new DomeStuTest("spring.xml"); Service service = (Service)dst.get("service"); service.save(); Person person = (Person)dst.get("person"); person.showMessage(); }}