文章出處

spring-boot雖然不推薦使用xml文件做為配置文件,但是并沒有把路堵死,所以與disconf的整合,仍舊可以沿用之前的xml方式來處理。

 

一、在Application類上用注解導入xml

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

@SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
@ImportResource({"classpath:spring-context.xml"})
public class WebApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }
}

注意這行 @ImportResource({"classpath:spring-context.xml"}) ,這里導入了一個xml的配置入口文件,這個是關鍵!

spring-context.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath:spring-disconf.xml"/>
    <import resource="classpath:spring-bean.xml"/>

</beans>

 

二、disconf配置文件spring-disconf.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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
          destroy-method="destroy">
        <property name="scanPackage" value="com.example"/>
    </bean>
    <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
          init-method="init" destroy-method="destroy">
    </bean>

    <bean id="configproperties_disconf"
          class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>app.properties</value>
            </list>
        </property>
    </bean>

    <bean id="propertyConfigurer"
          class="com.baidu.disconf.client.addons.properties.ReloadingPropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true"/>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="propertiesArray">
            <list>
                <ref bean="configproperties_disconf"/>
            </list>
        </property>
    </bean>
</beans>

跟以前不用spring-boot的時候,一毛一樣。當然還要有一個disconf.properties文件,參考下圖:

 

三、spring-bean.xml中使用disconf注入的屬性

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="appConfig" class="com.example.config.AppConfig">
        <property name="checkSign" value="${checkSign}"/>
        <property name="sendEmailWhenStart" value="${sendEmailWhenStart}"/>
        <property name="env" value="${app.env}"/>
        <property name="sendEmailWhenError" value="${sendEmailWhenError}"/>
    </bean>

</beans>

AppConfig是一個演示用的配置類

package com.example.config;

import lombok.Data;

/**
 * Created by yangjunming on 2017/4/17.
 */
@Data
public class AppConfig {
    private String env;
    private boolean sendEmailWhenStart;
    private boolean sendEmailWhenError;
    private boolean checkSign;
}

剩下的事情,就跟之前用spring+disconf時完全一樣了,不再贅述。


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()