`
raymond.chen
  • 浏览: 1426284 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

自定义Spring命名空间

 
阅读更多

1、定义schema约束xsd文件

      将xsd文件放到 META-INF 目录下

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:beans="http://www.springframework.org/schema/beans"
            xmlns:tool="http://www.springframework.org/schema/tool"
            xmlns="http://www.seasy.com/schema/seasy"
            targetNamespace="http://www.seasy.com/schema/seasy">
            
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
    <xsd:import namespace="http://www.springframework.org/schema/beans"/>
    <xsd:import namespace="http://www.springframework.org/schema/tool"/>
    
	<xsd:annotation>
        <xsd:documentation>
            <![CDATA[ Namespace support for the seasy framework ]]>
        </xsd:documentation>
    </xsd:annotation>
    
    <!-- type -->
    <xsd:complexType name="serviceType">
        <xsd:attribute name="id" type="xsd:ID">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The unique identifier for a bean. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="name" type="xsd:string" use="required">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The service name. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
        <xsd:attribute name="description" type="xsd:string" use="optional" default="">
            <xsd:annotation>
                <xsd:documentation><![CDATA[ The service description. ]]></xsd:documentation>
            </xsd:annotation>
        </xsd:attribute>
    </xsd:complexType>
    
    <!-- element -->
    <xsd:element name="service" type="serviceType">
        <xsd:annotation>
            <xsd:documentation><![CDATA[ The service config ]]></xsd:documentation>
        </xsd:annotation>
    </xsd:element>
    
</xsd:schema>

 

 

2、创建自定义标签对应的Config类:

public class ServiceConfig {
	private String name;
	private String description;
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
}

 

 

3、创建自定义标签的解析器类:

public class ServiceBeanDefinitionParser extends AbstractSingleBeanDefinitionParser{
	@Override
	protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
		String name = element.getAttribute("name");
		String description = element.getAttribute("description");
		
		builder.addPropertyValue("name", name);
		builder.addPropertyValue("description", description);
	}
	
	@Override
	protected Class<?> getBeanClass(Element element) {
		return ServiceConfig.class;
	}
}

 

 

4、创建自定义NamespaceHandler类:

public class SeasyNamespaceHandler extends NamespaceHandlerSupport{
	@Override
	public void init() {
		//注册bean配置信息的解析器
		//service 对应到beans xml文件的节点元素名
		//<seasy:service> </seasy:service>
		registerBeanDefinitionParser("service", new ServiceBeanDefinitionParser());
	}
}

 

 

5、在 META-INF 目录下新建文件 spring.schemas

http\://www.seasy.com/schema/seasy.xsd=META-INF/seasy.xsd

 

 

6、在 META-INF 目录下新建文件 spring.handlers

http\://www.seasy.com/schema/seasy=com.seasy.namespace.SeasyNamespaceHandler

 

7、创建beans的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:seasy="http://www.seasy.com/schema/seasy"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.seasy.com/schema/seasy http://www.seasy.com/schema/seasy.xsd">

	<seasy:service id="userService" name="UserService" description="" />
</beans>

 

8、测试类

public class NamespaceHandlerTest {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext
			= new ClassPathXmlApplicationContext(new String[]{"seasy-beans.xml"}, false);
		applicationContext.setValidating(true);
		applicationContext.refresh();
		
		ServiceConfig serviceConfig = applicationContext.getBean(ServiceConfig.class);
		System.out.println(serviceConfig.getName());
	}
}

 

 自定义标签有点复杂,通过以下方案可以实现同样的功能:

       1、创建自定义注解类,对应到自定义标签的Config类

       2、创建BeanFactoryPostProcessor的子类,在Bean工厂初始化之前根据自定义注解类扫描类

       3、创建ClassPathBeanDefinitionScanner的子类,用于根据自定义注解类扫描类,并注册到容器中

 

分享到:
评论

相关推荐

    SpringSecurity笔记2-SpringSecurity命名空间

    在"SpringSecurity笔记2-SpringSecurity命名空间"的学习中,还会涉及到如何自定义过滤器链,以及如何通过`&lt;custom-filter&gt;`元素插入自定义的SpringSecurity过滤器。同时,理解`&lt;access-denied-handler&gt;`和`...

    自定义 Schema 解析 Spring Bean

    自定义Schema解析是Spring框架提供的一个强大特性,它允许开发者创建自己的XML命名空间,从而定义自己的配置元素和属性。这在大型项目中特别有用,因为可以创建特定于项目的配置约定,提高代码的可读性和可维护性。 ...

    spring security 2.0 命名空间配置(带例子)

    在这个主题中,我们将深入探讨Spring Security 2.0的命名空间配置,并通过实例来理解其工作原理。 在Spring Security 2.0中,命名空间配置是简化安全配置的一种方式。它允许开发者使用XML配置文件中的特定命名空间...

    spring 自定义xml标签

    总之,自定义Spring XML标签是一项强大的功能,它允许开发者按照项目特性定制配置语法,提高代码的可读性和可维护性。通过实现`NamespaceHandler`和`BeanDefinitionParser`,我们可以轻松地将业务逻辑融入到Spring的...

    spring-扩展点-namespacehandler(Spring自定义标签)

    在Spring框架中,扩展点是允许用户自定义行为的关键组件,而`NamespaceHandler`就是其中的一个重要扩展点,主要用于处理自定义的XML命名空间。当我们需要在Spring配置文件中引入自定义标签时,`NamespaceHandler`起...

    spring-boot-starter-hbase自定义的spring-boot的hbasestarter

    在使用自定义的`spring-boot-starter-hbase`时,我们通常会创建一个配置类,声明HBase的相关属性,例如Zookeeper的地址、HBase的命名空间等。这些配置会通过Spring的环境变量和属性源加载到Spring Boot的应用上下...

    Spring 5.2.9 06 源码分析-spring自定义标签解析过程

    这个接口定义了两个主要方法:`init()`用于初始化命名空间处理器,以及`parseElement(Element element, ParserContext parserContext)`用于解析XML元素。 当Spring容器遇到一个自定义标签时,它会查找与该标签相关...

    06 源码分析-spring自定义标签解析过程

    当遇到一个新的命名空间时,Spring会查找对应的`NamespaceHandler`,该处理器负责为该命名空间下的每个自定义标签注册一个`BeanDefinitionParser`。 2. **解析标签**:在解析XML文件时,Spring的`DocumentReader`类...

    spring 自定义xsd

    这可以通过`xsi:schemaLocation`属性来实现,将你的XSD文件与命名空间关联起来: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ...

    这一次搞懂Spring自定义标签以及注解解析原理说明

    当遇到非默认命名空间的元素时,Spring会查找相应的`NamespaceHandler`来处理这个自定义标签。`NamespaceHandler`是Spring解析自定义标签的核心接口,它定义了如何将XML元素转换为`BeanDefinition`对象。例如,`...

    这一次搞懂Spring自定义标签以及注解解析原理说明.docx

    为了使Spring知道如何找到对应的`NamespaceHandler`,还需要在`classpath/META-INF/spring.handlers`文件中定义命名空间和`NamespaceHandler`的映射。 其次,我们来看看注解解析的原理。当我们在类上使用如@Service...

    spring自定义标签

    在Spring中,schema是XML配置文件的命名空间,它定义了可以使用的标签和属性。要添加自定义标签,我们需要创建一个schema文件,比如`spring-ext.xsd`。在这个文件中,我们可以定义自己的标签和属性,例如: ```xml ...

    Spring源码解密之自定义标签与解析

    最后,我们需要在Spring的配置中注册自定义的命名空间和处理器: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:...

    扩展Spring schema样例代码 maven

    命名空间是XML文件中识别自定义标签的关键,而解析器和处理器则负责将XML配置转化为Spring容器可以理解的Bean定义。 接下来,我们讨论如何在Maven项目中集成这些扩展。Maven是一个强大的项目管理和依赖管理工具,它...

    自定义xml标签

    接下来,我们需要在Spring的`schemaLocation`中声明这个自定义标签的命名空间和解析器。在XML配置文件的头部添加以下内容: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    springsecurity中文

    我们推荐你尝试一下 SpringSource 工具套件,因为它具有处理 Spring 组合命名空间的特殊功能。 要开始在你的应用环境里使用 security 命名空间,你所需要的就是把架构声明添加到你的应用环境文件里: ...

    spring-framework-2.5.1类包

    XML Schema的支持和自定义命名空间的使用大大减少了基于XML的配置。使用Java5及更新版本java的开发人员如今可以利用植入了像泛型(generic)和注解等新语言特性的Spring库。最近,和AspectJ表达式语言的紧密集成,...

    3.springSecurity源码分析1

    3. spring-security.xml配置:在这个文件中,我们通常会看到`&lt;http&gt;`标签的使用,它是SpringSecurity自定义的命名空间,允许我们声明各种安全策略。这些配置会被SpringSecurity的`SecurityNamespaceHandler`处理,它...

    Spring Cloud Nacos示例

    Nacos 提供了动态配置服务、服务发现、命名空间、健康检查、元数据管理等一系列功能,帮助开发者更便捷地构建云原生应用。 **1. 动态配置服务** Nacos 的核心功能之一是动态配置服务,它允许开发者在不重启服务的...

Global site tag (gtag.js) - Google Analytics