disconf

disconf 的入门教程

参考博客:https://blog.csdn.net/qinxu0611/article/details/86146263

1. 引入 pom 依赖

2. 在Spring 中配置

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- disconf 需要配置切面 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <context:component-scan base-package="com.bigguy.box"></context:component-scan>

<!-- 固定写法,只需要改变包名即可 -->
    <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
          destroy-method="destroy">
    <property name="scanPackage" value="com.bigguy.box"/>
    </bean>

    <!-- 使用托管方式的disconf配置(无代码侵入, 配置更改会自动reload) -->
    <bean id="configproperties_disconf"
          class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
        <property name="locations">
            <list>
                <!--多个动态配置配置在这里-->
                <value>classpath:hello.properties</value>
                <value>classpath:jdbc.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>

3.注入到配置文件中

@Component
public class HelloConfig {

    // 注意 注入的方式
    @Value("#{configproperties_disconf['hello.name']}")
    public String name;
    @Value("#{configproperties_disconf['hello.age']}")
    public int age;

    // get/set
  ...
}

4. 测试 Controller

@Controller
public class UserController {

    @Autowired
    HelloConfig helloConfig;


    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        String name = helloConfig.getName();
        System.out.println(name);
        return name;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值