模拟spring用dom4j解析xml(转载)

1.spring介绍

Spring 是什么?
     Spring是一个开源的控制反转(Inversion of Control ,IoC)和面向切面(AOP)的容器框架。它的主要目的是简化企业开发。

控制反转(Inversion of Control,IoC)?
     应用本身不负责依赖对象的创建及维护,依赖对象的创建和维护是由外部容器负责的。这样控制权就有应用转移到了外部容器,控制权的转移就是所谓的反转。
 
依赖注入(Dependency Injection)?
     在运行期,由外部容器动态地将依赖对象注入到组件中。

Spring优点? 
     1.降低组件之间的耦合度,实现软件各层之间的解耦。
     2.sprig提供了众多的服务。例如事物服务、JMS服务、持久化服务。
     3.容器提供单例模式支持,开发人员不需要自己编写实现代码。
     4.容器提供了AOP技术,利用它很容易实现如权限拦截、运行期监控等功能。
     5.容器提供的众多辅助类,使用这些类能够加开应用的开发,如:jdbcTemplate、HibernateTemplate.
     6.Spring对于主流的应用框架提供了集成支持,如:集成hibernate、JPA、struts等这样更便于应用的开发。

搭建spring开发环境

spring必须用到的jar:
    \dist\spring.jar
    \lib\jakarta-commons\commons-logging.jar
    如果使用了切面编程(AOP),还需要下了jar文件:
    \lib\aspectj\aspectjrt.jar 和\lib\aspectj\aspectjweaver.jar
   \lib\cglib\cglib-nodep-2.1_3.jar
   如果使用到了JSR-250中的注解,如果@Resource/@PostConstruct/@Predestroy,还需要下列jar文件
   \lib\j2ee\common-annotations.jar

如果PC不能联网,在spring配置文件下面不能提示信息,如果要提示信息则需要在eclipse中配置。
window-->preferences-->XML-->XML Catalog

   Location:选择本地的xsd文件(xsd文件在\dist\resources\目录下)。即“E:\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd”
   Key type:选择Schema location
   Key:在“http://www.springframework.org/schema/beans”后面加上xsd的文件名称。即“\spring-beans-2.5.xsd”


//1.在类路径下寻找配置文件来实例化容器
     ApplicationContext cxt = new  ClassPathXmlApplicationContext(new String[]{"beans.xml"});
    PersonService personService = (PersonService)cxt.getBean("personService");
       personService.save();
//2.在文件系统路径下寻找配置文件来实例化容器
     ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{"d:\\beans.xml"});

模仿spring的ApplicationContext读取配置文件

用到dom4j的dom4j-1.6.1.jar和lib\jaxen-1.1-beta-6.jar

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

public class ClassPathXMLApplicationContext {

     List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
     Map<String,Object> singletons = new HashMap<String, Object>();
 
 public ClassPathXMLApplicationContext(String fileName){
      this.readXML(fileName);
      this.instanceBeans();
 }

 /**
  * bean实例化
  */
 private void instanceBeans() {
  for(BeanDefinition beanDefine :beanDefines){
   try {
        if(beanDefine.getClassName() != null && !"".equals(beanDefine.getClassName().trim())){
             singletons.put(beanDefine.getId(), Class.forName(beanDefine.getClassName()).newInstance());
        }
   } catch (Exception e) {
        e.printStackTrace();
   }
  }
 
 }


 /**
  * 读取XML文件
  * @param fileName
  */
 private void readXML(String fileName) {
      SAXReader saxReader = new SAXReader();
      Document document = null;
  
  try {
       URL xmlPath =this.getClass().getClassLoader().getResource(fileName);
       document = saxReader.read(xmlPath);
       Map<String,String> nsMap = new HashMap<String,String>();
       //加命名空间
       nsMap.put("ns", "http://www.springframework.org/schema/beans ");
       //创建beans/bean查询路径
       XPath xsub = document.createXPath("//ns:beans/ns:bean");
       //设置命名空间
       xsub.setNamespaceURIs(nsMap);
       //获取文档下所有bean节点
       List<Element> beans = xsub.selectNodes(document);
       for(Element element:beans){
            String id = element.attributeValue("id");
            String clazz = element.attributeValue("class");
            BeanDefinition beanDefine = new BeanDefinition(id, clazz);
            beanDefines.add(beanDefine);
   }
  
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
 public Object getBean(String beanName){
  return singletons.get(beanName);
 }
}

 BeanDefinition 中有两个字段 id和class  这两个字段的get、set方法 构造方法

转载之  http://www.cnblogs.com/yuxuan/archive/2011/6/8.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值