Spring(二)编码剖析Spring对JavaBean的管理

来源:互联网 发布:漓江学院 知乎 编辑:程序博客网 时间:2024/06/06 04:57

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

用实体类保存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 stubfor (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 stubSAXReader 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 exceptione.printStackTrace();}}public Object getBean(String beanName) {return this.sigletons.get(beanName);}}


实例化Spring容器常用的两种方式:
一、在类路径下寻找配置文件来实例化容器
ApplicationContext ctx=new ClassPathXmlApplicationContext(new String[]{"beans.xml"});
Spring的配置文件可以指定多个,可以通过String数组传入。
二、在文件系统路径下寻找配置文件来实例化容器
ApplicationContext ctx=new FileSystemXmlApplicationContext(new String[]{"d:\\beans.xml"});
因为不同操作系统的设置可能有所不同,第二种方式通用性比较差,故比较少用。


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

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

0 0
原创粉丝点击