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时完全一样了,不再赘述。
spring-boot 速成(6) 整合disconf的更多相关文章
- Spring Boot 2.x整合Redis
最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...
- spring boot 2.0 整合 elasticsearch6.5.3,spring boot 2.0 整合 elasticsearch NoNodeAvailableException
原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 原码云项目地址:https://gi ...
- Spring Boot入门 and Spring Boot与ActiveMQ整合
1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...
- Spring Boot和Dubbo整合
provider端 POM依赖 <dependencies> <dependency> <groupId>org.springframework.boot</ ...
- 转-Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合
Hive/Phoenix + Druid + JdbcTemplate 在 Spring Boot 下的整合 http://blog.csdn.net/balabalayi/article/detai ...
- RabbitMQ入门:在Spring Boot 应用中整合RabbitMQ
在上一篇随笔中我们认识并安装了RabbitMQ,接下来我们来看下怎么在Spring Boot 应用中整合RabbitMQ. 先给出最终目录结构: 搭建步骤如下: 新建maven工程amqp 修改pom ...
- Spring Boot与ActiveMQ整合
Spring Boot与ActiveMQ整合 1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <dependency> <groupId>org.spri ...
- Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来
计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来. 本文目录 一.Spring Ca ...
- Spring Boot 2.0 整合携程Apollo配置中心
原文:https://www.jianshu.com/p/23d695af7e80 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够 ...
- Spring Boot 2.x 整合 Redis最佳实践
一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...
随机推荐
- [转载]VS2013 密钥 – 所有版本
http://www.wxzzz.com/307.html Visual Studio Ultimate 2013 KEY(密钥):BWG7X-J98B3-W34RT-33B3R-JVYW9 Visu ...
- insert into与insert ignore以及replace into的区别
insert ignore表示,如果表中已经存在相同的记录,则忽略当前新数据: INSERT INTO有无数据都插入,如果主键则不插入; REPLACE INTO 如果是主键插入则会替换以前的数据; ...
- fuzz for test of the Net::HTTP::GET
use Net::HTTP::GET; % %0e%0f ' *%26 @.jpg>; my $count = 0; for @chars X @chars X @chars X @chars ...
- free vmstat查看内存及系统调优【转】
内存查看 查看内存是否存在瓶颈,使用top指令看比较麻烦,而free命令更为直观: [/home/weber#]free total used free shared buffers cached M ...
- 使用配置文件启动MongoDB
Ubuntu 16.04 (阿里云ECS),MongoDB 4.0, 原来,已经写了10篇MongoDB的随笔了.可是,自己居然没有使用配置文件启动过MongoDB,对其更多的配置是不明白的. 昨天( ...
- 为你的 Hadoop 集群选择合适的硬件
随着Apache Hadoop的起步,云客户的增多面临的首要问题就是如何为他们新的的Hadoop集群选择合适的硬件. 尽管Hadoop被设计为运行在行业标准的硬件上,提出一个理想的集群配置不想提供硬件 ...
- java 内部类使用 .this 和 .new
如果需要生成对外部类对象的引用,可以使用外部类的名字后面紧跟圆点和this,这样产生的引用自动地具有正确的类型,这一点在编译器就被知晓并受到检查,因此并没有运行时开销 //: innerclasses ...
- Spring对象依赖关系处理
Spring中给对象属性赋值 1.通过set方法给属性注入值 2.p名称空间 3.自动装配 4.注解 编写MVCModel调用userAction MVCModel public class MVCM ...
- 一个洛谷Material化的Stylish美化主题
Luogu Argon Design 新主题 Luogu Argon Design 目前已经取代了 Luogu Material,但这并不代表 Luogu Material 会停止更新,在 Luogu ...
- 使用div模拟出frameset效果
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml" > <head> < ...