Spring入门学习(一)

时间:2021-10-24 10:15:17

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中的属性提取出来创建对象,通过相应的方法刚返回给页面

 package com.dom4j.test;

 import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set; import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; import com.service.Service;
import com.sun.beans.decoder.DocumentHandler; public class DomeStuTest { private Map<String,String> map = new HashMap<String,String>();
private Map<String,Object> objMap = new HashMap<String,Object>(); public DomeStuTest(String xml){
String xmlPath = getClass().getClassLoader().getResource(xml).getPath();
Document document = null;
SAXReader reader = new SAXReader(); try {
document = reader.read(new File(xmlPath));
} catch (DocumentException e) {
e.printStackTrace();
} //获取根节点
Element root = document.getRootElement(); //获取根节点下的子节点
Iterator it = root.elementIterator("bean");
while(it.hasNext()){
Element ele = (Element)it.next();
Attribute attr_name = ele.attribute("name");
Attribute attr_class = ele.attribute("class"); map.put(attr_name.getValue(), attr_class.getValue());
} //实例化容器内的对象并存储
Set<String> set = map.keySet();
Iterator its = set.iterator();
while(its.hasNext()){
String key = (String)its.next();
String value = map.get(key);
try {
Class clazz = Class.forName(value);
Object obj = clazz.newInstance();
objMap.put(key, obj);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} } } //取得对象
public Object get(String name){
Object obj = null;
Set<String> set = objMap.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
String key = (String)it.next();
if(key.equals(name)){
obj = objMap.get(key);
}
}
return obj;
} }

然后在页面这样就可以获得对象

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