Chinaunix首页 | 论坛 | 博客
  • 博客访问: 272503
  • 博文数量: 124
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 21
  • 用 户 组: 普通用户
  • 注册时间: 2016-08-20 14:44
文章分类

全部博文(124)

文章存档

2020年(1)

2018年(2)

2016年(2)

2015年(6)

2014年(10)

2013年(23)

2012年(7)

2011年(18)

2010年(15)

2009年(8)

2007年(8)

2006年(23)

2005年(1)

我的朋友

分类: Java

2014-03-09 21:05:24

在spring的配置文件中,可以添加自定义配置格式;
设我们需要自动下面的配置文件 application.xml;

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <spring:beans xmlns:spring=""
  3.       xmlns:xsi=""
  4.       xmlns="test.namespce"
  5.       xsi:schemaLocation=" /spring-beans.xsd
  6.             test.namespce x.xsd">

  7.   <test id="oneBean" serviceClass="Bean"/>
  8. </spring:beans>
其目的自动生成 “com.test."+serviceClass的类; 该语句的命名空间是“test.namespce"; 注意xmlns="test.namespce"; 对于其中test部分的格式校验,我们定义了x.xsd; 这个说明是在xsi:schemaLocation=中(“其中记录了 namespace 和xsd文件对);

2. 添加自定义的x.xsd;

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <schema xmlns="" xmlns:tns="test.namespce" targetNamespace="test.namespce">
  3.     <element name="test" type="tns:testType"></element>
  4.     <complexType name="testType">
  5.         <attribute name="id" type="string"></attribute>
  6.         <attribute name="serviceClass" type="string"></attribute>
  7.     </complexType>
  8. </schema>

3. 根据spring的需求,需要配置handlers。告诉系统当遇到test.namespce命名空间的xml时,需要哪个类来处理;该文件位置为:META-INF/spring.handlers;其中记录:

点击(此处)折叠或打开

  1. test.namespce=com.test.spring.WjmNamespaceHandler
4. 编写com.test.spring.WjmNamespaceHandler

点击(此处)折叠或打开

  1. package com.test.spring;

  2. import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

  3. public class WjmNamespaceHandler extends NamespaceHandlerSupport {

  4.     @Override
  5.     public void init() {
  6.         // TODO Auto-generated method stub
  7.         registerBeanDefinitionParser("test",new MyParser());
  8.     }

  9. }
其中重载了一个init函数。该函数中记录,如果遇到test为root的element时,需要哪个类来处理;

5. 参见类org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver,得到解析3的文件的方法getHandlerMappings;调用4的代码的函数请参见的resolve方法;注意开始是mapping中放置的是namespace和类名,被使用过一次后就被替换为实际类了。

6. 编写MyParser;

点击(此处)折叠或打开

  1. package com.test.spring;

  2. import org.springframework.beans.factory.config.BeanDefinition;
  3. import org.springframework.beans.factory.config.BeanDefinitionHolder;
  4. import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
  5. import org.springframework.beans.factory.support.RootBeanDefinition;
  6. import org.springframework.beans.factory.xml.BeanDefinitionParser;
  7. import org.springframework.beans.factory.xml.ParserContext;
  8. import org.w3c.dom.Element;

  9. public class MyParser implements BeanDefinitionParser {

  10.     @Override
  11.     public BeanDefinition parse(Element element, ParserContext parserContext) {
  12.         // TODO Auto-generated method stub
  13.         RootBeanDefinition root = new RootBeanDefinition();
  14.         String class_name = element.getAttribute("serviceClass");
  15.         try {

  16.             Class bean_class = Class.forName("com.test." + class_name);
  17.             root.setBeanClass(bean_class);/* 设置需要创建的bean */

  18.             String id = element.getAttribute("id");
  19.             BeanDefinitionHolder idHolder = new BeanDefinitionHolder(root, id);
  20.             BeanDefinitionReaderUtils.registerBeanDefinition(idHolder, parserContext.getRegistry());/*这样getBean就可以获得对象了*/
  21.         } catch (ClassNotFoundException e) {
  22.             // TODO Auto-generated catch block
  23.             e.printStackTrace();
  24.         }
  25.         return root;
  26.     }
  27. }
参见org.springframework.beans.factory.xml.NamespaceHandlerSupport类的parse方法看到相关调用;
7. 写一个简单的Bean类:

点击(此处)折叠或打开

  1. package com.test;

  2. public class Bean {
  3.     String name;

  4.     public String getName() {
  5.         return "hello";
  6.     }

  7.     public void setName(String name) {
  8.         this.name = name;
  9.     }

  10. }
8. 最终实现main函数;

点击(此处)折叠或打开

  1. package com.test.Main;

  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;

  4. import com.test.Bean;

  5. public class Main {
  6.     public static void main(String []args){
  7.         ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
  8.         Bean tag = (Bean) ctx.getBean("oneBean");
  9.         System.out.println("oneBean age is " + tag.getName());
  10.     }
  11. }






阅读(3902) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~