[TOC]
todo
- 在应用资源根目录下创建app-info.properties文件
-
xml中添加qconfig namespace
<?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:context="http://www.springframework.org/schema/context" xmlns:qconfig="http://www.qunar.com/schema/qconfig" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.qunar.com/schema/qconfig http://www.qunar.com/schema/qconfig.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
-
在Spring配置文件中添加如下配置即可
<context:annotation-config /> <!-- 添加如下配置后即可使用注解热加载 --> <qconfig:annotation-driven />
主要使用场景有两种
-
在XML中使用
这种使用方式无法热加载,但是很有必要
-
在代码中使用
在配置文件中使用示例如下
<!-- 这里可以写多个文件,用逗号分隔 -->
<qconfig:config files="zookeeper.properties,mysql.properties"/>
<!-- 如果想写多个 qconfig:config,不是写在一行,请注意这里的ignore-unresolvable -->
<qconfig:config files="config.properties" ignore-unresolvable="true" />
<qconfig:config files="configt.properties" ignore-unresolvable="true" />
<!-- 如果还需要引入spring原生的properties文件呢? -->
<context:property-placeholder location="classpath:xxxx.properties" ignore-unresolvable="true"/>
<!-- 其他地方的使用方式与properties文件的方式一致 -->
<bean id="orderService" class="com.qunar.hotel.OrderService">
<property name="zkAddress" value="${zkAddress}" />
</bean>
注意:当配置文件不是写在一行,而使用了多个<qconfig:config files="filename" ignore-unresolvable="true" />来配置多个配置文件,或<qconfig:config files="config.properties" ignore-unresolvable="true" />标签与<context:property-placeholder location="classpath:xxxx.properties" ignore-unresolvable="true"/>标签混用时,都需要加上ignore-unresolvable="true"属性,否则会报unresolved异常。
使用代码加载存在两种方式加载。