6小时快速入门Java微服务架构Spring Boot

发布时间 2023-08-02 08:58:30作者: 饿死的冏猫

springboot 快速入门

配置文件

例如修改tomcat启动端口号:

application.properties:

server.port = 8080
<!-- 注意yml文件 数据值前面必须有空格 -->
application.yml:

server:
  port: 8080

配置文件优先级:

.properties > .yml > .yaml

YAML: 基本语法

  • 大小写敏感

  • 数据值前边必须有空格,作为分隔符

  • 使用缩要进表示层级关系

  • 缩进不允许使用tab键,只允许使用空格

  • 缩进的空格数不重要,只要形同元素的元素左侧对其即可

  • # 表示注释,从这个字符一直到行尾,都会被解析器忽略

YAML: 数据格式

对象(map): 键值对的集合

person:
  name: zhangsan

# 行内写法
person: {name: zhangsan}

数组: 一组按次序排列的值

address:
  - beijing
  - shanghai
# 行内写法
address: [beijing.shanghai]

纯量: 单个的、不可再分的值

msg1: 'hello \n world' # 单引忽略转义文字
msg2: "hello \n world" # 双引识别转义字符

YAML: 参数引用

name:lisi
person: 
  name: ${name} # 引用上边定义的name值

读取配置文件内容

@value

Environment

@ConfigurationProperties
@value("${ xxx }")
private 数据类型 变量名;

@Autowired
private Environment env;
System.out.println(env.getProperty("xxx"))

@ConfigurationProperties

定义一个类

提供get和set方法

@Component // 被pring识别,是一个bean

@ConfigurationProperties(prefix = "xxx")

profile

功能: 一套程序可能安装到不同环境,比如:开发、生产、测试等

profile: 进行动态配置切换

  1. profile配置方式

    • 多profile文件方式

    • yml多文档方式

  2. profile激活方式

    • 配置文件

    • 虚拟机参数

    • 命令行参数