一、环境

1.1、Idea 2020.1

1.2、JDK 1.8

二、目的

通过properties文件配置spring boot 属性文件

三、步骤

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

在resources/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"
}

通过对比请求地址和返回结果,发现resources/config/application.properties中的配置内容已覆盖resources/application.properties

五、知识点

# 示例  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的更多相关文章

  1. 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 - ...

  2. 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 ...

  3. 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 -& ...

  4. 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 ...

  5. 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 ...

  6. 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 - ...

  7. spring cloud教程之使用spring boot创建一个应用

    <7天学会spring cloud>第一天,熟悉spring boot,并使用spring boot创建一个应用. Spring Boot是Spring团队推出的新框架,它所使用的核心技术 ...

  8. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...

  9. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

随机推荐

  1. Spring杂谈 | Spring中的AutowireCandidateResolver

    接口定义 用于推断一个特定的beanDefinition是否能作为指定依赖的候选者的策略接口 public interface AutowireCandidateResolver { // 默认情况下 ...

  2. 【带着canvas去流浪(15)】threejs fundamentals翻译系列1-scene graph

    示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在大前端>原创博文目录 华为云社区地址:[你要的前端打怪升级指南] 目录 ...

  3. 心路历程-安装Docker

    心路历程-安装Docker 本机环境 Windows10 激活HyperV功能 新建CentOS虚拟机 centos docker安装 由于是新的虚拟机,所以没有docker旧版本的问题,不需要卸载旧 ...

  4. python之邮件发送自动化

    # -*- coding:utf-8 -*-#@Time : 2020/3/24 22:55#@Autor: Mr.White#@File : 发送邮件.py 一.导入所需要的类 import smt ...

  5. xml(4)

    schema约束 dtd语法:<!ELEMENT 元素名称 约束> schema符合xml的语法,xml语句 一个xml中可以有多个schema,多个schema用名称空间区分(类似jav ...

  6. 世界这么大,Python 也想去看看

    把时间线拉回到 2015 年 4 月 13 日,一位河南省实验中学的心里老师在辞职信上写下了「世界那么大,我想去看看」这句话,后来爆红网络,我想这位心里老师当时写这句话的时候肯定没想到这句只有十个字的 ...

  7. Centos ps命令

    输出格式(ps -aux) USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND VSZ:占用的虚拟内存大小() RSS: COMMAND: 执 ...

  8. STM32编程:是时候深入理解栈了

    [导读] 从这篇文章开始,将会不定期更新关于嵌入式C语言编程相关的个人认为比较重要的知识点,或者踩过的坑. 为什么要深入理解栈?做C语言开发如果栈设置不合理或者使用不对,栈就会溢出,溢出就会遇到无法预 ...

  9. hdu4035 Maze 题解

    /* 设 E[i]表示在结点i处,要走出迷宫所要走的边数的期望. E[i] = ki*E[1] + (1-ki-ei)*E[fa[i]] + (1-ki-ei); E[i] = ki*E[1] + ( ...

  10. linux常用命令---文件软硬链接

    文件链接