学习目标


今天我们一起学习一下 Bootstrap 配置的相关知识,在学习目标中我已经列出了今天需要学习的知识点,第一个知识点为复习知识点,属于 Spring Boot 中的知识,这里我们既然讲到了配置文件,就顺便把 Spring Boot 支持的两种配置文件类型的相关知识复习一下,有助于我们对 Bootstrap 配置的学习。 首先我们先创建项目:打开 https://start.spring.io/,填写相关信息,添加 Web、Actuator 以及 Cloud Bootstrap 依赖,点击 “Generate Project” 按钮生成项目,并导入到 idea 中。(注:此处使用的 Spring Boot 版本为 1.X 系列)

一、(复习)为什么 Spring Boot 可以支持两种配置文件类型

熟悉 Spring Boot 的小伙伴一定知道 Spring Boot 是支持两种配置文件类型的: application.yaml / application.yml 、 application.properties

这里我们通过源代码查找的方式,看一下为什么 Spring Boot 会支持这两种配置文件类型。 首先,我们要找到加载上下文环境的类--ConfigFileApplicationListener,这个类是通过加载本地已知的配置文件中的属性来配置上下文环境。 然后在这个类中找到 Loader#load() 方法,查看 PropertySourcesLoader 源码,找到

private final List<PropertySourceLoader> loaders;

这里我们查看一下 PropertySourceLoader 接口的实现类,可以发现,有两个类实现了该接口:

  • PropertiesPropertySourceLoader
  • YamlPropertySourceLoader

具体代码就不再展开讲解了,已经十分明了了。


二、Bootstrap 配置文件

我们都知道,Spring Boot 中使用 application.properties 配置文件,通过上篇文章对 Bootstrap 上下文的学习,我们应该也能猜到,在 Spring Cloud 中使用 bootstrap.properties 配置文件。也就是说,在 Spring Cloud 项目中,application.properties 和 bootstrap.properties 配置文件是同时存在的。 在 BootstrapApplicationListener#onApplicationEvent() 方法中,可以看出当 spring.cloud.bootstrap.name:bootstrap 存在时,使用该配置项,否则,使用 "bootstrap" 默认值。

String configName = environment.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");

所以我们在项目的 resources 目录下新建名为 bootstrap.properties 的配置文件,这个就是我们 Spring Cloud 中的配置文件。


三、调整 Bootstrap 配置文件名称

由于 Spring Cloud 的默认配置文件为 bootstrap.properties,那如果我们想要修改配置文件的名称应该如何做呢?这里我们使用调整程序启动参数的方式来进行修改。 首先我们在 Idea 中添加如下程序启动参数:

--spring.cloud.bootstrap.name=spring-cloud

