Spring如何自定义XML配置扩展

网友投稿 282 2023-02-15

Spring如何自定义XML配置扩展

在Spring中,我们定义一个自己的标签有如下步骤:

自己定义一个XSD文件。

定义一个和XSD文件所对应的实体类。

创建实现了BeanDefinitionParser的类(其实更好的做法是继承抽象类AbstractBeanDefinitionParser),去解析我们的自定义标签。

创建一个继承了NamespaceHandlerSupport的类,去将我们创建的类注册到spring容器。

编写自己的Spring.handlers和Spring.schemas

一、定义一个XSD文件

首先我们在resources下创建META-INF目录。

创建resources/META-INF/model.xsd

xmlns:xsd="http://w3.org/2001/XMLSchema"

xmlns="http://demo1.example.com/schema1"

targetNamespace="http://demo1.example.com/schema1">

xmlns:xsd="http://w3.org/2001/XMLSchema"

xmlns="http://demo1.example.com/schema1"

targetNamespace="http://demo1.example.com/schema1">

首先看到xsd:element这块,这里面的属性name就是我们以后标签的名字,type则指向了上面的标签xsd:complexType这里,这个标签里面有两个子标签都是xsd:attribute,一个代表string类型的name,另一个代表int类型的age,意思就是bill这个标签里面有name和age两个属性。

再就是要注意最上面的几行,第二行的xmlns:xsd="http:hHKWIc//w3.org/2001/XMLSchema"这个是必须的,第三行xmlns="http://demo1.example.com/schema"里面这个url你随便写,但是要和第四行的targetNamespace保持一致。

二、定义一个和XSD文件所对应的实体类

public class ModelBean {

private String name;

private Integer age;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

}

三、实现BeanDefinitionParser,解析标签

public class BillBeanDefinitionParser implements BeanDefinitionParser {

private final Class> beanClass;

public BillBeanDefinitionParser(Class> beanClass) {

this.beanClass = beanClass;

}

@Override

public BeanDefinition parse(Element element, ParserContext parserContext) {

GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();

genericBeanDefinition.setBeanClass(beanClass);

genericBeanDefinition.setLazyInit(false);

genericBeanDefinition.getPropertyValues().add("name", element.getAttribute("name"));

genericBeanDefinition.getPropertyValues().add("age", element.getAttribute("age"));

parserContext.getRegistry().registerBeanDefinition(beanClass.getName(),genericBeanDefinition);

return null;

}

}

四、继承NamespaceHandlerSupport,注册类

public class BillNameSpaceHandler extends NamespaceHandlerSupport {

@Override

public void init() {

registerBeanDefinitionParser("bill",new BillBeanDefinitionParser(Model.class));

}

}

五、编写自己的Spring.handlers和Spring.schemas

META-INF/Spring.Handlers

http\://demo1.example.com/schema1=com.appst.xmlpc.handler.BillNameSpaceHandler

META-INF/Spring.schemas:

http\://demo1.example.com/schema1/model.xsd=META-INF/model.xsd

这两个文件都是properties格式的文件,这两个文件和开头的那个xsd都要放在resource目录下的META-INF文件夹下,再注意Spring.Handlers中的key是要和上面xsd中你自己定义的xmlns一致,value一定要指向你自己定义的NameSpaceHandler的全路径,Spring.schemas中key前半部分是自己定义的xmlns,后半部分的mytag.xsd就是你自己xsd的文件名。

然后在application-context.xml加上我们的标签:

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:billtag="http://demo1.example.com/schema1"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframeworhttp://k.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://demo1.example.com/schema1 http://demo1.example.com/schema1/model.xsd">

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:billtag="http://demo1.example.com/schema1"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframeworhttp://k.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd

http://demo1.example.com/schema1 http://demo1.example.com/schema1/model.xsd">

然后跑个测试看看:

//指定在单元测试启动的时候创建spring的工厂类对象

@ContextConfiguration(locations = {"classpath:applicationContext.xml"})

//RunWith的value属性指定以spring test的SpringJUnit4ClassRunner作为启动类

//如果不指定启动类,默认启用的junit中的默认启动类

@RunWith(value = SpringJUnit4ClassRunner.class)

public class SpringTest {

@Autowired

private ApplicationContext applicationContext;

@Test

public void testSpring() {

ModelBean model = (ModelBean) applicationContext.getBean(ModelBean.class.getName());

System.out.println(model.getAge());

System.out.println(model.getName());

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Sharding JDBC读写分离实现原理及实例
下一篇:windows下java
相关文章

 发表评论

暂时没有评论,来抢沙发吧~