Spring 自定义 Schema命名空间 定义bean信息
Spring 自定义 Schema命名空间 定义bean信息
1. 准备工作
1). 配置文件
xsd约束文件
spring.schemas 文件
spring.handlers 文件
2).组件类
- `BeanDefinitionParser解析类
- `NamespaceHandler 命名空间处理类
2. 代码演示
1).导入依赖
新建工程,导入spring的核心依赖
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.7.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.7.RELEASE</version> </dependency>
2)application.xml 核心配置文件
application.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:sz="http://www.sz.com/schema/language" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.sz.com/schema/language http://www.sz.com/schema/language.xsd "> <sz:language id="java" name="java" desc="Java天下第一"/> </beans>
3) xsd约束文件
language.xsd
<?xml version="1.0" encoding="UTF-8"?> <xsd:schema xmlns="http://www.sz.com/schema/language" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:beans="http://www.springframework.org/schema/beans" targetNamespace="http://www.sz.com/schema/language" elementFormDefault="qualified" attributeFormDefault="unqualified"> <xsd:import namespace="http://www.springframework.org/schema/beans" /> <xsd:element name="language"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="beans:identifiedType"> <xsd:attribute name="name" type="xsd:string" /> <xsd:attribute name="desc" type="xsd:string" /> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> </xsd:schema>
4) spring.schemas文件
spring.schemas
http\://www.sz.com/schema/language.xsd=META-INF/language.xsd
5) spring.handlers 文件
spring.handlers 指定使用的命令空间处理器
http\://www.sz.com/schema/language=com.sz.schema.LanguageNamespaceHandler
6) 命名空间处理类 LanguageNamespaceHandler
public class LanguageNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { registerBeanDefinitionParser("language",new LanguageBeanDifinitionParse()); } }
7) bean定义信息解析器
public class LanguageBeanDifinitionParse extends AbstractSingleBeanDefinitionParser { @Override protected Class<?> getBeanClass(Element element) { return ProgLanguage.class; } @Override protected void doParse(Element element, BeanDefinitionBuilder builder) { String name = element.getAttribute("name"); String desc = element.getAttribute("desc"); if (StringUtils.hasText(name)){ builder.addPropertyValue("name",name); } if (StringUtils.hasText(desc)){ builder.addPropertyValue("desc",desc); } } }
8) 实体类 ProgLanguage
public class ProgLanguage { private String name; private String desc; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Override public String toString() { return "ProgLanguage{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}'; } }
9) 测试类
public class App { public static void main(String[] args) { ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("classpath:application.xml"); ProgLanguage java = (ProgLanguage) app.getBean("java"); System.out.println("java = " + java); } }