博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring 自定义schema
阅读量:6533 次
发布时间:2019-06-24

本文共 3768 字,大约阅读时间需要 12 分钟。

hot3.png

扩展schema,定义自己的bean属性。。不错!

主要:

1,定义META-INF下.xsd文件,这里是people.xsd;定义spring.handlers;定义spring.schemas

2,定义namaspace解析类,这里是StudentNamespaceHandler

3,定义beanDefinition,这里是StudentBeanDefinitionParser

4,当然还有相关的javabean定义,这里是Student.java

详细代码:

people.xsd

姓名
年龄
spring.handlers;

http\://www.luyee.com/bat/schema/people=com.luyee.bat.spring.StudentNamespaceHandler
spring.schemas

http\://www.luyee.com/bat/schema/people.xsd=META-INF/people.xsd

StudentNamespaceHandler和StudentBeanDefinitionParser

package com.luyee.bat.spring;import java.text.SimpleDateFormat;import org.springframework.beans.factory.support.BeanDefinitionBuilder;import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;import org.springframework.beans.factory.xml.NamespaceHandlerSupport;import org.springframework.util.StringUtils;import org.w3c.dom.Element;public class StudentNamespaceHandler  extends NamespaceHandlerSupport {	public void init() {		registerBeanDefinitionParser("student", new StudentBeanDefinitionParser());  	}		class StudentBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{		 protected Class getBeanClass(Element element) {  		        return Student.class;  		    }  		  		    protected void doParse(Element element, BeanDefinitionBuilder bean) {  		        String name = element.getAttribute("name");  		        bean.addPropertyValue("name", name);  		        String age = element.getAttribute("age");  		        if (StringUtils.hasText(age)) {  		            bean.addPropertyValue("age", Integer.valueOf(age));  		        }  		    }  	}}
JavaBean:Student

package com.luyee.bat.spring;public class Student {		private String name;		private int age;	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}	public int getAge() {		return age;	}	public void setAge(int age) {		this.age = age;	}		}
测试:applicationContex.xml(people:student就好比bean)

StudentXsdTest.java

package com.luyee.bat.spring;  import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;    public class StudentXsdTest {        public static void main(String[] args) {          ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");          Student student1 = (Student) ctx.getBean("student1");          Student student2 = (Student) ctx.getBean("student2");          System.out.println("name: " +student1.getName()+" age :" + student1.getAge());          System.out.println("name: " +student2.getName()+" age :" + student2.getAge());      }  }

输出:

八月 13, 2013 8:50:50 下午 org.springframework.context.support.AbstractApplicationContext prepareRefreshINFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@46f7ba12: startup date [Tue Aug 13 20:50:50 CST 2013]; root of context hierarchy八月 13, 2013 8:50:50 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitionsINFO: Loading XML bean definitions from class path resource [applicationContext.xml]八月 13, 2013 8:50:52 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletonsINFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@25e9a396: defining beans [student1,student2]; root of factory hierarchyname: student1 age :18name: student2 age :20

最后贴个文件结构

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://my.oschina.net/91jason/blog/473524

你可能感兴趣的文章
内部类
查看>>
高速数论变换(NTT)
查看>>
Springmvc的跳转方式
查看>>
加密原理介绍,代码实现DES、AES、RSA、Base64、MD5
查看>>
LINUX中常用操作命令
查看>>
python 获取进程pid号
查看>>
链表中插入一个节点的三种情况
查看>>
洛谷.4180.[模板]次小生成树Tree(Kruskal LCA 倍增)
查看>>
TCL函数“参数自动补全” 与 “help 信息显示”
查看>>
POJ1050To the Max
查看>>
汇编基础--标识符、标号、伪指令和指令
查看>>
Linux软中断、tasklet和工作队列
查看>>
如何解决ORA-28002 the password will expire within 7 days问题(密码快过期)
查看>>
Asp.Net Core 轻松学-利用日志监视进行服务遥测
查看>>
LightSwitch社区资源搜集
查看>>
Android通讯录查询篇--ContactsContract.Data 二(续)
查看>>
IT人的自我导向型学习:开篇杂谈
查看>>
[原创]BizTalk动手实验系列目录
查看>>
HDU 4611Balls Rearrangement(思维)
查看>>
[LeetCode] Majority Element II
查看>>