更多Spring文章,欢迎点击 一灰灰Blog-Spring专题java
SpringBoot极大的减小了配置,开一个新项目时,彻底能够作到什么配置都不加,就能够直接跑,简单方便的同时,就带来了一个问题git
<!-- more -->github
首先建立一个SpringBoot项目,这一块就直接省略掉,下面直奔主题,如何获取配置spring
默认读取配置文件 application.properties
或者 application.yml
中的配置信息,两种不一样的文件类型,对应的内部配置方式也不太同样json
配置文件位置app
通常来讲,默认的配置文件application.properties
或者application.yml
文件放在目录dom
src/main/resources/
properties格式spring-boot
properties配置文件属于比较常见的一种了,定义也比较简单,形如 key=value
,一个实例以下学习
#服务端口号 server.port=8081 app.proper.key=${random.uuid} app.proper.id=${random.int} app.proper.value=test123 app.demo.val=autoInject
yml格式测试
yml格式的配置文件是以缩进来表示分层,kv之间用冒号来分割,形如
#服务端口号 server: port: 8081 app: proper: key: ${random.uuid} id: ${random.int} value: test123 demo: val: autoInject
格式对比
两种不一样格式的配置文件,有啥区别?
单纯从使用来说,并无特别的不一样,并且我我的也一直这么认为的,直到遇到了一个诡异的问题,后面给出
程序启动以后,如何获取配置文件application.yml
中的配置信息呢?在实际的使用中,最多见的有三种姿式
全部的配置信息,都会加载到Environment实体中,所以咱们能够经过这个对象来获取系统的配置,经过这种方式不只能够获取application.yml
配置信息,还能够获取更多的系统信息
使用姿式以下:
@RestController public class DemoController { @Autowired private Environment environment; @GetMapping(path = "show") public String show() { Map<String, String> result = new HashMap<>(4); result.put("env", environment.getProperty("server.port")); return JSON.toJSONString(result); } }
@Value
注解能够将配置信息注入到Bean的属性,也是比较常见的使用方式,但有几点须要额外注意
使用方式以下,主要是经过 ${}
,大括号内为配置的Key;若是配置不存在时,给一个默认值时,能够用冒号分割,后面为具体的值
@RestController public class DemoController { // 配置必须存在,且获取的是配置名为 app.demo.val 的配置信息 @Value("${app.demo.val}") private String autoInject; // 配置app.demo.not不存在时,不抛异常,给一个默认值data @Value("${app.demo.not:dada}") private String notExists; @GetMapping(path = "show") public String show() { Map<String, String> result = new HashMap<>(4); result.put("autoInject", autoInject); result.put("not", notExists); return JSON.toJSONString(result); } }
上面的两种方式对于某几个特别的配置来讲,一个一个的写还好,若是配置特别多时,每个都去这么玩,估计会敲的键盘原地爆炸了,固然这么不友好的事情,怎么能忍!所以就有了下面这种使用方式
@Data @Component @ConfigurationProperties(prefix = "app.proper") public class ProperBean { private String key; private Integer id; private String value; }
上面的写法,含义是将配置文件中配置 app.proper.key
, app.proper.id
, app.proper.value
三个配置的值,赋值给上面的bean
ConfigurationProperties
来制定配置的前缀上面这个过程,配置的注入,从有限的经验来看,多半是反射来实现的,因此这个Bean属性的Getter/Setter方法得加一下,上面借助了Lombok来实现,标一个@Component
表示这是个Bean,托付给Spring的ApplicationConttext来管理
配置文件application.properties
信息以下
#服务端口号 server.port=8081 app.proper.key=${random.uuid} app.proper.id=${random.int} app.proper.value=test123 app.demo.val=autoInject user.name=一灰灰Blog
写一个DemoController来返回读取的配置值
@RestController public class DemoController { @Autowired private Environment environment; @Autowired private ProperBean properBean; @Value("${app.demo.val}") private String autoInject; @Value("${app.demo.not:dada}") private String notExists; @Value("${user.name}") private String name; @GetMapping(path = "show") public String show() { Map<String, String> result = new HashMap<>(6); result.put("properBean", properBean.toString()); result.put("autoInject", autoInject); result.put("env", environment.getProperty("server.port")); result.put("not", notExists); result.put("name", name); return JSON.toJSONString(result); } }
访问后输出以下
{ "autoInject": "autoInject", "name": "user", "not": "dada", "env": "8081", "properBean": "ProperBean(key=d4f49141-fa67-4e4c-9e23-c495ff02fda7, id=132483528, value=test123)" }
请注意上面的not
和 name
返回
notExists
对应的配置信息,在配置文件中没有定义,因此返回默认的dataname
对应的配置信息 user.name
在application.properties
文件中是一灰灰Blog
,可是返回了user(测试环境为mac,mac系统的用户名为user,为啥叫user?由于某某人...)
前面主要介绍了常见的三种获取配置信息的方式,但遗留了几个问题
xxx.properties
的配置信息(能读取么?)一灰灰的我的博客,记录全部学习和工做中的博文,欢迎你们前去逛逛
尽信书则不如,已上内容,纯属一家之言,因我的能力有限,不免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激
一灰灰blog
知识星球