# 背景介绍
Spring现在几乎已经成为了Java开发的必备框架,在享受Spring框架本身强大能力的同时,有时我们也会希望自己研发的组件和Spring进行整合,从而使得组件更易于上手,而且配合Spring使用能发挥更强大的作用。
Apollo配置中心的Java客户端在前一段时间也提供了和Spring整合的功能,Apollo既支持传统的基于XML的配置,也支持目前比较流行的基于Java的配置。下面就以Apollo为例,简单介绍一下扩展Spring的几种方式。
# 基于XML配置的扩展
相信从事Java开发有一些年头的人一定会对Spring的xml配置方式非常熟悉。不管是bean的定义,还是Spring自身的配置,早期都是通过xml配置完成的。相信还是有一大批遗留项目目前还是基于xml配置的,所以支持xml的配置方式是一个必选项。
1、定义schema
要支持XML的配置方式,首先需要定义一套XML Schema来描述组件所提供的功能。
Apollo提供了向Spring Property Sources注入配置的功能,所以schema中就需要描述我们期望用户提供的namespace以及namespace之间的排序等元数据。
下面就是Apollo的schema示例,可以看到xml的配置节点名字是config,并且有两个可选属性:namespaces和order,类型分别是string和int。
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xsd:schema xmlns="http://www.ctrip.com/schema/apollo"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ctrip.com/schema/apollo"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xsd:annotation>
<xsd:documentation><![CDATA[ Namespace support for Ctrip Apollo Configuration Center. ]]></xsd:documentation>
</xsd:annotation>
<xsd:element name="config">
<xsd:annotation>
<xsd:documentation>
<![CDATA[ Apollo configuration section to integrate with Spring.]]>
</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:attribute name="namespaces" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The comma-separated list of namespace names to integrate with Spring property sources.
If not specified, then default to application namespace.
]]>
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="order" type="xsd:int" use="optional">
<xsd:annotation>
<xsd:documentation>
<![CDATA[
The order of the config, default to Ordered.LOWEST_PRECEDENCE, which is Integer.MAX_VALUE.
If there are properties with the same name in diffe