博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【springboot】springboot核心配置文件
阅读量:2241 次
发布时间:2019-05-09

本文共 4213 字,大约阅读时间需要 14 分钟。

文章目录

在《》中,我们已经创建好 一个springboot 的项目。在创建的时候,他出现了一个application.properties 文件,这就是我们的核心配置文件。

springboot的核心配置文件主要用来配置springboot程序。他有两种形式:

  • application.properties :键值对的properties属性文件配置方式
  • application.yml

举例:application.properties方式

server.port=8080   #配置Tomcat端口号server.servlet.context-path=/01-springboot-web # 配置请求上下文#旧版本中为server.context-path=/01-springboot-web

在这里插入图片描述

此时启动项目后,我们需要写如下网址才能访问:
在这里插入图片描述

举例:application.yml方式

1、yml 是一种yaml格式的配置文件,主要采用一定的空格,换行等格式进行配置

2、值与前面的冒号配置项必须有一个空格
3、yml 后缀也可以使用yaml后缀

server:  port: 9090  servlet:    context-path: /02-springboot-web

在这里插入图片描述

删除application.properties后,启动,访问如下;
在这里插入图片描述

多环境配置文件

在我们开发过程中,我们用于开发,测试,和发布的配置文件内容能不同,比如端口。那么我们该如何配置内。这时候就用到了spring.profiles.active

比如我们现在有开发环境和测试环境
开发文件中application-dev.properties

server.port=8090server.servlet.context-path=/01-springboot-web#旧版本中为server.context-path=/01-springboot-web

在这里插入图片描述

在测试环境中application-test.properties(与application-dev.properties的区别在于端口)

server.port=8099server.servlet.context-path=/01-springboot-web#旧版本中为server.context-path=/01-springboot-web

在这里插入图片描述

现在如果想使用application-test.properties文件中的配置,需要在application.properties 文件中如下操作
在这里插入图片描述
这样写,就是以端口8099启动了。

读取自定义配置文件

1 -用@Value注解读取,用于逐个读取自定义的配置

如果我们在application.properties中自定义了一些配置项,比如:

在这里插入图片描述
为了读取内容,我们写一个controller

package com.cjp.springboot.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class ConfigInfoController {
// 1。用@Value注解读取,用于逐个读取自定义的配置 @Value("${boot.name}") private String name; @Value("${boot.location}") private String location; @RequestMapping("/boot/config") public @ResponseBody String Config(){
return name+ "_______"+location; } }

启动后访问网址:

在这里插入图片描述
这里出现了乱码,我们需要配置一下:
在这里插入图片描述
在application.properties文件中添加配置项:

spring.http.encoding.charset=utf-8spring.http.encoding.enabled=truespring.http.encoding.force=true

在这里插入图片描述

重新启动后,即可显示正常了:
在这里插入图片描述

2-用ConfigurationProperties 将这个文件映射为一个对象

我们在配置文件中,自定义的配置项为:

#自定义配置项boot.name=mybootboot.location=河北

我们首先创建一个包config 并在包内创建一个类ConfigInfo.java

在这里插入图片描述
代码如下;

package com.cjp.springboot.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;//ConfigurationProperties 注解,并且用prefix配置前缀,因为我们在配置文件中的名字为boot.name,为此前缀为boot@Component@ConfigurationProperties(prefix = "boot")public class ConfigInfo {
private String name; private String location;// 属性的名字必须和配置文件的自定义的名字一样,这样才能映射 public String getName() {
return name; } public void setName(String name) {
this.name = name; } public String getLocation() {
return location; } public void setLocation(String location) {
this.location = location; }}

完成ConfigInfoController文件:

package com.cjp.springboot.controller;import com.cjp.springboot.config.ConfigInfo;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class ConfigInfoController {
// 1。用@Value注解读取,用于逐个读取自定义的配置 @Value("${boot.name}") private String name; @Value("${boot.location}") private String location;// 2/用ConfigurationProperties注解的方式 @Autowired private ConfigInfo configInfo; @RequestMapping("/boot/config") public @ResponseBody String Config(){
return name+ "_______"+location + "=====" +configInfo.getName()+"------"+configInfo.getLocation(); }}

启动运行:

在这里插入图片描述

总结

1、application.yml和application.properties中都是固定的名字,不能改动

2、如果两个文件同时存在,则会优先使用application.properties,application.yml无效,为此,要想使用application.yml需要删除application.properties
3、在多环境配置中,如果application.properties和application-test.properties中有相同的配置项,则以application-test.properties为准。
4、如果application.properties有server.servlet.context-path等配置项,而application-test.properties等文件中没有此配置,application.properties中的server.servlet.context-path配置项才会生效

转载地址:http://uahbb.baihongyu.com/

你可能感兴趣的文章
进程和线程的概念、区别和联系
查看>>
CMake 入门实战
查看>>
绑定CPU逻辑核心的利器——taskset
查看>>
Linux下perf性能测试火焰图只显示函数地址不显示函数名的问题
查看>>
c结构体、c++结构体和c++类的区别以及错误纠正
查看>>
Linux下查看根目录各文件内存占用情况
查看>>
A星算法详解(个人认为最详细,最通俗易懂的一个版本)
查看>>
利用栈实现DFS
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>