Spring 自定义 Schema命名空间 定义bean信息

Spring 自定义 Schema命名空间 定义bean信息

1. 准备工作

1). 配置文件

  1. xsd约束文件

  2. spring.schemas 文件

  3. spring.handlers 文件

2).组件类

  1. `BeanDefinitionParser解析类
  2. `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);
    }

}

测试结果

image-20221012142726004

#Java#
全部评论
有干货,又举例,诚意满满
点赞 回复 分享
发布于 2022-10-12 17:48 广东

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
2024-11-09 01:22
已编辑
东南大学 Java
高级特工穿山甲:羡慕,我秋招有家企业在茶馆组织线下面试,约我过去“喝茶详谈”😢结果我去了发现原来是人家喝茶我看着
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务