一、首先创建一个springboot的空项目(创建过程不再赘述,一个标准maven项目),创建完的结构如下所示,直接贴图:
MyApplication.java文件的内容如下:
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
application.properties文件内容如下:
# 端口
server.port=8080
server.context-path=/springboot-yml-properties
pom.xml文件内容如下:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>springboot-yml-properties</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-yml-properties Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- springboot 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- springboot 项目热加载支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- springboot web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<finalName>springboot-yml-properties</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
到这里项目创建结束;
二、接下来简单说下properties和yml文件的语法区别:
首先启动项目,从 一 中我们看到已经通过properties文件将项目端口配置为 8080,故我们在启动项目后可以看到如下log信息:
2017-12-05 15:52:35.952 INFO 6760 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-05 15:52:35.959 INFO 6760 --- [ restartedMain] com.test.MyApplication : Started MyApplication in 3.871 seconds (JVM running for 4.331)
接下来我们终止刚刚启动的项目,删除项目中的application.properties文件,在同一路径下建立application.yml文件,并配置如下内容:
server:
port: 8087
context-path: /springboot-yml-properties
我们发现并没有配置什么新东西,和刚刚用application.properties配置文件配置的内容完全一样,但是语法不一样(个人认为这样更优雅,因为这样一个结构就不需要在每一行开头都写上server. 这样的前缀),此时启动项目,我们在控制台可以看到如下信息:
2017-12-05 15:59:08.289 INFO 244 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8087 (http)
2017-12-05 15:59:08.299 INFO 244 --- [ restartedMain] com.test.MyApplication : Started MyApplication in 4.013 seconds (JVM running for 4.501)
看到这里,我想您应该已经明白,刚刚的yml配置起到了和properties文件一样的效果;
第二部分只是想要说明使用yml配置文件会比properties配置文件在语法上会更加优雅些许(主要还看个人习惯吧),接下来我会分别使用yml和properties进行多环境下的配置说明以及参数获取方法(springboot项目),这才是本篇我想说的重点,算是一个总结学习吧,首先是yml文件部分;
yml文件部分 start
三、
01、首先我们都知道一个常识,那就是每个人都有自己的年龄,比如我们现在的业务需求是查询所有年龄大于20的人的相关信息,如果我们选择通过配置文件来配置这个值为20的常量的话,我们该如何配置和如何从配置文件中获取这个值呢?直接贴代码,application.yml的内容如下(注意 “age:“ 和 “20“ 之间需要一个空格,yml的语法 ):
server:
port: 8087
context-path: /springboot-yml-properties
#年龄
age: 20
package com.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${age}")
private Integer age;
@RequestMapping("/test")
public String test() {
return "年龄:" + age;
}
}
接下来我们启动项目,访问http://localhost:8087/springboot-yml-properties/test,可看到页面上会显示:
年龄:20
到这里说明,我们仅仅通过一个注解
@Value("${age}")
就在项目启动时拿到了20这个年龄值;
02、同时我们随着业务的扩展,可能我们不只是要查询年龄大于20的人的相关信息,我们还需要有更多的标准,比如年龄大于20且姓王的人相关信息,这么这个姓 王 如果我们选择在配置文件配置的话该如何配置和在程序中读取呢,我想看到这里您已经知道了,直接贴代码如下:
server:
port: 8087
context-path: /springboot-yml-properties
#年龄
age: 20
#姓
lastName: 王
TestController.java文件内容修改为:
package com.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${age}")
private Integer age;
@Value("${lastName}")
private String lastName;
@RequestMapping("/test")
public String test() {
return "年龄:" + age + ", 姓:" + lastName;
}
}
启动项目,访问http://localhost:8087/springboot-yml-properties/test,可看到页面上会显示:
年龄:20, 姓:王这一步并没有什么特别之处,你可以看到我们用了同样的方法拿到了相关参数;
03、这一步大家可以不看,直接看04,一个yml配置文件的灵活使用方法,基于我们打算打印 年龄:20, 姓:王 这样一个需求:
修改配置文件如下:
server:
port: 8087
context-path: /springboot-yml-properties
#年龄
age: 20
#姓
lastName: 王
content: "age: ${age}, lastName: ${lastName}"
TestController.java文件内容修改为:
package com.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Value("${age}")
private Integer age;
@Value("${lastName}")
private String lastName;
@Value("${content}")
private String content;
@RequestMapping("/test")
public String test() {
return content;
}
}
如您所想,访问
http://localhost:8087/springboot-yml-properties/test,可看到页面上会显示:
age: 20, lastName: 王
这也就是在配置文件中使用当前的配置;
04、到这里大家可能意识到一个问题,那就是现在我们的查询条件有年龄,姓两个相关常量,我们就需要定义两个变量并加上相应的@Value注解来为其注入相应的值,那么以后又有了身高,性别等这样的参数那怎么办?难道要继续在TestController.java文件中声明成员变量并不断的加注解
@Value
来注入吗?答案当然是:No!
首先修改配置文件内容如下:
server:
port: 8087
context-path: /springboot-yml-properties
#人
person:
age: 20
lastName: 王
date: 2001-03-27
创建一个java文件PersonPropertiesInfo.java,代码如下:
package com.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class PersonPropertiesInfo {
private Integer age;
private String lastName;
private String date;
//set get...忽略,一定要有set方法
}
TestController.java文件内容修改为:
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private PersonPropertiesInfo personPropertiesInfo;
@RequestMapping("/test")
public String test() {
return "年龄:" + personPropertiesInfo.getAge() +
" 姓:" + personPropertiesInfo.getLastName() +
" 出生日期:" + personPropertiesInfo.getDate();
}
}
访问
http://localhost:8087/springboot-yml-properties/test,可看到页面上会显示:
年龄:20 姓:王 出生日期:2001-03-27
到这里我们可以看到 我们不再需要在每个属性上加@Value注解,只需加一个前缀即可,很方便;
但是这里需要为每个成员生成set get方法,因此我在这里提供另一种方式(了解即可,个人不推荐使用),那就是每个成员变量上继续加@Value注解,您看到这里可能会说如果这样的话前面何必说那么多呢,我只能说这样的唯一好处就是不需要set方法,只需要get方法供您获取其值即可:
此种状态下,PersonPropertiesInfo.java文件内容如下:
package com.test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PersonPropertiesInfo {
@Value("${person.age}")
private Integer age;
@Value("${person.lastName}")
private String lastName;
@Value("${person.date}")
private String date;
public Integer getAge() {
return age;
}
public String getLastName() {
return lastName;
}
public String getDate() {
return date;
}
}
您可以看到没有set方法,但每个成员变量都需要加注解;
四、使用多个配置文件实现多环境情况下的配置(开发环境,生产环境)
首先创建两个新的配置文件,一个名叫application-dev.yml,内容如下:
person:
age: 21
lastName: 王
date: 2001-03-27
另一个名为application-product.yml,内容如下:
person:
age: 22
lastName: 王
date: 2001-03-27
您可以看到两个配置文件的年龄不同,接下来修改application.yml内容如下:
server:
port: 8087
context-path: /springboot-yml-properties
spring:
profiles:
active: dev
此时启动项目,访问http://localhost:8087/springboot-yml-properties/test 您可以看到 年龄:21 姓:王 出生日期:2001-03-27,年龄为21
再修改application.yml内容如下:
server:
port: 8087
context-path: /springboot-yml-properties
spring:
profiles:
active: product
访问http://localhost:8087/springboot-yml-properties/test 您可以看到 年龄:22 姓:王 出生日期:2001-03-27,年龄为22
五、使用一个配置文件实现多环境情况下的配置(开发环境,生产环境)
可能有的小伙伴会有这样的需求,那就是我只想使用一个配置文件,觉得还需要创建多个配置文件太麻烦了,那怎么办?恭喜您,yml配置文件完全可以满足您的需求,且只需要三个英文状态下的短横线即可;
首先我们修改application.yml文件内容如下:
server:
port: 8087
context-path: /springboot-yml-properties
spring:
profiles:
active: dev
---
spring:
profiles: dev
person:
age: 21
lastName: 王
date: 2001-03-27
---
spring:
profiles: product
person:
age: 22
lastName: 王
date: 2001-03-27
然后我们删除application-product.yml和application-dev.yml这两个文件,接下来我们启动项目访问http://localhost:8087/springboot-yml-properties/test,我们会在浏览器看到
年龄:21 姓:王 出生日期:2001-03-27
(注意年龄为21);接下来我们修改 application.yml的内容如下(只是把active:dev修改为active:product):
server:
port: 8087
context-path: /springboot-yml-properties
spring:
profiles:
active: product
---
spring:
profiles: dev
person:
age: 21
lastName: 王
date: 2001-03-27
---
spring:
profiles: product
person:
age: 22
lastName: 王
date: 2001-03-27
启动项目,访问http://localhost:8087/springboot-yml-properties/test,页面显示
年龄:22 姓:王 出生日期:2001-03-27(注意年龄为22);
yml文件部分 end
properties文件部分start
六、多环境下使用properties文件配置
01、首先删除项目中的yml文件,在src/main/resources路径下建以下三个文件:
application.properties:
# 端口
server.port=8081
server.context-path=/springboot-yml-properties
spring.profiles.active=dev
application-dev.properties:
person.age=20
person.lastName=zhang
person.date=2017-04-18
application-product.properties:
person.age=22
person.lastName=wang
person.date=2017-03-16
02、
PersonPropertiesInfo.java文件的内容为:
package com.test;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "person")
public class PersonPropertiesInfo {
private Integer age;
private String lastName;
private String date;
set get...方法忽略
}
TestController.java内容为:
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private PersonPropertiesInfo personPropertiesInfo;
@RequestMapping("/test")
public String test() {
return "年龄:" + personPropertiesInfo.getAge() +
" 姓:" + personPropertiesInfo.getLastName() +
" 出生日期:" + personPropertiesInfo.getDate();
}
}
03、
此时启动项目,访问http://localhost:8081/springboot-yml-properties/test会看到浏览器上显示 年龄:20 姓:zhang 出生日期:2017-04-18
04、修改application.properties文件内容如下:
# 端口
server.port=8081
server.context-path=/springboot-yml-properties
spring.profiles.active=product
此时再访问http://localhost:8081/springboot-yml-properties/test会看到浏览器上显示
年龄:22 姓:wang 出生日期:2017-03-16
properties文件部分end
看到这里我想您已经懂了如何根据自己的需求灵活的选择配置文件和进行配置,that's all,多谢各位看官,仅仅是本人一个学习总结~
的的
的