本文共 4213 字,大约阅读时间需要 14 分钟。
在《》中,我们已经创建好 一个springboot 的项目。在创建的时候,他出现了一个application.properties 文件,这就是我们的核心配置文件。
springboot的核心配置文件主要用来配置springboot程序。他有两种形式:
server.port=8080 #配置Tomcat端口号server.servlet.context-path=/01-springboot-web # 配置请求上下文#旧版本中为server.context-path=/01-springboot-web此时启动项目后,我们需要写如下网址才能访问:
1、yml 是一种yaml格式的配置文件,主要采用一定的空格,换行等格式进行配置
2、值与前面的冒号配置项必须有一个空格 3、yml 后缀也可以使用yaml后缀server: port: 9090 servlet: context-path: /02-springboot-web删除application.properties后,启动,访问如下;
在我们开发过程中,我们用于开发,测试,和发布的配置文件内容能不同,比如端口。那么我们该如何配置内。这时候就用到了spring.profiles.active
比如我们现在有开发环境和测试环境 开发文件中application-dev.propertiesserver.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启动了。
如果我们在application.properties中自定义了一些配置项,比如:
为了读取内容,我们写一个controllerpackage 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重新启动后,即可显示正常了:
我们在配置文件中,自定义的配置项为:
#自定义配置项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/