Spring自定义标签

为什么需要自定义标签?

  • 提供可配置化支持
    • 比如事务注解,配置即开启事务
<tx:annotation-driven transaction-manager="transactionManager" />

如何自定义标签

1.创建一个需要拓展的组件

@Data
public class User {
    /**
     * 相当于BeanName
     */
    private String id;
    private String username;
    private String address;
    private int age;
}

2.定义一个XSD文件描述组件内容

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://www.zhixing.com/schema/user"
        xmlns:tns="http://www.zhixing.com/schema/user"
        elementFormDefault="qualified">

    <element name="user">
        <complexType>
            <attribute name="id" type="string"/>
            <attribute name="username" type="string"/>
            <attribute name="address" type="string"/>
            <attribute name="age" type="int"/>
        </complexType>
    </element>

</schema>
  • id属性相当于BeanName,组件中可以不定义,但是XSD文件中需要定义

3.实现BeanDefinitionParser,解析XSD文件和组件定义

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

    @Override
    protected void doParse(Element element, BeanDefinitionBuilder builder) {
        String username = element.getAttribute("username");
        String address = element.getAttribute("address");
        int age = Integer.parseInt(element.getAttribute("age"));
        builder.addPropertyValue("username", username);
        builder.addPropertyValue("address", address);
        builder.addPropertyValue("age", age);
    }
}

4.创建Handler文件,注册组件到容器

public class MyNameSpaceHandler extends NamespaceHandlerSupport {
    @Override
    public void init() {
        registerBeanDefinitionParser("user", new UserBeanDefinitionParser());
    }
}

5.编写spring.handlers和spring.schemas

  • spring.handlers
http\://www.zhixing.com/schema/user=com.zhixing.parser.MyNameSpaceHandler
  • spring.schemas
http\://www.zhixing.com/schema/user.xsd=META-INF/user.xsd

6.测试自定义标签

  • 配置文件
<?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:myname="http://www.zhixing.com/schema/user"

       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.zhixing.com/schema/user
       http://www.zhixing.com/schema/user.xsd">

    <myname:user id="myuser" username="zhangsan" address="nanjing" age="18"/>

</beans>
  • 测试代码
public class MyParserTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        User myUser = (User) applicationContext.getBean("myuser");
        System.out.println(myUser);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值