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

Spring(二)Spring对JavaBean的管理

时间:2015-07-22 22:57:14      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

部分与第一篇重复的内容省掉了。

用实体类保存JavaBean的配置信息

package test.spring.entity;

public class Bean {

	private String id;
	private String classPath;
	public Bean(String id, String classPath) {
		super();
		this.id = id;
		this.classPath = classPath;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getClassPath() {
		return classPath;
	}
	public void setClassPath(String classPath) {
		this.classPath = classPath;
	}
	
}
编码模拟Spring读取beanx.xml中的数据对JavaBean进行管理的过程,关键是xml格式文件的读取
package test.spring.jnit;

import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.transform.sax.SAXResult;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

import test.spring.entity.Bean;

public class BeanManageTest {

	private List<Bean> listOfBean = new ArrayList<Bean>();
	private Map<String, Object> sigletons = new HashMap<String, Object>();

	public BeanManageTest(String fileName) {
		this.readXML(fileName);
		this.instanceBeans();
	}

	private void instanceBeans() {
		// TODO Auto-generated method stub
		for (Bean bean : listOfBean) {
			try {
				if (bean.getClassPath() != null
						&& !"".equals(bean.getClassPath().trim()))
					sigletons.put(bean.getId(),
							Class.forName(bean.getClassPath()).newInstance());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void readXML(String fileName) {
		// TODO Auto-generated method stub
		SAXReader saxReader = new SAXReader();
		Document document = null;
		try {
			URL xmlPath = this.getClass().getClassLoader()
					.getResource(fileName);
			document = saxReader.read(xmlPath);
			Map<String, String> map = new HashMap<String, String>();
			map.put("ns", "http://www.springframework.org/schema/beans");// 加入命名空间
			XPath xPath = document.createXPath("//ns:beans/ns:bean");
			xPath.setNamespaceURIs(map);
			List<Element> beans = xPath.selectNodes(document);
			for (Element element : beans) {
				String id = element.attributeValue("id");
				String classPath = element.attributeValue("class");
				Bean bean = new Bean(id, classPath);
				listOfBean.add(bean);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public Object getBean(String beanName) {
		return this.sigletons.get(beanName);
	}

}

结果一样,调用方式有所改变

package test.spring.jnit;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.spring.service.PersonService;

public class SpringTest {

	@Test
	public void testInstanceSpring() {
		// ApplicationContext applicationContext = new
		// ClassPathXmlApplicationContext(
		// new String[] { "beans.xml" });
		// ApplicationContext applicationContext = new
		// ClassPathXmlApplicationContext(
		// "beans.xml");
		// PersonService personService = (PersonService) applicationContext
		// .getBean("personService");// 得到的是Object,需进行转换

		BeanManageTest beanManageTest = new BeanManageTest("beans.xml");
		PersonService personService = (PersonService) beanManageTest
				.getBean("personService");// 得到的是Object,需进行转换

		personService.save();
	}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。如需转载,请注明出处:http://blog.csdn.net/lindonglian

Spring(二)Spring对JavaBean的管理

标签:

原文地址:http://blog.csdn.net/lindonglian/article/details/47009857

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