扩展SpringBoot Schema配置Bean
Posted HiveDark
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扩展SpringBoot Schema配置Bean相关的知识,希望对你有一定的参考价值。
一、Spring Schema
Spring2.0开始,Spring提供XML Schema可扩展机制,用户可以自定义XML Schema文件,并自定义XML Bean解析器,集成到Spring IOC容器中。
二、创建步骤
- 2.1 创建一个xml schema文件,描述自定义的构建模型,即xsd文件。
- 2.2 自定义构建模型对应的java对象。
- 2.3 自定义命名空间解析器,继承NamespaceHandlerSupport。
- 2.4 自定义BeanDefinition解析器,继承AbstractSingleBeanDefinitionParser。
- 2.5 创建beans.xml文件,注册解析。
- 2.6 获取bean,测试使用。
2.0 项目目录
2.1 创建xsd,schema文件,People.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns="http://a.cn/schema/people"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://a.cn/schema/people"
elementFormDefault="qualified">
<xsd:import
namespace="http://www.springframework.org/schema/beans"
/>
<xsd:import
namespace="http://www.springframework.org/schema/tool" />
<xsd:element name="people">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension
base=<u>"beans:identifiedType"</u>>
<xsd:attribute name="name"
type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation><![CDATA[ 服务接口实现类]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="age"
type="xsd:string" use="required">
<xsd:annotation>
<xsd:documentation><![CDATA[ 服务接口实现类]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
2.2 自定义xsd对应的java对象,People.java
package com.david.test.test_springboot_schema.schema;
public class People
private String id;
private String name;
private Integer age;
public String getId()
return id;
public void setId(String id)
this.id = id;
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;
2.3 自定义命名空间解析器,MyNamespaceHandler
package com.david.test.test_springboot_schema.schema;
import
org.springframework.beans.factory.xml.NamespaceHandlerSupport;
public class MyNamespaceHandler extends
NamespaceHandlerSupport
@Override
public void init()
registerBeanDefinitionParser("people", new
PeopleBeanDefinitionParser());
2.4 自定义BeanDefinition解析器,PeopleBeanDefinitionParser
package com.david.test.test_springboot_schema.schema;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
public class PeopleBeanDefinitionParser extends AbstractSingleBeanDefinitionParser
protected Class getBeanClass(Element element)
return People.class;
protected void doParse(Element element, BeanDefinitionBuilder bean)
String name = element.getAttribute("name");
String age = element.getAttribute("age");
String id = element.getAttribute("id");
if (StringUtils.hasText(id))
bean.addPropertyValue("id", id);
if (StringUtils.hasText(name))
bean.addPropertyValue("name", name);
if (StringUtils.hasText(age))
bean.addPropertyValue("age", Integer.valueOf(age));
2.5 创建beans.xml、spring.handlers、spring.schemas文件
beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<<u>beans</u>
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:david="http://a.cn/schema/people"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://a.cn/schema/people
http://a.cn/schema/people.xsd">
<david:people id="testPeople" name="zhangsan"
age="30"/>
<david:people id="testPeople2" name="lisi" age="50"/>
<david:people id="testPeople3" name="wangwu"
age="60"/>
</beans>
spring.handlers
http\\://a.cn/schema/people=\\
com.david.test.test_springboot_schema.schema.MyNamespaceHandler
spring.schemas
http\\://a.cn/schema/people.xsd=META-INF/people.xsd
2.6 SpringBoot启动类里加载beans.xml
package com.david.test.test_springboot_schema;
import java.util.Map;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import com.david.test.test_springboot_schema.schema.GlobalApplicationContext;
import com.david.test.test_springboot_schema.schema.People;
@Configuration
@SpringBootApplication
@ImportResource(locations = "classpath:beans.xml")
public class MainBootApplication extends SpringBootServletInitializer
public static void main(String[] args)
SpringApplication.run(MainBootApplication.class, args);
//获取bean
Map<String,People> map = GlobalApplicationContext.applicationContext.getBeansOfType(People.class);
System.out.println(map);
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder)
// TODO Auto-generated method stub
return builder.sources(MainBootApplication.class);
GlobalApplicationContext.java
package com.david.test.test_springboot_schema.schema;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class GlobalApplicationContext implements ApplicationContextAware,InitializingBean
public static ApplicationContext applicationContext;
@Override
public void afterPropertiesSet() throws Exception
// TODO Auto-generated method stub
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
// TODO Auto-generated method stub
GlobalApplicationContext.applicationContext = applicationContext;
三、注意事项
beans.xml中引入的标签命名空间需与people下定义的一致即可,不需要单独部署xsd也可以使用,放在META-INF文件下。
以上是关于扩展SpringBoot Schema配置Bean的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot - Flyway - JPA 集成 - 创建名称为“flywayInitializer”的 bean 时出错 - information_schema 中的未知表“事件”
springboot 开发中将 bean 放入容器 你知道的有几种方式?