SpringBoot配置详解
SpringBoot配置详解
- 本文链接:http://blog.battcn.com/2018/04/22/springboot/v2-config-properties/
上一篇介绍了SpringBoot
由来及构建方式,通过第一章的教程我们对SpringBoot
不在感到陌生,可以发现SpringBoot
虽然干掉了 XML 但未做到零配置,它体现出了一种约定优于配置,也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。一般情况下默认的配置足够满足日常开发所需,但在特殊的情况下,我们往往需要用到自定义属性配置、自定义文件配置、多环境配置、外部命令引导等一系列功能。不用担心,这些SpringBoot
都替我们考虑好了,我们只需要遵循它的规则配置即可
准备前提
为了让SpringBoot
更好的生成数据,我们需要添加如下依赖(该依赖可以不添加,但是在 IDEA 和 STS 中不会有属性提示,没有提示的配置就跟你用记事本写代码一样苦逼,出个问题弄哭你去),该依赖只会在编译时调用,所以不用担心会对生产造成影响…
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>
自定义属性配置
在application.properties
写入如下配置内容
my1.age=25
my1.name=Luis
其次定义MyProperties1.java
文件,用来映射我们在application.properties
中的内容,这样一来我们就可以通过操作对象的方式来获得配置文件的内容了
package com.winterchen.properties;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;/*** Created by Donghua.Chen on 2018/6/1.*/
@Component
@ConfigurationProperties(prefix = "my1")
public class MyProperties1 {private int age;private String name;// 省略 get set@Overridepublic String toString() {return "MyProperties1{" +"age=" + age +", name='" + name + '\'' +'}';}
}
接下来就是定义我们的PropertiesController
用来注入MyProperties1
测试我们编写的代码,值得注意的是Spring4.x
以后,推荐使用构造函数的形式注入属性…
package com.winterchen.controller;import com.winterchen.properties.MyProperties1;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** Created by Donghua.Chen on 2018/6/1.*/
@RequestMapping("/properties")
@RestController
public class PropertiesController {private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);private final MyProperties1 myProperties1;@Autowiredpublic PropertiesController(MyProperties1 myProperties1) {this.myProperties1 = myProperties1;}@GetMapping("/1")public MyProperties1 myProperties1() {log.info("=================================================================================================");log.info(myProperties1.toString());log.info("=================================================================================================");return myProperties1;}
}
打开浏览器,输入如下地址:http://localhost:8080/properties/1,观察控制台,监听到如下内容则表示程序正确
2018-06-01 12:22:13.846 INFO 37514 --- [nio-8080-exec-1] c.w.controller.PropertiesController : =================================================================================================
2018-06-01 12:22:13.850 INFO 37514 --- [nio-8080-exec-1] c.w.controller.PropertiesController : MyProperties1{age=25, name='Luis'}
2018-06-01 12:22:13.851 INFO 37514 --- [nio-8080-exec-1] c.w.controller.PropertiesController : =================================================================================================
自定义文件配置
定义一个名为my2.properties
的资源文件,自定义配置文件的命名不强制application
开头
my2.age=25
my2.name=Luis
my2.email=1085143002@qq.com
其次定义MyProperties2.java
文件,用来映射我们在my2.properties
中的内容。
package com.battcn.properties;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;/*** @author Levin* @since 2018/4/23 0023*/
@Component
@PropertySource("classpath:my2.properties")
@ConfigurationProperties(prefix = "my2")
public class MyProperties2 {private int age;private String name;private String email;// 省略 get set @Overridepublic String toString() {return "MyProperties2{" +"age=" + age +", name='" + name + '\'' +", email='" + email + '\'' +'}';}
}
接下来在PropertiesController
用来注入MyProperties2
测试我们编写的代码
private final MyProperties1 myProperties1;private final MyProperties2 myProperties2;@Autowiredpublic PropertiesController(MyProperties1 myProperties1, MyProperties2 myProperties2) {this.myProperties1 = myProperties1;this.myProperties2 = myProperties2;}
@GetMapping("/2")
public MyProperties2 myProperties2() {log.info("=================================================================================================");log.info(myProperties2.toString());log.info("=================================================================================================");return myProperties2;
}
打开浏览器,输入如下地址:http://localhost:8080/properties/2,观察控制台,监听到如下内容则表示程序正确
2018-06-01 12:29:38.634 INFO 38345 --- [nio-8080-exec-2] c.w.controller.PropertiesController : =================================================================================================
2018-06-01 12:29:38.634 INFO 38345 --- [nio-8080-exec-2] c.w.controller.PropertiesController : MyProperties2{age=25, name='Luis', email='1085143002@qq.com'}
2018-06-01 12:29:38.634 INFO 38345 --- [nio-8080-exec-2] c.w.controller.PropertiesController : =================================================================================================
多环境化配置
在真实的应用中,常常会有多个环境(如:开发,测试,生产等),不同的环境数据库连接都不一样,这个时候就需要用到spring.profile.active
的强大功能了,它的格式为application-{profile}.properties
,这里的application
为前缀不能改,{profile}
是我们自己定义的。
创建application-dev.properties
、application-test.properties
、application-prod.properties
,内容分别如下
application-dev.properties
server.servlet.context-path=/dev
application-test.properties
server.servlet.context-path=/test
application-prod.properties
server.servlet.context-path=/prod
在application.properties
配置文件中写入spring.profiles.active=dev
,这个时候我们在次访问http://localhost:8080/properties/1就没用处了,因为我们设置了它的context-path=/dev
,所以新的路径就是http://localhost:8080/dev/properties/1,由此可以看出来我们激活不同的配置读取的属性值是不一样的
外部命令引导
前面三种方式都是基于配置文件层面的,那么有没有办法外部引导呢,假设这样的场景,我们对已经开发完成的代码打包发布,期间在测试环境测试通过了,那么即可发布上生产,这个时候是修改application.properties
的配置方便还是直接在命令参数配置方便呢,毫无疑问是后者更有说服力。默认情况下,SpringApplication
会将命令行选项参数(即:–property,如–server.port=9000)添加到Environment,命令行属性始终优先于其他属性源。
如何测试?
- 进入到项目目录,此处以我本地目录为主:/Users/Winterchen/Documents/mygit/springboot-learning-experience/spring-boot-config
- 然后打开 cmd 程序,不会在当前目录打开 cmd 的请自行百度,输入:
mvn package
- 打包完毕后进入到:/Users/Winterchen/Documents/mygit/springboot-learning-experience/spring-boot-config/target 目录中去,我们可以发现一个名为spring-boot-config-0.0.1-SNAPSHOT.jar的包
- 接着在打开 cmd 程序,输入:
java -jar spring-boot-config-0.0.1-SNAPSHOT.jar --spring.profiles.active=test --my1.age=32
。仔细观察spring.profiles.active=test
、my1.age=32
这俩配置的键值是不是似曾相识(不认识的请从开头认真阅读) - 最后输入测试地址:http://localhost:8080/test/properties/1我们可以发现返回的JSON变成了
{"age":32,"name":"Luis"}
表示正确
总结
- 掌握
@ConfigurationProperties
、@PropertySource
等注解的用法及作用 - 掌握编写自定义配置
- 掌握外部命令引导配置的方式
SpringBoot配置详解相关推荐
- 一起来学SpringBoot | 第二篇:SpringBoot配置详解
SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...
- SpringBoot的配置详解application
SpringBoot的配置文件application有两种文件格式,两种配置的内容是一致的,只是格式不一致. 1.application.properties 2.application.yml或者a ...
- SpringBoot——slf4j+logback日志处理及配置详解
SpringBoot--sl4j+logback日志处理及配置详解 日志的级别 打印级别:ALL > TRACE > FATAL > DEBUG > INFO > WAR ...
- creo配置文件config选项详解_5年资深架构师重点聚焦:SpringBoot的配置详解+内嵌Servlet容器
Spring Boot的配置详解 在本节中,我们将重点聚焦在Spring Boot的配置方面. 理解Spring Boot的自动配置 按照"约定大于配置"的原则,Spring Bo ...
- SpringBoot—整合log4j2入门和log4j2.xml配置详解
关注微信公众号:CodingTechWork,一起学习进步. 引言 对于一个线上程序或者服务而言,重要的是要有日志输出,这样才能方便运维.而日志的输出需要有一定的规划,如日志命名.日志大小,日志分 ...
- SpringBoot整合Log4j2以及配置详解
文章目录 标题SpringBoot整合Log4j2以及配置详解 1.加入依赖 2.在src.java.main.resources目录下创建log4j2.xml文件 log4j2.xml文件内容如下: ...
- SpringBoot配置文件中spring.profiles.active配置详解
SpringBoot配置文件中spring.profiles.active配置详解 1.多环境配置 我们在开发Spring Boot应用时,通常同一套程序会被应用和安装到几个不同的环境,比如:开发.测 ...
- Springboot多数据源配置详解
Springboot多数据源配置详解 概念 配置 多数据源使用 概念 一般来说,我们正常的业务只涉及一个数据源,在特定的业务场景中需要使用多个数据源的情况,就需要配置多个数据源来满足特定的业务需求.本 ...
- Springboot整合redis配置详解
Springboot整合redis配置详解 1.导入依赖 <dependency><groupId>org.springframework.boot</groupId&g ...
最新文章
- 手游 android 分辨率 适配,安卓不得不玩的神作,十大超高画质手游(安卓)
- Entity Framework中的Migrations
- Tesseract OCR——Windows 10 + CMake-GUI + Visual Studio 2019下编译和使用解决方案
- 【Spring】Resource接口:ClassPathResource
- python列表求平均值_python与统计概率思维
- SAP Cloud for Customer Sales Order Pricing Date的配置
- mysql双机热备 读写分离_轻松搭建MySQL主从复制、读写分离(双机热备)
- JS-深入理解继承(非class方式与class继承)
- 【开发者portal在线开发插件系列三】字符串 及 可变长度字符串
- electron加载html加载不起来,Electron 预加载远程页面提升用户体验
- python 科研统计_Python科研统计作图Plotnine+Seaborn+matplotlib替代R ggplot2系列!(二)...
- Atitit nlp 自然语言处理attilax总结 目录 1.1. 主要范畴	1 1.2. 研究难点	2 2. Ati涉及的领域(文档 tts 分词 抽取 摘要 检索)	2 3. Atit
- VM虚拟机安装centos6.3详细图文教程
- unity3d游戏3d横版格斗游戏完整项目源码分享
- win7安装IIS后如何远程访问IIS
- 峨眉山徒步休闲三日游攻略内附详细时间
- 「教程」Windows程序未响应,怎么分析确认具体原因
- 如何编制试算平衡表_试算平衡表的编制步骤是怎样的?
- 一日一Shader·进阶版笔刷【SS_18】
- Markdown学习记录 [ javaSE 基础Ⅰ] Day01
热门文章
- ajax onload怎么用,Ajax中onload和onreadystatechange两种请求方式的区别
- cortex_m0架构(顶层AHB_Lite接口)
- linux路由内核实现分析(四)---路由缓存机制(1)
- [MFC] CDialog::DoModal()函数用法
- Android默认记住登录用户名,【教程】Android 记住密码和自动登录界面的实现
- 《Reids 设计与实现》第四章 整数集合和压缩列表
- MyBatis 阶段总结
- 项目分层思路——管家婆
- [转载]Oracle触发器用法实例详解
- gerrit与crowdid, openid集成,设置openIdSsoUrl 直接登录