Spring Boot 属于约定大于配置,就是说 Spring Boot 推荐不做配置,很多都是默认配置,但如果想要配置系统,使得软件符合业务定义,Spring Boot 可以通过多种方式进行配置。

Spring Boot 配置文件默认在 src/main/resouces/application.properties ,配置文件可以是 .properties 也可以是 .yml 后缀。两者除了展示形式,没有区别。

配置文件的三种方式

  1. bootstrap 开头 .properties 后缀 或 .yml 后缀,通常用于 Spring Cloud 应用
  2. application 开头 .properties 后缀 或 .yml 后缀,每个 Spring Boot 应用默认
  3. 通过 @Configuration 注解的代码类配置
  4. 通过 cmd 命令行配置

1 bootstrap

1.1 bootstrap 应用场景

因为本系列是 Spring Boot 教程实例,没有设计到 Spring Cloud ,而 Bootstrap 实际应用场景是在 Spring Cloud,本章也不具体讨论。

1.2 bootstrap 与 application

bootstrap.yml(bootstrap.properties)用来程序引导时执行,应用于更加早期配置信息读取,如可以使用来配置application.yml中使用到参数等。

application.yml(application.properties) 应用程序特有配置信息,可以用来配置后续各个模块中需使用的公共参数等。

bootstrap.yml 先于 application.yml 加载

为什么要有 bootstrap 配置

2 application

application.yml(application.properties) 是针对独立 Spring Boot 的应用配置文件,跟 .NET 的 web.confg(app.confg) 文件功能一样。

2.1 application 常用配配置

2.1.1 端口配置

properties 格式

#指定springboot内嵌容器启动的端口,默认使用tomcat容器时在8080端口
server.port=8081

yml 格式

#指定springboot内嵌容器启动的端口,默认使用tomcat容器时在8080端口
server
port: 8081

2.1.2 thymeleaf组件配置

properties 格式

#是否开启thymeleaf缓存
spring.thymeleaf.cache=false
#thymeleaf路径
spring.thymeleaf.prefix=classpath:/templates/
#后缀
spring.thymeleaf.suffix=.html
#编码
spring.thymeleaf.encoding=UTF-8
#文本类型
spring.thymeleaf.content-type=text/html
#展示形式
spring.thymeleaf.mode=HTML5

yml 格式

#thymelea模板配置
spring:
thymeleaf:
#缓存
cache: false
#thymeleaf 所在路径
prefix: classpath:/templates/
#thymeleaf 后缀
suffix: .html
#thymeleaf 采用的标准
mode: HTML5
#thymeleaf 编码格式
encoding: UTF-8

2.1.3 数据库连接配置

properties 格式

#描述数据源 #编码、时区等格式
spring.datasource.url=jdbc:mysql://localhost:3306/tanglong?useUnicode=true&
characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=Asia
#数据源用户名
spring.datasource.username=root
#数据源密码
spring.datasource.password=0000
#数据源驱动包名
spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
#数据源类型
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource

2.1.4 数据持久化

