一、SpringBoot全局配置文件

SpringBoot两个全局配置文件,application.properties和application.yml

例如端口号配置

(1)application.properties文件

(2)application.yml文件格式

二、yaml文件格式语法

1.大小写敏感

2.属性: 值 之间必须有空格

3.值如果是字符串 默认不用加引号。加单引号转义字符功能失效原样输出,加双引号转义字符有转义功能,

4.对象

(1)

(2)或者

5.

三、调用yaml

1.pom.xml添加

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>

2.

package com.example.demo.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age; public String getName() {
return name;
}
public void setName(String username) {
this.name = username;
}
}

单元测试

package com.example.demo;

import com.example.demo.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests { @Autowired //
Person person; @Test
public void contextLoads() {
System.out.println(person.getName());
}
}

测试文件运行

 四、application.properties配置

书写方式

运行同理

注意如果是有中文

properties文件 需要转成ascii

六、用value注解方式单个读取配置文件

七、 加载非全局配置文件

创建一个

@Component
@ConfigurationProperties(prefix = "person")
@PropertySource(value = {"classpath:person.properties"})
public class Person {
private String name; public String getName() {
return name;
}
public void setName(String username) {
this.name = username;
}
}

八、导入Spring 配置文件

九、占位符

十、激活指定profile

快速切换开发、测试、生产的配置环境

1. properties的激活

一个开发环境,一个生产环境的配置文件

在application.properties 中激活开发环境的配置

2.yml的激活

3.命令行方式激活

4.虚拟机参数

十一、配置顺序

1.

2.外部加载配置

(2)SpringBoot 配置的更多相关文章

  1. SpringBoot配置属性之Server

    SpringBoot配置属性系列 SpringBoot配置属性之MVC SpringBoot配置属性之Server SpringBoot配置属性之DataSource SpringBoot配置属性之N ...

  2. SpringBoot基础系列-SpringBoot配置

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...

  3. springboot上传文件 & 不配置虚拟路径访问服务器图片 & springboot配置日期的格式化方式 & Springboot配置日期转换器

    1.    Springboot上传文件 springboot的文件上传不用配置拦截器,其上传方法与SpringMVC一样 @RequestMapping("/uploadPicture&q ...

  4. springboot配置Druid数据源

    springboot配置druid数据源 Author:SimpleWu springboot整合篇 前言 对于数据访问层,无论是Sql还是NoSql,SpringBoot默认采用整合SpringDa ...

  5. springboot配置详解

    springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...

  6. SpringBoot 配置 Servlet、Filter、Listener

    SpringBoot 配置 Servlet.Filter.Listener 在SpringBoot应用中,嵌入式的 Servlet 3.0+ 容器不会直接使用 ServletContainerInit ...

  7. SpringBoot 配置静态资源映射

    SpringBoot 配置静态资源映射 (嵌入式servlet容器)先决知识 request.getSession().getServletContext().getRealPath("/& ...

  8. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  9. springboot配置cxf

    1.引入两个需要的jar <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf- ...

  10. SpringBoot配置(2) slf4j&logback

    SpringBoot配置(2) slf4j&logback 一.SpringBoot的日志使用 全局常规设置(格式.路径.级别) SpringBoot能自动适配所有的日志,而且底层使用slf4 ...

随机推荐

  1. 安徽师大附中%你赛day9 T2 富 解题报告

    富 题目背景 出于某些原因, 苟先生在追杀富先生. 题目描述 富先生所在的地方是一个\(n\times m\)的网格,苟先生排出了他的狼狗大军,共有\(k\)条狗,第\(i\)条狗所在的位置为\((x ...

  2. Codeforces Round #525 (Div. 2)A. Ehab and another construction problem

    A. Ehab and another construction problem 题目链接:https://codeforc.es/contest/1088/problem/A 题意: 给出一个x,找 ...

  3. centos的网络设置问题

    遭遇了多次centos的网络连接问题,现将正确配置总结下: 这里是使用vmware虚拟平台,因为涉及到中间这层,所以需要设置下: 保证centos也能连上网,首先物理机连上网,接着物理机的vmware ...

  4. tomcat内存配置及配置参数详解

    1.jvm内存管理机制: 1)堆(Heap)和非堆(Non-heap)内存 按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟 ...

  5. 在线输入RGB更改背景色

    HTML: <!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" co ...

  6. c++ fstream用法(2)

    一> #include "stdafx.h" #include<iostream> #include<string> #include<fstr ...

  7. 群联MPALL(Rel) 7F V5.03.0A-DL07量产工具 PS2251-07(PS2307)

    前言:U盘被写保护,真的很醉人啊~~      群联MPALL是一款群联PS2251系列主控量产修复工具,本版本支持PS2251-67.PS2251-68.PS2251-02.PS2251-03.PS ...

  8. 五分钟搞懂Vuex

    这段时间一直在用vue写项目,vuex在项目中也会依葫芦画瓢使用,但是总有一种朦朦胧胧的感觉.于是决定彻底搞懂它. 看了一下午的官方文档,以及资料,才发现vuex so easy! 作为一个圈子中的人 ...

  9. 编写一个 Chrome 浏览器扩展程序

    浏览器扩展允许我们编写程序来实现对浏览器元素(书签.导航等)以及对网页元素的交互, 甚至从 web 服务器获取数据,以 Chrome 浏览器扩展为例,扩展文件包括: 一个manifest文件(主文件, ...

  10. DOM 2

    1.对文档的信息进行检索常用的方法: getElementById; getElementsByTagName; getAttribute;//得到的是属性值 2把需要的信息添加到DOM中常用的方法: ...