SpringBoot虽然去掉了 XML 但未做到零配置,它体现出了一种约定优于配置,是一种软件设计范式,旨在减少软件开发人员做决定的数量,而又不失灵活性。一般情况下默认的配置足够满足日常开发所需,但在特殊的情况下,我们往往需要用到自定义属性配置、自定义文件配置、多环境配置 等一系列功能。
1. 自定义属性配置
1.1. application.properties
配置文件
my.age=23
my.name=wzp
1.2. 定义Properties.java文件,用来映射我们在 application.properties 中的内容,我们就可以通过操作对象的方式来获得配置文件的内容
package com.wzp.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "application")
public class Properties {
private int age;
private String name;
// 省略 get set 方法
@Override
public String toString() {
return "Properties{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
1.3. 定义
PropertiesController
用来注入Properties
测试我们编写的代码,Spring4.x
以后,推荐使用构造函数的形式注入属性…
package com.wzp.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController (这个注解在上一篇已经说过了,就不再解释啦)
@RequestMapping("/properties")
public class PropertiesController {
private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);
private final Properties properties;
@Autowired
public PropertiesController(Properties properties) {
this.properties = properties;
}
@GetMapping("/propertie")
public Properties myProperties1() {
log.info("========================================");
log.info(properties.toString());
log.info("========================================");
return properties;
}
}
1.4. 打开浏览器,输入如下地址:http://localhost:8080/properties/propertie,观察控制台,如果出现如下内容,那么恭喜你——成功啦!!!
2019-01-08 10:25:57.215 INFO 8264 --- [nio-8080-exec-3] com.wzp.demo.PropertiesController : ========================================
2019-01-08 10:25:57.215 INFO 8264 --- [nio-8080-exec-3] com.wzp.demo.PropertiesController : Properties{age=0, name='null'}
2019-01-08 10:25:57.215 INFO 8264 --- [nio-8080-exec-3] com.wzp.demo.PropertiesController : ========================================
2. 配置文件的其它命名
2.1 将application.properties改为application.yml文件,则内容书写格式如下
my:
name: wzp
age: 23
2.2. 读取配置文件的相关信息也可在controller里操作
package com.wzp.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/properties")
@RestController
public class PropertiesController {
private static final Logger log = LoggerFactory.getLogger(PropertiesController.class);
@Value("${my.name}")
private String name;
@Value("${my.age}")
private int age;
@RequestMapping(value = "/propertie")
public String propertie(){
return name+":"+age;
}
}
2.3. 打开浏览器,访问 http://localhost:8080/properties/propertie 出现如下结果 则成功读取配置文件信息
3. 多环境化配置
3.1. 在真实的应用中,常常会有多个环境(如:开发,测试,生产等),不同的环境数据库连接都不一样,这个时候就需要用到
spring.profile.active
的强大功能了,它的格式为application-{profile}.properties
,这里的application
为前缀不能改,{profile}
是我们自己定义的。分别刻意创建以下多种配置满足不同的需求
3.1.1. application-dev.properties 开发环境
server.servlet.context-path=/dev
3.1.2. application-prod.properties 生产环境
server.servlet.context-path=/prod
3.1.3. application-test.properties 测试环境
server.servlet.context-path=/test
3.2. 在 application.properties 配置文件中写入
spring.profiles.active=dev
,这个时候我们在次访问 http://localhost:8080/properties/propertie 就没用了,新的路径就是 http://localhost:8080/dev/properties/propertie 读取的配置文件也是 application-dev.properties 里面的内容
4. 结语
嗯...看了很多大佬的教程,结合我自己的总结了一波,当然了,不足之处请多包涵,也请多指教...如有雷同,也请多包涵...嘻嘻...
注:如有需要,可自行转载,但是要加上原创作者及原文章链接哦...