完成一个Spring的自定义配置一般需要5步:
1.设计配置属性和JavaBean
2.编写XSD(XML Schema)文件,它就是一个校验XML,定义了一些列的语法来规范XML
3.编写NameSpaceHandler和BeanDefinitionParser完成解析工作
4.编写spring。handlers和spring.schemas串联起所有部件
5.在Bean文件中应用
1.配置JavaBean和设计配置属性
import lombok.Data;
@Data
public class People {
private String id;
private String name;
private Integer age;
}
MyName.xml
<?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:test="http://code.test.com/schema/people"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://code.test.com/schema/people http://code.test.com/schema/people.xsd
">
<test:people id="people" name="testName" age="100"/>
</beans>
2.编写XSD文件
resources/META-INF/people.xsd
<xsd:schema
xmlns="http://code.test.com/schema/people"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://code.test.com/schema/people"
elementFormDefault="qualified"
>
<xsd:import namespace="http://www.springframework.org/schema/beans"/>
<xsd:element name="people">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="beans:identifiedType">
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="age" type="xsd:int"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
3,编写NamespaceHandler和BeanDefinitionParser完成解析工作
import org.springframework.beans.factory.xml.NamespaceHandlerSupport; public class MyNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { registerBeanDefinitionParser("people",new PeopleBeanDefinitionParser()); } }
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 PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { @Override protected Class getBeanClass(Element element){ return People.class; } @Override protected void doParse(Element element, BeanDefinitionBuilder bean){ String name = element.getAttribute("name"); String age = element.getAttribute("age"); String id = element.getAttribute("id"); if (StringUtils.hasText(name)) { bean.addPropertyValue("name",name); } if (StringUtils.hasText(age)) { bean.addPropertyValue("age",Integer.valueOf(age)); } if (StringUtils.hasText(id)) { bean.addPropertyValue("id",id); } } }
4.编写spring.handlers和spring.schemas串联起所有部件
resources/META-INF/spring.handlers
http://code.test.com/schema/people=com.example.schema.MyNamespaceHandler
resources/META-INF/spring.schemas
http://code.test.com/schema/people.xsd=META-INF/people.xsd
5.在Bean文件中应用
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SchemaMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/xml/MyName.xml");
People people =(People) context.getBean("people");
System.out.println(people.getName());
}
}
Dubbo中spring的配置
1.dubbo-provider.xml
<?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:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:service interface="cn.example.InterfaceService" ref="service"/>
</beans>
2,进入xsd中
<xsd:element name="service" type="serviceType"> <xsd:annotation> <xsd:documentation><![CDATA[ Export service config ]]></xsd:documentation> </xsd:annotation> </xsd:element> <xsd:complexType name="serviceType"> <xsd:complexContent> <xsd:extension base="abstractServiceType"> <xsd:choice minOccurs="0" maxOccurs="unbounded"> <xsd:element ref="method" minOccurs="0" maxOccurs="unbounded" /> <xsd:element ref="parameter" minOccurs="0" maxOccurs="unbounded" /> <xsd:element ref="beans:property" minOccurs="0" maxOccurs="unbounded" /> </xsd:choice> <xsd:attribute name="interface" type="xsd:token" use="required"> <xsd:annotation> <xsd:documentation><![CDATA[ Defines the interface to advertise for this service in the service registry. ]]></xsd:documentation> <xsd:appinfo> <tool:annotation> <tool:expected-type type="java.lang.Class"/> </tool:annotation> </xsd:appinfo> </xsd:annotation> </xsd:attribute> <xsd:attribute name="ref" type="xsd:string" use="optional"> <xsd:annotation> <xsd:documentation><![CDATA[ The service implementation instance bean id. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="class" type="xsd:string" use="optional"> <xsd:annotation> <xsd:documentation><![CDATA[ The service implementation class name. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="path" type="xsd:string" use="optional"> <xsd:annotation> <xsd:documentation><![CDATA[ The service path. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:attribute name="provider" type="xsd:string" use="optional"> <xsd:annotation> <xsd:documentation><![CDATA[ Deprecated. Replace to protocol. ]]></xsd:documentation> </xsd:annotation> </xsd:attribute> <xsd:anyAttribute namespace="##other" processContents="lax" /> </xsd:extension> </xsd:complexContent> </xsd:complexType>
3.配置handlers和schemas
http://code.alibabatech.com/schema/dubbo/dubbo.xsd=META-INF/dubbo.xsd
http://code.alibabatech.com/schema/dubbo=com.alibaba.dubbo.config.spring.schema.DubboNamespaceHandler
4.DubboNamespaceHandler
public class DubboNamespaceHandler extends NamespaceHandlerSupport {
static {
Version.checkDuplicate(DubboNamespaceHandler.class);
}
public void init() {
registerBeanDefinitionParser("application", new DubboBeanDefinitionParser(ApplicationConfig.class, true));
registerBeanDefinitionParser("module", new DubboBeanDefinitionParser(ModuleConfig.class, true));
registerBeanDefinitionParser("registry", new DubboBeanDefinitionParser(RegistryConfig.class, true));
registerBeanDefinitionParser("monitor", new DubboBeanDefinitionParser(MonitorConfig.class, true));
registerBeanDefinitionParser("provider", new DubboBeanDefinitionParser(ProviderConfig.class, true));
registerBeanDefinitionParser("consumer", new DubboBeanDefinitionParser(ConsumerConfig.class, true));
registerBeanDefinitionParser("protocol", new DubboBeanDefinitionParser(ProtocolConfig.class, true));
registerBeanDefinitionParser("service", new DubboBeanDefinitionParser(ServiceBean.class, true));
registerBeanDefinitionParser("reference", new DubboBeanDefinitionParser(ReferenceBean.class, false));
registerBeanDefinitionParser("annotation", new DubboBeanDefinitionParser(AnnotationBean.class, true));
}
}