[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 称为发布版本,它往往是进 ...
随机推荐
- for each....in、for in、for of
一.一般的遍历数组的方法: var array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i) { console.log(i,a ...
- 关于z-index使用方法
z-index控制的是元素的层叠顺序,当z-index越大此层越靠上:但是z-index需在已给元素定位(定位方式不限)的前提下否则该属性失效!! jquery获取index值的方法: $(" ...
- Wim镜像编辑
1.挂载install.wim文件为本地的一个文件夹 dism /mount-wim /wimfile:D:\install.wim /index:1 /mountdir:D:\Win 注:1> ...
- 洗礼灵魂,修炼python(38)--面向对象编程(8)—从算术运算符进一步认识魔法方法
上一篇文章了解了魔法方法,相信你已经归魔法方法至少有个概念了,那么今天就进一步的认识魔法方法.说这个之前,大脑里先回忆一下算术操作符. 什么是算术操作符?忘记没有?忘记了的自己倒回去看我前面的博文或者 ...
- 纯CSS选项卡
html: <!doctype html> <html> <head> <meta charset="utf-8"> <tit ...
- 【PAT】B1044 火星数字(20 分)
/* 火星文有两位,第二位为0不输出 */ #include<stdio.h> #include<algorithm> #include<string.h> #in ...
- Hp电脑开机报错:no boot disk has been detected or the disk has failed
hp主机开机报错no boot disk has been detected or the disk has failed,重启之后没有作用,开机之后仍然是同样界面.考虑是硬盘问题,按ESC+F10 ...
- iOS-单选cell的实现
一.思路 先设置一个chooseCelltag标记(类型为NSIndexPath),然后在点击cell触发的时候,如果tag设置有值,就设置 UITableViewCell *selectedCell ...
- 描述各自页面的 page
一个小程序页面由四个文件组成(注意:为了方便开发者减少配置项,描述页面的四个文件必须具有相同的路径与文件名).分别是: 页面 Page(JS文件) Page(Object) 函数用来注册一个页面.接受 ...
- WPF之托盘图标的设定
首先需要在项目中引用System.Windows.Forms,System.Drawing; using System; using System.Collections.Generic; using ...