Spring boot Sample 003之spring-boot-configuration-properties
一、环境
1.1、Idea 2020.1
1.2、JDK 1.8
二、目的
三、步骤
3.1、点击File -> New Project -> Spring Initializer,点击next

3.2、在对应地方修改自己的项目信息

3.3、选择Web依赖,选中Spring Web。可以选择Spring Boot版本,本次默认为2.2.6,点击Next

3.4、编辑工程名和项目路径,确定后点击Finish完成

3.5、项目结构

四、添加测试方法

4.1、新建UserController实体类
package org.ouyushan.springboot.configuration.properties.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import java.util.HashMap; /**
* @Description:
* @Author: ouyushan
* @Email: ouyushan@hotmail.com
* @Date: 2020/4/27 13:34
*/ @RestController
@RequestMapping("/api/user")
public class UserController { @Value(value = "${ouyushan.secret}")
private String secret; @Value(value = "${ouyushan.number}")
private int number; @Value(value = "${ouyushan.desc}")
private String desc; @GetMapping("/")
public String hello() {
return "Hello World Based On Spring-Boot";
} // @RequestParam 简单类型的绑定
@GetMapping("/getUser")
public HashMap<String, Object> getUser(@RequestParam String username) {
HashMap<String, Object> map = new HashMap<>();
map.put("title", "hello world");
map.put("username", username);
map.put("secret",secret);
map.put("number",number);
map.put("desc",desc);
return map;
}
}
4.2、配置默认application.properties
ouyushan.secret=${random.value}
ouyushan.number=${random.int}
ouyushan.name=ouyushan
ouyushan.desc=${ouyushan.name} is a domain name
server.port=9090
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Chongqing
{
"number":-1860712823,
"secret":"e1066512836d41276cbd3baf86189ac8",
"title":"hello world",
"username":"spring",
"desc":"ouyushan is a domain name"
}
4.3、配置resources/config/application.properties
ouyushan.secret=${random.value}
ouyushan.number=${random.int}
ouyushan.name=ouyushan-config
ouyushan.desc=${ouyushan.name} is a domain name
server.port=7070
{
"number":-1540619073,
"secret":"352c725a9695f463f4edbcbd6dee1944",
"title":"hello world",
"username":"spring",
"desc":"ouyushan-config is a domain name"
}
五、知识点
# 示例 spring-boot-configuration-properties 外部化配置 properties文件优先级 1. 当前目录下的一个/config子目录
2. 当前目录
3. 一个classpath下的/config包
4. classpath根路径(root) 可通过@Value取值 @Value(value = "${ouyushan.secret}")
private String secret; **@ConfigurationProperties(prefix = "sample")** 读取外部配置文件中sample 开始的属性,自动映射到类中的字段,并覆盖代码中的值。
Spring boot Sample 003之spring-boot-configuration-properties的更多相关文章
- Spring boot Sample 004之spring-boot-configuration-yaml
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 通过yaml文件配置spring boot 属性文件 三.步骤 3.1.点击File -> New Project - ...
- Spring boot Sample 009之spring-boot-web-thymeleaf
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合thymeleaf模板开发web项目 三.步骤 3.1.点击File -> New Pro ...
- Spring boot Sample 012之spring-boot-web-upload
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合web实现文件上传下载 三.步骤 3.1.点击File -> New Project -& ...
- Spring boot Sample 0010之spring-boot-web-freemarker
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 spring boot 整合freemarker模板开发web项目 三.步骤 3.1.点击File -> New Pr ...
- Spring boot Sample 006之spring-boot-custom-servlet
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.步骤 2.1.点击File -> New Project -> Spring Initializer,点击next 2 ...
- Spring boot Sample 005之spring-boot-profile
一.环境 1.1.Idea 2020.1 1.2.JDK 1.8 二.目的 通过yaml文件配置spring boot 属性文件 三.步骤 3.1.点击File -> New Project - ...
- spring cloud教程之使用spring boot创建一个应用
<7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
随机推荐
- Eugene and an array(边界麻烦的模拟)
一道看似小学生的题,搞了我几个小时...... 首先思路就有两种: \(Ⅰ.找和为0的bad子串,再用n*(n+1)/2-bad子串得到答案\) \(Ⅱ.找和不为0的good子串\) 如果你选择找ba ...
- CC2530应用——按键控制灯光状态变化
独立新建工程并编写.编译代码,实现按键控制灯光闪烁状态的变换,实现以下任务要求:[1]程序开始运行:D4灯闪烁,D3.D5.D6灯熄灭.[2]按下模块上的SW1按键松开后,实现D5.D6灯轮流闪烁.[ ...
- Mybatis-入门演示
MyBatis:持久层框架 前言 之前有看过和学习一些mybatis的文章和内容,但是没有去写过文章记录下,现在借鉴b站的狂神视频和官方文档看来重新撸一遍入门.有错误请多指教. 内容 数据访问层-相当 ...
- vue项目-打印页面中指定区域的内容(亲测有效!)
关于打印整个页面的,没什么好说的.今天我给大家分享一个打印指定区域的方法,你想打印哪里,就打印哪里! 我也是刚刚开始接触打印这一块功能的,然后当然是找度娘深入了解了一番啦,期间试了网上的各种方法,有的 ...
- 多线程测试时的辅助类--CountDownLatch
多线程时,很多时候由于mian线程与多线程结束时间不可控,造成无法测试 辅助测试类---CountDownLatch 我看的视频教程匿名内部类无法使用外部的变量,所以CountDownLatch定义为 ...
- [hihoCoder1231 2015BeijingOnline]求圆与多边形公共部分的周长
题意:如题 思路:离散.将所有交点求出来,相当于将多变形的边切成了很多条元边,对每条元边,有两种情况 在圆内,答案加上此边长 在圆外,答案加上此边相对于圆心的"有向转弧" #inc ...
- linux-Curl error (37): Couldn't read a file:// file for file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64 [Couldn't open file /e tc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64]
在安装Python3-pip 的时候遇到 [root@localhost rpm-gpg]# yum install python3-pipFedora 31 - x86_64 - Updates - ...
- docker 使用 Nvidia 显卡
docker19.03读取NVIDIA显卡 作者: 张首富 时间: 2019-09-06 w x: y18163201 前言 2019年7月的docker 19.03已经正式发布了,这次发布对我来说有 ...
- 如何发布自己用python写的py模块
Python——怎么发布你的Python模块 我们在学习Python的时候,除了用pip安装一些模块之外,有时候会从网站下载安装包下来安装,我也想要把我自己编写的模块做成这样的安装包,该怎么办,如何发 ...
- React:redux+router4搭建应用骨架
可能是短期内关于react的对后一篇笔记.假设读者对redux和router4有基本了解. 缘由: 现在网上很多关于react+redux的文章都是沿用传统的文件组织形式,即: |--componen ...