Spring入门学习(一)

Spring的主要功能是控制反转和面向切面编程,下面我们就来编写第一个spring的程序来体验一下控制反转

 

首先是加载配置文件

 

<?xml version="1.0" encoding="UTF-8"?>

<!--

- Middle tier application context definition for the image database.

-->

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">



</beans>

 

 

下面我们在程序中加载配置文件

ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

 

然后新建类

package com.service.impl;



import com.service.Service;



public class ServiceBean implements Service {



@Override

public 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 {



@Test

public 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 Map<String,String> map = new HashMap<String,String>();

22     private Map<String,Object> objMap = new HashMap<String,Object>();

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<String> 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<String> 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();

    }



}

 

你可能感兴趣的:(Spring入门)