一、环境

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. P2774 方格取数问题 网络流

    题目: P2774 方格取数问题 题目背景 none! 题目描述 在一个有 m*n 个方格的棋盘中,每个方格中有一个正整数.现要从方格中取数,使任意 2 个数所在方格没有公共边,且取出的数的总和最大. ...

  2. Qt和JS的交互

    参考文章:https://www.cnblogs.com/lgxZJ/archive/2017/12/31/8158132.html Qt和JavaScript的交互 Qt提供了对JS的良好支持,有两 ...

  3. C# 中 枚举Enum 一些转换的方法整理

    工作中 经常遇到枚举 的一些转换  特别是获取枚举备注等  特地整理下 方法以后使用 public void TestMethod1() { TestEnumOne colorEnum = TestE ...

  4. javaScript 添加和移除class类名的几种方法

    添加类属性: // 一次只能设置一个类值,如果当前属性本身存在类值,会被替换 element.className = '类名'; /* * .setAttribute 用来设置自定义属性和值的 * 自 ...

  5. Azkaban无法连接网页

    出的问题如下图 首先我查看日志看到有一个 [ERROR] 2020/03/13 11:12:34.417 +0800 ERROR [PluginCheckerAndActionsLoader] [Az ...

  6. X Error:BadDrawable (individ Pixmap or Window parameter 9)

    #描述 平台:aarch64 系统:ubuntu16.04.02 Qt Version:4.8.7 Qt程序可以正常运行,界面渲染出现问题以及乱码,控制提示错误内容: "unable to ...

  7. js 延迟加载的几种方法

    1. defer 属性HTML 4.01 为 <script>标签定义了 defer属性. 用途:表明脚本在执行时不会影响页面的构造.也就是说,脚本会被延迟到整个页面都解析完毕之后再执行. ...

  8. 网站设计时应考虑哪些因素,以保证网站是对SEO友好

    根据用户的搜索习惯做好栏目的设计 根据用户的习惯做好三大标签的设计 做好首页栏目的展现布局  对于用户来说的重点 展示栏目的合理化 多样化 细节化 代码的静态化 域名 服务器购买稳定 合格 网站内容的 ...

  9. 什么是virtual string tree?

    Virtual stringtree(以后简称VST)是一个提供源码的免费的第三方插件,支持DELPHI和C++builder,可在http://www.soft-gems.net/下载到最新的版本. ...

  10. phantomJS安装出错解决办法

    解决办法:https://github.com/xhlwill/blog/issues/11