自定义schema规范约束
- 新建test-schema.xsd文件定义如下内容
<xsd:schema xmlns="http://www.myhost.com/schema/demo" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://www.myhost.com/schema/demo"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:element name="demo" type="Tdemo" />
<xsd:complexType name="Tdemo">
<xsd:attribute name="id" type="xsd:string"/>
<xsd:attribute name="interFace" type="xsd:string"/>
<xsd:attribute name="version" type="xsd:string"/>
</xsd:complexType>
</xsd:schema>
- 将文件放在META-INF目录下
- 在META-INF目录下新建spring.schemas文件,内容如下:
http\://www.myhost.com/schema/demo/test-schema.xsd=META-INF/test-schema.xsd
自定义shema实现
- 继承NamespaceHandlerSupport类
public class SpringParseNameSpaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("demo" ,new DemoDefiniParse());
}
}
在META-INF目录下新建spring.handlers文件,内容如下:
http\://www.myhost.com/schema/demo=com.test.SpringParseNameSpaceHandler
- 实现BeanDefinitionParser接口
//AbstractSingleBeanDefinitionParser继承于AbstractBeanDefinitionParser,而AbstractBeanDefinitionParser已实现BeanDefinitionParser接口
public class DemoDefiniParse extends AbstractSingleBeanDefinitionParser {
@Override
protected void doParse(Element element, BeanDefinitionBuilder builder) {
String id = element.getAttribute("id");
String interFace = element.getAttribute("interFace");
String version = element.getAttribute("version");
builder.addPropertyValue("id",id);
builder.addPropertyValue("interFace",interFace);
builder.addPropertyValue("version",version);
}
@Override
protected Class<?> getBeanClass(Element element) {
return Demo.class;
}
}
- 定义bean类Demo
public class Demo {
private String id;
private String interFace;
private String version;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getInterFace() {
return interFace;
}
public void setInterFace(String interFace) {
this.interFace = interFace;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
- 新建xml配置文件spring-demo.xml
xmlns:pta="http://www.myhost.com/schema/demo" :
xmlns:固定开头
pta:自定义前缀,一般都有含义,如dubbo框架xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 注册服务<dubbo:service>,dubbo由此处定义
http://www.myhost.com/schema/demo : 取值于test-schema.xsd中的targetNamespace
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.myhost.com/schema/demo http://www.myhost.com/schema/demo/test-schema.xsd"
http://www.myhost.com/schema/demo:如上
http://www.myhost.com/schema/demo/test-schema.xsd:约束
<?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:pta="http://www.myhost.com/schema/demo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.myhost.com/schema/demo http://www.myhost.com/schema/demo/test-schema.xsd"
default-autowire="byName">
<pta:demo id="test" interFace="org.springframework.jdbc.core.JdbcTemplate" version="1.0"></pta:demo>
</beans>
- 本地测试
public class SpringParseDemo {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-demo.xml");
applicationContext.getApplicationName();
Object sit = applicationContext.getBean("test");
System.out.println(sit);
}
}