Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2511222
  • 博文数量: 319
  • 博客积分: 9650
  • 博客等级: 中将
  • 技术积分: 3881
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 21:05
文章分类

全部博文(319)

文章存档

2017年(5)

2016年(10)

2015年(3)

2014年(3)

2013年(10)

2012年(26)

2011年(67)

2010年(186)

2009年(9)

分类: Java

2010-06-02 19:59:30

Xml代码
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns=""  
  3.         xmlns:xsi=""  
  4.         xmlns:context=""  
  5.         xmlns:tx=""  
  6.         xsi:schemaLocation=" /spring-beans-2.5.xsd   
  7.                  /spring-context-2.5.xsd   
  8.                  /spring-tx-2.5.xsd">  
  9.         <bean id="userBean" class="com.szy.spring.implbean.UserBean" />  
  10. beans>  

Spring的配置文件中记录了类的包路径,因此我们首先是要读入配置文件。在配置文件中Bean有id和class两个属性,我们首先定义一个Bean类,包含这两个属性:

 

Java代码
  1. package com.szy.spring.implbean;   
  2.   
  3. public class Bean   
  4. {   
  5.     private String id;   
  6.     private String className;   
  7.     public String getId()   
  8.     {   
  9.         return id;   
  10.     }   
  11.     public void setId(String id)   
  12.     {   
  13.         this.id = id;   
  14.     }   
  15.     public String getClassName()   
  16.     {   
  17.         return className;   
  18.     }   
  19.     public void setClassName(String className)   
  20.     {   
  21.         this.className = className;   
  22.     }   
  23.        
  24. }   
  25.    

由于配置文件是xml文件,在这里使用Jdom包操作xml文件,读入配置文件中的Bean信息。

Java代码
  1. /**  
  2.      * 读取xml配置文件  
  3.      * @param fileName 配置文件名  
  4.      */  
  5.     private void readXML(String fileName)   
  6.     {   
  7.         // 寻找配置文件   
  8.         URL xmlPath = this.getClass().getClassLoader().getResource(fileName);   
  9.         Document doc = null;   
  10.         Element root = null;   
  11.         try  
  12.         {   
  13.             SAXBuilder sb = new SAXBuilder(false);   
  14.             doc = sb.build(new FileInputStream(new File(xmlPath.toURI())));   
  15.             // 设置命名空间   
  16.             Namespace xhtml = Namespace.getNamespace("xhtml",   
  17.                     "");   
  18.             root = doc.getRootElement(); // 获取根元素   
  19.             List list = root.getChildren("bean", xhtml); //获取全部bean节点   
  20.             for (Element element : list)// 遍历节点,取得每个节点的属性   
  21.             {   
  22.                 String id = element.getAttributeValue("id");   
  23.                 String className = element.getAttributeValue("class");   
  24.                 Bean bean = new Bean();   
  25.                 bean.setId(id);   
  26.                 bean.setClassName(className);   
  27.                 beanList.add(bean);   
  28.             }   
  29.         } catch (Exception e)   
  30.         {   
  31.             e.printStackTrace();   
  32.         }   
  33.   
  34.     }  

 其中beanList是一个List对象,因为在配置文件中存在很多Bean。

 

得到了所有的Bean对象后,下面就实例化每个Bean对象,结果存放在Map对象中。

 

Java代码
  1. /**  
  2.      * bean的实例化  
  3.      */  
  4.     private void instanceBeans()   
  5.     {   
  6.         for (Bean bean : beanList)   
  7.         {   
  8.             try  
  9.             {   
  10.                 if (bean.getClassName() != null && !"".equals(bean.getClassName().trim()))   
  11.                     beanObject.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());   
  12.             } catch (Exception e)   
  13.             {   
  14.                 e.printStackTrace();   
  15.             }   
  16.         }   
  17.   
  18.     }  

 其中beanObject为Map对象。

 

最后再加入一个方法,方便外部能获取Map中的对象

Java代码
  1. /**  
  2.      * 获取bean实例  
  3.      * @param beanName 配置文件中bean的Id  
  4.      * @return  
  5.      */  
  6.     public Object getBean(String beanName)   
  7.     {   
  8.         return this.beanObject.get(beanName);   
  9.     }  

 完整的MyClassPathXMLApplicationContext见附件中的代码。

 

下面测试MyClassPathXMLApplicationContext类。

Java代码
  1. @Test  
  2.     public void testMethod() throws Exception   
  3.     {   
  4.         //读取配置文件   
  5.         MyClassPathXMLApplicationContext ctx=new MyClassPathXMLApplicationContext("applicationContext.xml");   
  6.         //获取UserBean的实例   
  7.         PersonBean bean=(PersonBean)ctx.getBean("userBean");   
  8.         //调用方法   
  9.         bean.show();   
  10.     }  

输出结果

结果代码 复制代码
  1. Hello Kuka  

成功。

上面仅是简单的演示了Spring管理Bean的原理,但是在实际操作中还需要考虑很对其它因素。

  • (2.9 MB)
阅读(1922) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~