#是否打印sql语句
spring.jpa.show-sql= true
#mybatis配置文件路径
mybatis.config-location=classpath:MyBatis.xml
#指定 mybatis 本地路径,例如在 mybatis/mappings 下所有的 xml 后缀的文件
mybatis.mapper-locaitons=classpath:mybatis/mappings/*.xml
#打印myBatis的sql语句 com.demo.mapper 为包名
logging.level.com.demo.mapper=debug
#别名实体包,多个逗号隔开
mybatis.type-aliases-package=com.user.bean

2.1.5 多种开发环境

多环境配置请参见 Spring Boot 多环境如何配置

#开发/测试/生产环境分别对应dev/test/prod,可以自由定义,当前配置为开发环境
spring.profiles.active=dev #不同环境中的配置信息可以写在其他文件中
application-test.properties 或者 application-prod.properties

2.2 如何获取 application 中的值

2.2.1 新建一个类标注 @ConfigurationProperties

  1. 新建一个 java 类
  2. 给类加 @ConfigurationProperties(prefix = "corn") 其中 prefix 表前缀,例如 corn.username=123,那么corn就是前缀

    假设 application.yml 代码如下
corn:
uploadPath: /Users/jiaojunkang/Documents/project_java/corn/src/corn-master/files/
rootPath: /Users/jiaojunkang/Documents/project_java/fwrsoft/html/
host: corn.icontag.cn
componentAppId:
componentSecret:
componentToken:
componentAesKey:
baiduApikey:
baiduSecretkey:
baiduAppId:
smsUrl:
smsUid:
smsPwd:
pageSize: 10

那么对应的 java 配置类


@Component
@ConfigurationProperties(prefix = "corn")
public class CornConfig {
//上传路径
private String uploadPath; //静态文件跟目录
private String rootPath; //HOST
private String host;
//微信开发平台appid
private String componentAppId;
private String componentSecret;
private String componentToken;
private String componentAesKey; private String baiduApikey;
private String baiduSecretkey; private String smsUrl; private String pageSize; public String getPageSize() {
return pageSize;
} public void setPageSize(String pageSize) {
this.pageSize = pageSize;
} public String getSmsUrl() {
return smsUrl;
} public void setSmsUrl(String smsUrl) {
this.smsUrl = smsUrl;
} public String getSmsUid() {
return smsUid;
} public void setSmsUid(String smsUid) {
this.smsUid = smsUid;
} public String getSmsPwd() {
return smsPwd;
} public void setSmsPwd(String smsPwd) {
this.smsPwd = smsPwd;
} private String smsUid; private String smsPwd; public String getBaiduAppId() {
return baiduAppId;
} public void setBaiduAppId(String baiduAppId) {
this.baiduAppId = baiduAppId;
} private String baiduAppId; public String getUploadPath() {
return uploadPath;
} public void setUploadPath(String uploadPath) {
this.uploadPath = uploadPath;
} public String getRootPath() {
return rootPath;
} public void setRootPath(String rootPath) {
this.rootPath = rootPath;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public String getComponentAppId() {
return componentAppId;
} public void setComponentAppId(String componentAppId) {
this.componentAppId = componentAppId;
} public String getComponentSecret() {
return componentSecret;
} public void setComponentSecret(String componentSecret) {
this.componentSecret = componentSecret;
} public String getComponentToken() {
return componentToken;
} public void setComponentToken(String componentToken) {
this.componentToken = componentToken;
} public String getComponentAesKey() {
return componentAesKey;
} public void setComponentAesKey(String componentAesKey) {
this.componentAesKey = componentAesKey;
} public String getBaiduApikey() {
return baiduApikey;
} public void setBaiduApikey(String baiduApikey) {
this.baiduApikey = baiduApikey;
} public String getBaiduSecretkey() {
return baiduSecretkey;
} public void setBaiduSecretkey(String baiduSecretkey) {
this.baiduSecretkey = baiduSecretkey;
}
}

2.2.2 在普通的类中使用 value 配置

给属性增加 @Value 标签,例如 使用 @Value("${spring.redis.host}") 标注 host 表示 Redis

@Value("${spring.redis.host}")
private String host = "127.0.0.1"; @Value("${spring.redis.port}")
private int port = 6379; // 0 - never expire
private int expire = 0; //timeout for jedis try to connect to redis server, not expire time! In milliseconds
@Value("${spring.redis.timeout}")
private int timeout = 0; @Value("${spring.redis.password}")
private String password = "";

3 @Configuration

使用 @Configuration 也可以替代部分 application.yml 文件配置,但 @Configuration 可以更加灵活的实现配置的自定义,他们的区别或者在于 @Configuration 可以动态配置,缺点是一旦发布无法更改。

  1. @Configuration 通常用于 aplication的补充
  2. @Configuration 用在类上,@Bean 用在方法上

例如配置 QuartzConfigration 通常采用的配置


@Configuration
public class QuartzConfigration {
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/config/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
} // 创建schedule
@Bean(name = "scheduler")
public Scheduler scheduler() {
return schedulerFactoryBean().getScheduler();
}
}

4 命令行配置

Spring Boot 在服务器部署可以使用下面命令,这时使用 -- 开头引入 spring 中的 application 的值,即可在命令行配置 Spring Boot。

java -jar xxxx-0.0.1-SNAPSHOT.jar

我们可以通过在命令行增加配置的方式给 Spring Boot 添加配置,命令行配置优先于 application.yml 执行。

如下配置了一个端口,使用 --server.port=8088 配置 来实现命令行配置

java -jar xxxx-0.0.1-SNAPSHOT.jar --server.port=8088 --spring.profiles.active=dev

Spring Boot 配置文件和命令行配置的更多相关文章

  1. Spring Boot程序接收命令行参数

    Spring Boot程序接收命令行参数 输入一行,回车,触发一次.如果想要调用service层,也是可以,能调用service层,就可以做很多事,触发一次就好比调用了一次http接口一样 packa ...

  2. Spring boot 配置文件参数映射到配置类属性

    [参考文章]:SpringBoot之@EnableConfigurationProperties分析 [参考文章]:在Spring Boot中使用 @ConfigurationProperties 注 ...

  3. Spring Boot配置文件yml讲解--行内对象的配置方式

    yml行内对象的配置方法,一般是采取 上面的缩进方式,我只想配置在一行怎么处?——

  4. 详解Spring Boot配置文件之多环境配置

    一. 多环境配置的好处: 1.不同环境配置可以配置不同的参数~ 2.便于部署,提高效率,减少出错~ 二. properties多环境配置 1. 配置激活选项 spring.profiles.activ ...

  5. Spring Boot 配置文件 – 在坑中实践

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢!   『 仓廪实而知礼节,衣食足而知荣辱 - 管仲 』   本文提纲 一.自动配置 二.自定义 ...

  6. Springboot 系列(二)Spring Boot 配置文件

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 不管是通过官方提供的方式获取 Spring ...

  7. Spring Boot 启动(二) 配置详解

    Spring Boot 启动(二) 配置详解 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring Boot 配置 ...

  8. 史上最全的Spring Boot配置文件详解

    Spring Boot在工作中是用到的越来越广泛了,简单方便,有了它,效率提高不知道多少倍.Spring Boot配置文件对Spring Boot来说就是入门和基础,经常会用到,所以写下做个总结以便日 ...

  9. Spring Boot 配置文件中的花样,看这一篇足矣!

    在快速入门一节中,我们轻松的实现了一个简单的RESTful API应用,体验了一下Spring Boot给我们带来的诸多优点,我们用非常少的代码量就成功的实现了一个Web应用,这是传统的Spring应 ...

随机推荐

  1. Java Web基础面试题整理

    Tomcat的缺省端口是多少,怎么修改 tomcat默认缺省端口是8080 修改方法: 找到Tomcat目录下的conf文件夹 进入conf文件夹里面找到server.xml文件 打开server.x ...

  2. SAP-批量删除生产订单

    1.SE38运行:PPARCHP1 2.先用COOIS导出订单,已经CLSD,没有删除的

  3. 使用re.split 按标点+空格的一种分割办法

    import re import string t1 = re.split("["+string.punctuation+" ]","(555) 12 ...

  4. 100天搞定机器学习|day43 几张GIF理解K-均值聚类原理

    前文推荐 如何正确使用「K均值聚类」? KMeans算法是典型的基于距离的聚类算法,采用距离作为相似性的评价指标,即认为两个对象的距离越近,其相似度就越大.该算法认为簇是由距离靠近的对象组成的,因此把 ...

  5. ATX agent+UIautomation2 自动化测试介绍

    纯搬运贴,内容几乎来源于作者的几篇介绍文章,这里做了整合 目前ATX+UIautomator2 处于自动化界的浪口风尖,现在有幸终于有时间对ATX进行了粗浅的了解 为什么要用ATX ATX+UIaut ...

  6. Springboot源码分析之代理三板斧

    摘要: 在Spring的版本变迁过程中,注解发生了很多的变化,然而代理的设计也发生了微妙的变化,从Spring1.x的ProxyFactoryBean的硬编码岛Spring2.x的Aspectj注解, ...

  7. DIY显示器篇------DIY教程

    前言: DIY显示器是这几年才火起来的,或者说这几年在游戏圈火起来的.我第一次看到是在NGA上,一位玩PUBG的大佬自己DIY了一个显示器,27寸 2k 144 ips的屏幕,当时市面上只有四款显示器 ...

  8. 运行所选代码生成器时出错:“值-1超出了可接受的[0,2147483647]范围。参数名称:value”

    在使用vs2019添加mvc控制器的时候 这已经是第二次遇到这个问题了.常言道,多喝热水,重启试试.有时候当应用工作不正常,重启也许能解决问题.但是程序员通常接触不到服务器系统权限.而运维人员和公司流 ...

  9. 程序员过关斩将--cookie和session的关系其实很简单

    月高风下,下班路上.... 菜菜哥,告诉你一个秘密,但是不允许告诉任何人 这么秘密,你有男票了?~ 不是,昨天我偷偷去面试了,结果挂了 这不是好事吗,上天让公司留住你..... 好吧,不过还是要请教你 ...

  10. IntelliJ IDEA 2019 快捷键终极大全,速度收藏!

    转载注明:https://blog.csdn.net/WantFlyDaCheng/article/details/100078777 自动代码 查询快捷键 其他快捷键 调试快捷键 重构 十大Inte ...