20220406_spring自定义schema学习笔记
1概述
Spring提供了可扩展Schema的支持,灵活性较强,完成一个自定义的Bean配置注册。自定义schema步骤如下:
- 编写JavaBean
- 编写XSD文件
- 编写BeanDefinitionParser完成解析工作
- 编写NamespaceHandler
- 配置spring.handlers
- 配置spring.schemas串联起xsd部件
- 编写应用配置文件中注册Bean
2代码示例
2.1MyPeople
package com.kikop.myspringschema.model;
public class MyPeople {
private String name;
private int age;
private String pk;
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;
}
public String getPk() {
return pk;
}
public void setPk(String pk) {
this.pk = pk;
}
}
2.2编写xsd文件
http://www.kikop.com/schema/mypeople
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema
xmlns="http://www.kikop.com/schema/mypeople" <!-- 步骤1-->
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.kikop.com/schema/mypeople" <!-- 步骤2-->
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<!-- 步骤3-->
<!--1.定义elementName,要与 namespaceHandler保持一致-->
<xsd:element name="mypeople">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<!--复合元素之上以某个复合元素为基础,然后添加一些元素,-->
<!--具体的解释看:http://www.w3school.com.cn/schema/schema_complex.asp;-->
<xsd:attribute name="name" type="xsd:string">
<xsd:annotation>
<xsd:documentation>姓名</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="pk" type="xsd:string"/>
<xsd:attribute name="age" type="xsd:int"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
// 步骤4,设置放的位置
C010myspringdemo\src\main\resources\META-INF\mypeople.xsd
2.3编写BeanDefinitionParser完成解析工作
package com.kikop.myspringschema.xml;
import com.kikop.myspringschema.model.MyPeople;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
public class MyPeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
@Override
protected Class<?> getBeanClass(Element element) {
return MyPeople.class;
}
@Override
protected void doParse(Element element, BeanDefinitionBuilder beanDefinitionBuilder) {
// super.doParse(element, builder);
String name = element.getAttribute("name");
String age = element.getAttribute("age");
String pk = element.getAttribute("pk");
if (StringUtils.hasText(pk)) {
beanDefinitionBuilder.addPropertyValue("pk", pk);
}
if (StringUtils.hasText(name)) {
beanDefinitionBuilder.addPropertyValue("name", name);
}
if (StringUtils.hasText(age)) {
beanDefinitionBuilder.addPropertyValue("age", Integer.valueOf(age));
}
}
}
2.4编写NamespaceHandler
package com.kikop.myspringschema.xml;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyPeopleNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("mypeople", new MyPeopleBeanDefinitionParser());
}
}
2.5配置spring.handlers
# 2.定义命名空间处理器
http\://www.kikop.com/schema/mypeople=com.kikop.myspringschema.xml.MyPeopleNamespaceHandler
2.6.配置spring.schemas串联起xsd部件
# 3.schema定义xsd文件的位置
http\://www.kikop.com/schema/mypeople.xsd=META-INF/mypeople.xsd
2.7编写应用配置文件中注册Bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:myxsi="http://www.kikop.com/schema/mypeople"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.kikop.com/schema/mypeople http://www.kikop.com/schema/mypeople.xsd">
<!--注册Bean-->
<myxsi:mypeople id="myPeople" name="kikop" age="30" pk="33"/>
</beans>
2.8测试
package com.kikop.myspringschema;
import com.kikop.myspringschema.model.MyPeople;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MySpringSchemaDemo {
public static void main(String[] args) {
getMyPeopleInfo();
}
private static void getMyPeopleInfo() {
// new ClassPathXmlApplicationContext("myxmlbeanlifecycle/ApplicationContext.xml");
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"myspringschema/spring.xml");
String beanName = "myPeople";
MyPeople myPeople = (MyPeople) ctx.getBean(beanName);
System.out.println(myPeople.getPk());
System.out.println(myPeople.getName());
System.out.println(myPeople.getAge());
}
}
网友评论