bootstrap 配置文件名称发生了改变,改变成了 "spring-cloud",现有三个文件:(标黑的为我们的期望值

  • application.properties

    • spring.application.name=spring-cloud-client
  • bootstrap.properties
    • spring.application.name = spring-cloud-config-client-demo
  • spring-cloud.properties
    • spring.application.name = spring-cloud

其中 application.properties(ConfigFileApplicationListener 被加载了)和 spring-cloud.properties 读取了,这是我们所期望的。 重启项目,在 http://localhost:8080/env 中查看结果,结果也是和我们所期望的是相同的:


四、调整 Bootstrap 配置文件路径

既然配置文件名称是可以修改的,那么接下来我们尝试使用类似的方式修改一下 Bootstrap 配置文件路径,这里我们在 resources 目录下新建 config/spring-cloud2.properties 文件,添加如下程序启动参数:

--spring.cloud.bootstrap.name=spring-cloud2
--spring.cloud.bootstrap.location=config

现有四个文件:(标黑的为我们的期望值

  • application.properties

    • spring.application.name = spring-cloud-client
  • bootstrap.properties
    • spring.application.name = spring-cloud-config-client-demo
  • spring-cloud.properties
    • spring.application.name = spring-cloud
  • config/spring-cloud2.properties
    • spring.application.name = spring-cloud2

重启项目,在 http://localhost:8080/env 中查看结果,结果也是和我们所期望的是相同的:


五、自定义 Bootstrap 配置(实现 Spring 标准接口 )

上面说了这么多 Bootstrap 配置相关的东西,一直使用的都是别人的配置项,接下来我们来自定义配置,首先我们实现 Spring 的标准接口来完成自定义Bootstrap 配置 :

1、创建 META-INF/spring.factories 文件(类似于 Spring Boot 自定义 Starter)

2、创建自定义 Bootstrap 配置 Configuration

package top.alanshelby.springcloudchapter2.bootstrap;import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;import java.util.HashMap;
import java.util.Map;/*** Bootstrap 配置 Bean** @ClassName MyConfiguration* @Author AlanShelby* @Date 2019-04-22 14:29:14 14:29* @Version 1.0*/
@Configuration
public class MyConfiguration implements ApplicationContextInitializer {@Overridepublic void initialize(ConfigurableApplicationContext applicationContext) {ConfigurableEnvironment environment = applicationContext.getEnvironment();// 获取 PropertySourceMutablePropertySources propertySources = environment.getPropertySources();// 定义一个新的 PropertySource 放在首位propertySources.addFirst(createPropertySource());}private PropertySource createPropertySource() {Map<String, Object> source = new HashMap<>();source.put("name", "AlanShelby");PropertySource propertySource = new MapPropertySource("my-property-source", source);return propertySource;}
}

上面的代码是我们定义了一个新的 PropertySource 并将其放到了 MutablePropertySources 首位,这个配置项的 Key 值是 my-property-source,里面对应的属性信息为 source.put("name", "AlanShelby")

3、配置 META-INF/spring.factories 关联 Key org.springframework.cloud.bootstrap.BootstrapConfiguration

org.springframework.cloud.bootstrap.BootstrapConfiguration =top.alanshelby.springcloudchapter2.bootstrap.MyConfiguration

然后浏览器访问 http://localhost:8080/env,可以看到以下信息,表示我们自定义的配置成功了:


六、自定义 Bootstrap 配置(实现 SpringCloud 的接口)

上面我们使用了实现 Spring 标准接口的方式自定义了 Bootstrap 配置项,接下来我们来实现 Spring Cloud 接口来实现该功能。

1、实现 PropertySourceLocator

package top.alanshelby.springcloudchapter2;import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
import org.springframework.core.env.*;import java.util.HashMap;
import java.util.Map;/*** 自定义 Bootstrap 配置属性源(实现 SpringCloud 的接口)** @ClassName MyPropertySourceLocator* @Author AlanShelby* @Date 2019-04-22 14:45:15 14:45* @Version 1.0*/
public class MyPropertySourceLocator implements PropertySourceLocator {@Overridepublic PropertySource<?> locate(Environment environment) {if (environment instanceof ConfigurableEnvironment) {ConfigurableEnvironment configurableEnvironment = ConfigurableEnvironment.class.cast(environment);MutablePropertySources propertySources = configurableEnvironment.getPropertySources();propertySources.addFirst(createPropertySource());}return null;}private PropertySource createPropertySource() {Map<String, Object> source = new HashMap<>();source.put("spring.application.name", "AlanShelby-SpringCloud");PropertySource propertySource = new MapPropertySource("alan-property-source", source);return propertySource;}
}

2、配置 META-INF/spring.factories 关联 Key org.springframework.cloud.bootstrap.BootstrapConfiguration:

org.springframework.cloud.bootstrap.BootstrapConfiguration = top.alanshelby.springcloudchapter2.MyPropertySourceLocator

然后浏览器访问 http://localhost:8080/env,可以看到以下信息,表示我们自定义的配置成功了:


至此,关于 Spring Cloud 中的 Bootstrap 配置就讲解完了,这是我的理解,各位看官如果有不同见解或文章中有错误,请不吝指正。

所用代码码云地址:https://gitee.com/AlanShelby/spring-cloud-chapter

个人微信公众号:AlanShelby(多多关注,感谢~)

bootstrap 点击图片放大查看_Spring Cloud 之 Bootstrap 配置相关推荐

  1. bootstrap 点击图片放大查看_Bootstrap 开源 SVG 图标库 Bootstrap Icons

    (给程序员的那些事加星标) 转自:开源中国 Bootstrap 开源了首套 SVG 图标库 Bootstrap Icons,其团队表示这是有史以来第一次拥有自己的图标库,此图标库起初专门针对其从表单控 ...

  2. 实现点击图片放大查看功能

    1.html 代码 <div id="imgEnlargeDiv" style="display: none; text-align: center;positio ...

  3. layer.photos 点击图片放大查看

    $("body").on("click",".imgs img",(e) => {layer.photos({photos: { &q ...

  4. 点击图片放大缩小功能

    1.点击图片放大缩小的思路 图片部分: <table><div><img style="width:62px;height:83px;display:block ...

  5. jquery点击图片放大效果

    点击图片放大效果无非就是创建一个大容器,点击小图片获取图片路径存放到大容器里. 接下来看一下效果图 HTML结构 <img class="enlargeImg" width= ...

  6. js实现点击图片放大效果,以及懒加载图片

    js实现点击图片放大效果,以及懒加载图片 近期有个后端管理页面小优化,原来的图片是点击才会去后端请求图片展示到前端,用dialog的方式展示,但是不太直观 存在两个问题 1.点击查看后,电子照片会变形 ...

  7. 微信小程序系列——点击图片放大预览

    需求 开发的时候,把图片放到页面上,点击图片没有任何反应,不能放大也不能缩小 这怎么能行!!! 所以需求来了:点击图片能够把图片弹出来,这样就能放大图片看细节了! 实现步骤 微信提供了预览图片的接口, ...

  8. 初学者笔记——微信小程序点击图片放大

    微信小程序点击图片放大 wx.previewImage 官方文档的解释 PS:红色框框内要注意,需要预览的图片连接列表只支持网络连接图片,2.2.3版本以上支持云文件ID. 将图片dream.jpg上 ...

  9. vue项目 一行js代码搞定点击图片放大缩小

    一行js代码搞定xue项目需要点击图片放大缩小,其实主要用的是用到了vue:class的动态切换,内容比较简单.一开始我把维护的需求想得太复杂了,和测试小姐姐聊了一下才反应过来. 两个月不到跟了四个项 ...

最新文章

  1. WMI技术介绍和应用——接收事件
  2. Python中os和shutil模块实用方法集锦
  3. Linux 环境运维 - 查看远程调试端口被占用的ip地址,设置连接处于空闲状态快速自动化断开方法,keepalive机制相关说明
  4. GitLab-使用SSH的方式拉取和推送项目
  5. 一名拿到阿里offer的Java程序员分享三轮面试经验
  6. 无状态的HTTP协议
  7. C和指针之函数递归实现把amount表示的值转换为单词形式written_amount(unsigned int amount,char *buffer)
  8. JS点击获取验证码后60秒内禁止重新获取(防刷新)
  9. java包管理之maven安装
  10. html左侧隐藏菜单栏,如何制作一个炫酷的隐藏侧边栏菜单
  11. python print 换行_和我一起学Python?第1讲——Print()函数
  12. 1007 素数对猜想 (20 分)—PAT (Basic Level) Practice (中文)
  13. Win11系统怎么更新显卡驱动 手动更新显卡驱动程序的方法
  14. 如何编制试算平衡表_编制试算平衡表
  15. 中小科技企业新蓝图,抓住资本新机遇!北京证券交易所要来了
  16. img标签 图片报错处理
  17. OR-CAD CAPTURE学习笔记——ERROR(ORCAP-11010)
  18. matlab 医学断层图像,利用MATLAB实现CT断层图像的三维重建
  19. Windows/Ubuntu16.04双系统和ros安装方法及可能出现的问题
  20. 等离子气化技术优势分析

热门文章

  1. oracle execute immediate 单引号嵌套,Oracle EXECUTE IMMEDIATE语句里面的引号处理
  2. 【编译原理】课程实验——基于Java的词法分析与语法分析
  3. 动力电池回收法规出炉丨IBM量子计算新突破丨iphone X冬天不能用?苹果:恩!
  4. 小米集团副总裁崔宝秋:人类正进入“AIoT+5G”超级互联网时代
  5. Android svg矢量图实现心跳动画
  6. linux usb设备连接失败怎么办,Linux中使用扩展USB Hub的问题
  7. 黑武器linux下载地址,酷毙了!暗黑版 Arch,BlackArch Linux 2017.03.01发布
  8. Python入门必备知识
  9. 猜数字小游戏实现方法及其思路
  10. 育碧旗下游戏盗版率超过93% 深受其害