【SpringBoot教程三】:配置文件和多环境配置
来源:互联网 发布:树莓派网络配置 编辑:程序博客网 时间:2024/05/22 04:44
配置文件和多环境配置
配置文件的种类:
配置文件的生效顺序,会对值进行覆盖:
1. @TestPropertySource 注解
2. 命令行参数
3. Java系统属性(System.getProperties())
4. 操作系统环境变量
5. 只有在random.*里包含的属性会产生一个RandomValuePropertySource
6. 在打包的jar外的应用程序配置文件(application.properties,包含YML和profile变量)
7. 在打包的jar内的应用程序配置文件(application.properties,包含YML和profile变量)
8. 在@Configuration类上的@PropertySource注解
9. 默认属性(使用SpringApplication.setDefaultProperties指定)
Application属性文件,按优先级排序,位置高的将覆盖位置低的
1. 当前目录下的一个/config子目录
2. 当前目录
3. 一个classpath下的/config包
4. classpath根路径(root)
这个列表是按优先级排序的(列表中位置高的将覆盖位置低的)
DEMO
lyw.name=LYWlyw.age=22lyw.desc=${lyw.name} is a boy#时间格式化spring.jackson.date-format=yyyy-MM-dd HH:mm:ss#时区设置spring.jackson.time-zone=Asia/Chongqing在src/main/resources/config/创建 application.propertis文件
lyw.name=LYWlyw.age=22lyw.desc=${lyw.name} is a cool boy
这里 lyw.name 是报黄线的装有 spring插件的宝贝门 可以 直接去掉黄线
会创建一个元数据additional-spring-configuration-metadata.json
当然 这个可以忽略的
项目呢就是这样的
补上Controller的代码:
package com.lyw.controller;import java.util.Date;import java.util.HashMap;import java.util.Map;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.fasterxml.jackson.core.sym.Name;@RestController@RequestMapping("/user")public class UserController {//加载配置文件中的参数@Value(value="${lyw.name}")private String name;@Value(value="${lyw.age}")private int age;@Value(value="${lyw.desc}")private String desc;@RequestMappingpublic Map<String,Object> index(){Map<String,Object> map = new HashMap<>();map.put("Name", name);map.put("Age", age);map.put("Desc", desc);map.put("Date", new Date());return map;}}可以看到 我只是简单的返回了一下 map
运行的结果:
上面 结果可以看到。。。
这里 日期格式 已经通过
#时间格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#时区设置
spring.jackson.time-zone=Asia/Chongqing
spring.profiles.active=testlyw.name=LYWlyw.age=22lyw.desc=${lyw.name} is a boy#时间格式化spring.jackson.date-format=yyyy-MM-dd HH:mm:ss#时区设置spring.jackson.time-zone=Asia/Chongqing这里 上面加了 一行 是读取对应文件的
server.port=8083创建 application-dev.propertis
server.port=8088
1. Properties配置多环境,需要添加多个配置文件,YML只需要一个配件文件
2.书写格式的差异,yaml相对比较简洁,优雅
3. YML的缺点:不能通过@PropertySource注解加载。如果需要使用@PropertySource注解的方式加载值,那就要使用properties文件。
spring: profiles: active: dev jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: Asia/Chongqinglyw: name: LYW age: 18 desc: ${lyw.name} is a very good Boy! ---spring: profiles: dev server: port: 8088 ---spring: profiles: test server: port: 8081 ---spring: profiles: pro server: port: 8080
- 【SpringBoot教程三】:配置文件和多环境配置
- SpringBoot ——配置文件多环境配置
- springboot系列教程(三)——配置方式、多环境配置
- 第三节:SpringBoot使用properties配置文件实现多环境配置
- SpringBoot 使用properties配置文件实现多环境配置
- springboot多环境配置
- springboot(三)配置文件
- SpringBoot 基础知识学习(二)——配置文件多环境配置
- SpringBoot配置多套环境
- SpringBoot中通过配置文件控制路径和变量(三)
- 《Springboot极简教程》SpringBoot配置文件PropertySourcesPlaceholderConfigurer
- SpringBoot(三):SpringBoot 属性配置文件详解
- SpringBoot学习(三)--Spring profile多环境方式实现logback日志配置
- springboot 配合 profile 多环境自动切换配置文件
- spring boot 中配置文件的读取和多环境配置
- springboot maven 配置多环境部署
- SpringBoot属性配置实战(多环境)
- SpringBoot配置文件以及配置项
- codefoces 722C - Destroying Array
- java中private,public,protected的区别
- 配对游戏
- html中如何使用bootstrap
- 欢迎使用CSDN-markdown编辑器
- 【SpringBoot教程三】:配置文件和多环境配置
- 读书与工作
- Session入Memcache缓存
- Android动画分类
- okttp
- Android 获取系统顶部状态栏高度和底部导航栏的高度
- An SSL error has occurred and a secure connection to the server cannot be made.
- 帝国分类目录官网
- CISCO 接入公网配置