Spring 自定义标签的解析

1.自定义 parser类 用于标签的解析

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 email=element.getAttribute("email");

        if(StringUtils.hasText(userName)){
            builder.addPropertyValue("userName", userName);
        }
        if(StringUtils.hasText(email)){
            builder.addPropertyValue("email", email);
        }
    }

}


public class User {
    private String userName;
    private String email;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
 

2.自定义handler类 注册解析类

public class UserNamespaceHandler extends NamespaceHandlerSupport {

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

}
 

3.再resources下创建 META_INF 文件夹存放 spring.handlers   spring.schemas  spring-user.xsd 文件

 spring.handlers的内容

http\://www.wjs.com/schema/user=com.example.handler.UserNamespaceHandler

 

spring.schemas的内容

http\://www.wjs.com/schema/user/spring-user.xsd=META-INF/spring-user.xsd
 

spring-user.xsd的内容

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://www.wjs.com/schema/user" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:beans="http://www.springframework.org/schema/beans"
  targetNamespace="http://www.wjs.com/schema/user" elementFormDefault="qualified">
  <xsd:import namespace="http://www.springframework.org/schema/beans"/>
  <xsd:element name="user">
    <xsd:complexType>

      <xsd:attribute name="id" type="xsd:string"/>
      <xsd:attribute name="userName" type="xsd:string"/>
      <xsd:attribute name="email" type="xsd:string"/>


    </xsd:complexType>
  </xsd:element>

</xsd:schema>

4.springCustomTag.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:myTag="http://www.wjs.com/schema/user"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.wjs.com/schema/user http://www.wjs.com/schema/user/spring-user.xsd">
  
  <myTag:user id="testUser" userName="name" email="lx@163.com"/>
</beans>

5.运行

package com.example.controller;

import com.example.bean.domain.User;
import com.example.bean.factory.Animal;
import com.example.test.aop.service.OrderService;
import com.example.test.aop.service.UserService;
import com.example.test.tx.service.UserInfoService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author xiang.li
 * create date 2018/10/23
 * description
 */
public class SpringTest {
    public static void main(String[] args) {
     

        ApplicationContext beans=new ClassPathXmlApplicationContext("springCustomTag.xml");
        User user=(User) beans.getBean("testUser");
        System.out.println("username:"+user.getUserName()+":"+"email:"+user.getEmail());

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值