spring-namespace实现自己的自定义标签类

spring-namespace实现自己的自定义标签类
强烈推介IDEA2020.2破解激活,IntelliJ IDEA 注册码,2020.2 IDEA 激活码

这篇文章介绍的是如何通过spring namespace的方式进行bean的配置

最终要达到的目的如下:

<?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:user="http://www.wuxueyou.cn/schema/user"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.wuxueyou.cn/schema/user http://www.wuxueyou.cn/schema/user.xsd">

    <user:self-user id="user2" userId="12" name="aaa"/>
    <bean id="user1" class="com.xueyou.User">
        <property name="userId" value="33"/>
        <property name="name" value="xiaoming"/>
    </bean>
</beans>

好,下面上货。

1、配置java bean

2、编写xsd文件

3、编写BeanDefinationParse标签解析类

4、编写调用标签解析类的NamespaceHandler类

5、编写spring.handlers和spring.schemas供spring读取

6、在spring中使用

目录结构

1、配置java Bean

package com.xueyou;

public class User {
    private int userId;
    private String name;

    public User() {
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}

2、编写xsd文件 

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.wuxueyou.cn/schema/user"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:beans="http://www.springframework.org/schema/beans"
            targetNamespace="http://www.wuxueyou.cn/schema/user"
            elementFormDefault="qualified">
    <xsd:import namespace="http://www.springframework.org/schema/beans"
                schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd"/>
    <xsd:element name="self-user">
        <xsd:complexType>
            <!--这里的最外层的id是spring用的,用来定义bean的名称用的,不要和类中的id混淆了-->
            <xsd:attribute name="id" type="xsd:string" use="required"/>
            <xsd:attribute name="userId" type="xsd:int" use="required"/>
            <xsd:attribute name="name" type="xsd:string" use="required"/>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

3、编写BeanDefinationParse标签解析类

package com.xueyou.parser;

import com.xueyou.User;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSimpleBeanDefinitionParser;
import org.w3c.dom.Element;

public class UserDefinationParser extends AbstractSimpleBeanDefinitionParser {
    @Override
    protected Class<?> getBeanClass(Element element) {
        return User.class;
    }

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        int userId = Integer.valueOf(element.getAttribute("userId"), 0);
        String name = element.getAttribute("name");
        builder.addPropertyValue("userId", userId);
        builder.addPropertyValue("name", name);
    }
}

4、编写调用标签解析类的NamespaceHandler类

package com.xueyou.handler;

import com.xueyou.parser.UserDefinationParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;

public class UserNamespaceHandler extends NamespaceHandlerSupport {
    public void init() {
        registerBeanDefinitionParser("self-user", new UserDefinationParser());
    }
}

5、编写spring.handlers和spring.schemas以供spring读取

spring.handlers

http\://www.wuxueyou.cn/schema/user=com.xueyou.handler.UserNamespaceHandler

spring.schemas

http\://www.wuxueyou.cn/schema/user.xsd=namespace/user.xsd

 6、打包

首先把刚才的打成一个jar包,需要注意在maven的plugin中添加如下内容, 这个shade插件能够合并指定的内容,比如spring.schema等等

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass></mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

7、在其他项目中使用

在一个web项目中使用这个类

<?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:user="http://www.wuxueyou.cn/schema/user"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.wuxueyou.cn/schema/user http://www.wuxueyou.cn/schema/user.xsd">

    <user:self-user id="user2" userId="12" name="aaa"/>
    <bean id="user1" class="com.xueyou.User">
        <property name="userId" value="33"/>
        <property name="name" value="xiaoming"/>
    </bean>
</beans>

在controller中进行测试

package com.example.demo.controller;

import com.xueyou.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/namespacetest")
public class NamespaceController {

    @Resource(name = "user2")
    private User user;

    @RequestMapping("/user")
    public User namespacetest() {
        return user;
    }
}

 运行结果:

运行结果

最终,我们可以使用spring-namespace对bean进行配置了。这样比<bean>标签要好的多。

本文来源0day__,由javajgs_com转载发布,观点不代表Java架构师必看的立场,转载请标明来源出处:https://javajgs.com/archives/8094

发表评论