SpringBoot配置属性的规定
- 通过.拆散各个元素
- 最初一个.将前缀与属性名称离开
- 必须是字母(az)和数字(0-9)
- 必须是小写字母
- 用连字符-来分隔单词
- 惟一容许的其余字符是[和],用于List的索引
- 不能以数字结尾
雷同的配置项
Spring Boot 2.x 加载配置文件的时候会移除特殊字符并且还会将配置均用全小写的形式进行匹配和加载。
application.properties
com.rumenz.id-name=rumenzcom.rumenz.id_name=rumenzcom.rumenz.idName=rumenzcom.rumenz.idname=rumenz
测试
package com.rumenz.lession8.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @className: RumenzController * @description: TODO 类形容 * @author: 入门小站 rumenz.com * @date: 2021/11/8 **/@Controller@RequestMapping("/rumenz")public class RumenzController { @Value("${com.rumenz.idname}") private String comRumenzIdName; @RequestMapping("/index") @ResponseBody public String index(){ return comRumenzIdName; }}
浏览器拜访
http://127.0.0.1:8080/rumenz/index
,返回rumenz
以上4个配置项是等价的,举荐全小写两头用
-
宰割。
配置文件的优先级
application.properties和application.yml文件能够在放在以下几个地位。
- 内部:利用程序运行目录的
config
子目录 - 内部:利用程序运行目录的跟目录
- 外部:在config包外面
classpath:/config/
- 外部:在classpath根目录
classpath:/
门路 | 参数值 |
---|---|
/springboot-zhinan/lession8/config/application.properties | com.rumenz.level=1 |
/springboot-zhinan/lession8/application.properties | com.rumenz.level=2 |
/springboot-zhinan/lession8/src/main/resources/config/application.properties | com.rumenz.level=3 |
/springboot-zhinan/lession8/src/main/resources/application.properties | com.rumenz.level=4 |
application.properties
中配置了com.rumenz.level
com.rumenz.level=1
测试
package com.rumenz.lession8.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @className: RumenzController * @description: TODO 类形容 * @author: 入门小站 rumenz.com * @date: 2021/11/8 **/@Controller@RequestMapping("/rumenz")public class RumenzController { @Value("${com.rumenz.idname}") private String comRumenzIdName; @Value("${com.rumenz.level}") private String comRumenzLevel; @RequestMapping("/index") @ResponseBody public String index(){ return comRumenzIdName; } @RequestMapping("/index1") @ResponseBody public String index1(){ return comRumenzLevel; }}
应用mvn clean package
打包后,应用jar -jar lession8-0.0.1-SNAPSHOT.jar
运行。如果上述4个中央都定义了application.properties
,并且都配置了com.rumenz.level
,当咱们的业务外面须要用com.rumenz.level
配置项。查找的优先级是:
- 1.先找运行目录下的config的
application.properties
中的com.rumenz.level
,找到返回,否则下一步 - 2.再找运行目录下的
application.properties
中的com.rumenz.level
,找到返回否则下一步 - 3.再找classpath下的config目录的
application.properties
的com.rumenz.level
,找到返回否则下一步 - 4.再找classpath下的
application.properties
的com.rumenz.level
,找到就返回,找不到就报错,我的项目启动失败。
命令行参数配置
springboot的application.properties
中能够配置一些参数,如端口号,账号,明码。如果咱们想在运行的时候想长期批改运行的端口也是能够的。
java -jar lession8-0.0.1-SNAPSHOT.jar --server.port=9000
这样我的项目运行的端口就变成了9000
,--server.port=9000
相当于在application.properties
中配置server.port=9000
零碎环境变量
咱们也能够在操作系统外面定义变量,而后通过@Value
注入到Spring容器中。
package com.rumenz.lession8.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @className: RumenzController * @description: TODO 类形容 * @author: 入门小站 rumenz.com * @date: 2021/11/8 **/@Controller@RequestMapping("/rumenz")public class RumenzController { //读取零碎环境变量 @Value("${PATH}") private String path; @RequestMapping("/index2") @ResponseBody public String index2(){ return path; } }
浏览器拜访http://127.0.0.1:8080/rumenz/index2
,返回/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/usr/local/share/dotnet:~/.dotnet/tools:/usr/local/mysql/bin:/usr/local/mysql/bin
零碎属性
咱们能够设置VM arguments
向Spring容器传递参数值。
Idea中设置VM arguments
测试
package com.rumenz.lession8.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * @className: RumenzController * @description: TODO 类形容 * @author: 入门小站 rumenz.com * @date: 2021/11/8 **/@Controller@RequestMapping("/rumenz")public class RumenzController { @Value("${spring.test.env}") private String springTestEnv; @RequestMapping("/index3") @ResponseBody public String index3(){ return springTestEnv; }}
浏览器拜访http://127.0.0.1:8080/rumenz/index3
,返回test
jar包运行设置VM arguments
java -jar -Dspring.test.env=testvm lession8-0.0.1-SNAPSHOT.jar
浏览器拜访
http://127.0.0.1:8080/rumenz/index3
,返回testvm
本小结源码地址:
- GitHub:https://github.com/mifunc/spr...
- Gitee:https://gitee.com/rumenz/spri...
- https://rumenz.com/rumenbiji/...
- 我的博客 https://rumenz.com/ ,
- 我的工具箱 https://tooltt.com/
- 微信公众号:【入门小站】
- 关注【入门小站】回复【1001】获取 linux常用命令速查手册
- 关注【入门小站】回复【1003】获取 LeetCode题解【java语言实现】
- 关注【入门小站】回复【1004】获取 Java根底外围总结
- 关注【入门小站】回复【1009】获取 阿里巴巴Java开发手册