disconf 的入门教程
参考博客:https://blog.youkuaiyun.com/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;
}
}