当前位置: 首页 > news >正文

SpringBoot入门篇2 - 配置文件格式、多环境开发、配置文件分类

目录

1.配置文件格式(3种)

例:修改服务器端口。(3种)

src/main/resources/application.properties

server.port=80

 src/main/resources/application.yml(主要用这种)

server:port: 80

  src/main/resources/application.yaml

server:port: 80

SpringBoot配置文件加载优先级:/application.properties > application.yml > application.yaml

2.yaml数据格式

yaml,一种数据序列化格式。

优点:容易阅读、以数据为中心,重数据轻格式。

yam文件扩展名:.yml(主流)、.yaml 

语法规则:
大小写敏感
属性值前面添加空格。(空格和冒号要隔开)
# 表示注释

数组格式:

enterprise:name: abcage: 16tel: 111111subject:- Java- C- C++

3.yaml数据读取方式(3种)

application.yml

lesson: SpringBootserver:port: 80enterprise:name: abcage: 16tel: 111111subject:- Java- C- C++

controller/BookController.java 有下面三种写法

① @Value(直接读取)

package com.example.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/books")
public class BookController {@Value("${lesson}")private String lesson;@Value("${server.port}")private Integer port;@Value("${enterprise.subject[0]}")private String subject_0;@GetMapping("/{id}")public String getById(@PathVariable Integer id) {System.out.println(lesson);System.out.println(port);System.out.println(subject_0);return "hello, spring boot!";}
}

② Environmet(封装后读取)

package com.example.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/books")
public class BookController {@Autowiredprivate Environment environment;@GetMapping("/{id}")public String getById(@PathVariable Integer id) {System.out.println(environment.getProperty("lesson"));System.out.println(environment.getProperty("server.port"));System.out.println(environment.getProperty("enterprise.age"));System.out.println(environment.getProperty("enterprise.subject[1]"));return "hello, spring boot!";}
}

③ 实体类封装属性(封装后读取)

package com.example.controller;import com.example.domain.Enterprise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/books")
public class BookController {@Autowiredprivate Enterprise enterprise;@GetMapping("/{id}")public String getById(@PathVariable Integer id) {System.out.println(enterprise);return "hello, spring boot!";}
}

需要额外封装一个类domain/enterprise.java

package com.example.domain;import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;import java.util.Arrays;@Component
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {private String name;private Integer age;private String tel;private String[] subject;public void setName(String name) {this.name = name;}public void setAge(Integer age) {this.age = age;}public void setTel(String tel) {this.tel = tel;}public void setSubject(String[] subject) {this.subject = subject;}@Overridepublic String toString() {return "Enterprise{" +"name='" + name + '\'' +", age=" + age +", tel='" + tel + '\'' +", subject=" + Arrays.toString(subject) +'}';}
}

pom.xml也需要额外添加一个依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional>
</dependency>

4.多环境开发配置

resources/application.xml

spring:profiles:active: pro---
# 开发
spring:config:activate:on-profile: dev
server:port: 80---
# 生产
spring:config:activate:on-profile: pro
server:port: 81---
# 测试
spring:config:activate:on-profile: test
server:port: 82

5.使用命令行启动多环境

java -jar xxx.jar --spring.profiles.active=test --server.port=88 

参数加载的优先顺序可以从官网获得:Core Features

6.Maven与SpringBoot关联操作

在开发中,关于环境配置,应该以Maven为主,SpringBoot为辅。

①Maven中设置多环境属性

pom.xml 

  <profiles><profile><id>dev</id><properties><profile.active>dev</profile.active></properties></profile><profile><id>pro</id><properties><profile.active>pro</profile.active></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><profile.active>test</profile.active></properties></profile></profiles>

使用插件对资源文件开启对默认占位符的解析 

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.3.1</version><configuration><encoding>UTF-8</encoding><useDefaultDelimiters>true</useDefaultDelimiters></configuration>
</plugin>

②SpringBoot引入Maven属性

application.yml 

spring:profiles:active: ${profile.active}---
# 开发
spring:config:activate:on-profile: dev
server:port: 80---
# 生产
spring:config:activate:on-profile: pro
server:port: 81---
# 测试
spring:config:activate:on-profile: test
server:port: 82

③Maven打包,进行测试


7.配置文件分类

http://www.lryc.cn/news/142014.html

相关文章:

  • UOS安装6.1.11内核或4.19内核
  • HiveSQL刷题
  • path路径模块
  • 1.文章复现《热电联产系统在区域综合能源系统中的定容选址研究》(附matlab程序)
  • 【Terraform学习】使用 Terraform 托管 S3 静态网站(Terraform-AWS最佳实战学习)
  • 触发JVM fatal error并配置相关JVM参数
  • 爬虫(bilibili热门课程记录)
  • 14-模型 - 增删改查
  • C#与西门子PLC1500的ModbusTcp服务器通信3--搭建ModbusTcp服务器
  • Linux系统编程:线程控制
  • 基于Java+SpringBoot+Vue前后端分离纺织品企业财务管理系统设计和实现
  • 搭建开发环境-Windows
  • 【 Python 全栈开发 - 人工智能篇 - 45 】集成算法与聚类算法
  • SSM商城项目实战:账户充值功能实现
  • wireshark工具pcap文件转换
  • Python+TinyPNG熊猫网站自动化的压缩图片
  • 【Linux】socket 编程基础
  • openGauss学习笔记-51 openGauss 高级特性-列存储
  • ReactNative 密码生成器实战
  • 开始MySQL之路——外键关联和多表联合查询详细概述
  • 无涯教程-PHP - intval() 函数
  • 2023年国赛数学建模思路 - 案例:粒子群算法
  • 【1++的数据结构】之map与set(一)
  • Ubuntu断电重启后黑屏左上角光标闪烁,分辨率低解决办法,ubuntu系统display只有4:3 怎么办?太卡
  • Java 微服务当中POST form 、url、json的区别
  • repo 常用命令汇总——202308
  • [Linux]命令行参数和进程优先级
  • Android13新特性之通知权限提升
  • 206. 反转链表 (简单系列)
  • 攻防世界-Fakebook