[02] SpringBoot的项目属性配置
1、application.properties 简述


2、配置文件的加载顺序
- 当前目录下的 /config 目录
- 当前目录
- classpath下的 /config 目录
- classpath 根目录
- /demo/config/application.properties
- /demo/application.properties
- /demo/springboot-demo.jar/BOOT-INF/classes/config/application.properties
- /demo/springboot-demo.jar/BOOT-INF/classes/application.properties
3、自定义属性与加载
server:
port: 8081
person:
name: zhangsan
age: 18
server:
port: 8081
person:
name: zhangsan
age: 18
@Component
public class Person {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@Component
public class Person {
@Value("${person.name}")
private String name;
@Value("${person.age}")
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

4、@ConfigurationProperties
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
5、参数间的引用
server:
port: 8081
person:
name: zhangsan
age: 18
zhangsan:
name: ${person.name}
age: ${person.age}
server:
port: 8081
person:
name: zhangsan
age: 18
zhangsan:
name: ${person.name}
age: ${person.age}
6、使用随机值
# 随机字符串
person.value=${random.value}
person.name=${random.string}
# uuid
person.name=${random.uuid}
# 随机int
person.age=${random.int}
# 随机long
person.height=${random.long}
# 10以内的随机数
preson.age=${random.int(100)}
# 10-20的随机数
person.name=${random.int[10,20]}
# 随机字符串
person.value=${random.value}
person.name=${random.string}
# uuid
person.name=${random.uuid}
# 随机int
person.age=${random.int}
# 随机long
person.height=${random.long}
# 10以内的随机数
preson.age=${random.int(100)}
# 10-20的随机数
person.name=${random.int[10,20]}
7、Environment
@RestController
@RequestMapping("/demo/")
public class DemoController {
@Autowired
private Environment environment;
@RequestMapping("/printPerson.do")
public String printPerson() {
String personName = environment.getProperty("person.name");
return personName;
}
}
@RestController
@RequestMapping("/demo/")
public class DemoController {
@Autowired
private Environment environment;
@RequestMapping("/printPerson.do")
public String printPerson() {
String personName = environment.getProperty("person.name");
return personName;
}
}
@RestController
@RequestMapping("/demo/")
public class DemoController {
@Autowired
private Person person;
@Autowired
private Environment environment;
@RequestMapping("/printPerson.do")
public String printPerson() {
String nameFromClass = person.getName();
String nameFromEnv = environment.getProperty("person.name");
System.out.println("class: " + nameFromClass);
System.out.println("env: " + nameFromEnv);
return nameFromEnv;
}
}
@RestController
@RequestMapping("/demo/")
public class DemoController {
@Autowired
private Person person;
@Autowired
private Environment environment;
@RequestMapping("/printPerson.do")
public String printPerson() {
String nameFromClass = person.getName();
String nameFromEnv = environment.getProperty("person.name");
System.out.println("class: " + nameFromClass);
System.out.println("env: " + nameFromEnv);
return nameFromEnv;
}
}
class: 80c628c7948c4270f831956945a0f26e
env: d8ff3f804268a929d365c663ade6894c
class: 80c628c7948c4270f831956945a0f26e
env: 9351315473098298f8416dae16bd1acb
class: 80c628c7948c4270f831956945a0f26e
env: ef78a4148b049c3fc25a71bddb560715
class: 80c628c7948c4270f831956945a0f26e
env: d8ff3f804268a929d365c663ade6894c
class: 80c628c7948c4270f831956945a0f26e
env: 9351315473098298f8416dae16bd1acb
class: 80c628c7948c4270f831956945a0f26e
env: ef78a4148b049c3fc25a71bddb560715

8、多环境配置 application-{profile}
- application-dev.yml
- application-test.yml
- application-prod.yml

java -jar xxx.jar --spring.profiles.active=prod
java -jar xxx.jar --spring.profiles.active=prod
9、加载多个自定义配置文件

@Component
@Configuration
@PropertySource("classpath:config/person.properties")
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@Component
@Configuration
@PropertySource("classpath:config/person.properties")
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
10、打包jar时需要注意的自定义配置文件打包
<build>
<!-- 打包资源文件 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
<build>
<!-- 打包资源文件 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
</build>
11、@Configuration 和 @Bean
- @Configuration 标注在类上,相当于spring的xml配置文件中的 <beans>,作用为配置spring容器(应用上下文)
- @Bean 标注在方法上(返回某个实例的方法),等价于spring的xml配置文件中的 <bean>,作用为注册bean对象
12、参考链接
[02] SpringBoot的项目属性配置的更多相关文章
- SpringBoot 入门:项目属性配置
开发一个SpringBoot 项目,首当其冲,必然是配置项目 一.项目属性配置 1. SpringBoot自带了Tomcat服务器,通过使用项目配置文件来修改项目的配置,如图配置了部署在80端口,目录 ...
- Spring Boot项目属性配置
接着上面的入门教程,我们来学习下Spring Boot的项目属性配置. 1.配置项目内置属性 属性配置主要是在application.properties文件里配置的(编写时有自动提示)这里我们将se ...
- VS项目属性配置实验过程
(原创,转载注明出处:http://www.cnblogs.com/binxindoudou/p/4017975.html ) 一.实验背景 cocos2d-x已经发展的相对完善了,从项目的创建.编译 ...
- VS IDE 中Visual C++ 中的项目属性配置
VS IDE 中Visual C++ 中的项目属性配置 一. Visual C++ 项目系统基于 MSBuild. 虽然可以直接在命令行上编辑 XML 项目文件和属性表,我们仍建议你使用 VS IDE ...
- springboot快速入门(二)——项目属性配置(日志详解)
一.概述 application.properties就是springboot的属性配置文件 在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring ...
- SpringBoot项目属性配置-第二章
SpringBoot入门 1. 相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了 ...
- SpringBoot总结之属性配置
一.SpringBoot简介 SpringBoot是spring团队提供的全新框架,主要目的是抛弃传统Spring应用繁琐的配置,该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配 ...
- 转载:VS项目属性配置总结
本文来自:http://www.mamicode.com/info-detail-232474.html https://www.cnblogs.com/alinh/p/8066820.h ...
- VS项目属性配置总结
以下是针对VS2013下的VC++项目: Debug和Release说明: Debug 通常称为调试版本,它包含调试信息,并且不作任何优化,便于程序员调试程序.Release 称为发布版本,它往往是进 ...
随机推荐
- 洛谷P3987 我永远喜欢珂朵莉~(set 树状数组)
题意 题目链接 Sol 不会卡常,自愧不如.下面的代码只有66分.我实在懒得手写平衡树了.. 思路比较直观:拿个set维护每个数出现的位置,再写个线段树维护区间和 #include<bits/s ...
- 使用volley上传多张图片,一个参数对应多张图片,转载
https://my.oschina.net/u/1177694/blog/491834 原帖地址 而如果使用volley的话,因为请求数据那些都很简便,但遇到上传文件就麻烦那可不好,同时使用多个网络 ...
- puppeteer入门
转自: https://www.jianshu.com/p/a89d8d6c007b 作者: ppCode puppeteer新手入门(chromium下载跳坑) ppCode 关注 2017.12. ...
- python第六十八天--第十二周作业
主题: 需求: 用户角色,讲师\学员, 用户登陆后根据角色不同,能做的事情不同,分别如下讲师视图 管理班级,可创建班级,根据学员qq号把学员加入班级 可创建指定班级的上课纪录,注意一节上课纪录对应多条 ...
- 关于解决JetBrains 激活问题
今天升级了下JetBrains 的一系列产品,安装后打开发现以前的激活失效了. 在网上看到网友们各类屏蔽和寻找服务器,尝试了下都不太方便. 最后去rover的个人博客看了下,很给力,总能跟上JetBr ...
- 【17】有关python面向对象编程的提高【多继承、多态、类属性、动态添加与限制添加属性与方法、@property】
一.多继承 案例1:小孩继承自爸爸,妈妈.在程序入口模块再创建实例调用执行 #father模块 class Father(object): def __init__(self,money): self ...
- 17秋 软件工程 第六次作业 Beta冲刺 Scrum5
17秋 软件工程 第六次作业 Beta冲刺 Scrum5 各个成员冲刺期间完成的任务 世强:完成APP端相册.部员管理.手势签到模块: 陈翔:完成Scrum博客.总结博客,完成超级管理员前后端对接: ...
- JS中=>,>>>是什么意思
最近经常看到 JS中=>,符号,于是查了一下别人的博客 =>是es6语法中的arrow function 举例:(x) => x + 6 相当于 function(x){ ret ...
- cronolog分割tomcat日志文件
tomcat日志备份磁盘压力解决方案,使用cronolog每日生成文件uat部署操作如下,,观察两天,下周一部署生产message系统,后续根据需求部署到其它业务线 示例方案:uat—message0 ...
- go标准库的学习-mime/multipart
参考:https://studygolang.com/pkgdoc 导入方式: import "mime/multipart" multipart实现了MIME的multipart ...