spring-boot 速成(6) 整合disconf
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时完全一样了,不再赘述。
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
分类:
09.Open Source
, 15.Java/Scala
标签:
spring-boot
, disconf
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 几个技巧,教你去除文章的 AI 味!
· 系统高可用的 10 条军规
· 关于普通程序员该如何参与AI学习的三个建议以及自己的实践
· 对象命名为何需要避免'-er'和'-or'后缀
· AI与.NET技术实操系列(八):使用Catalyst进行自然语言处理
2015-05-21 hadoop:将WordCount打包成独立运行的jar包
2013-05-21 javascript:算法笔记
2011-05-21 Silverlight Telerik控件学习:数据录入、数据验证
2010-05-21 Flash/Flex学习笔记(52):使用TweenLite
2009-05-21 Enterprise Library 4.1学习笔记1----配置应用程序块(c/s和b/s均适用)