SpringBoot project Obtaining value from the configuration file

Although Spring-boot outset of the project a number of parameters automatically configured, such as port and other servers, if we want to modify these parameters, then you should add these parameters in the configuration file, and then obtain the values ​​of these parameters from the project, the following that is, how to obtain the value of the operation from the configuration file.

SpringBoot global configuration file name is fixed, there are two

  • application.properties
  • application.yml

The role of these two documents is to modify the default value SpringBoot automatically configured,

application.yml that I manually added, there is no time to quickly create a project of this document

Join To modify the server's port, so you can add

If these parameters in the configuration file

person.last-name=张三
person.age=20
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

 

So how do you get these parameters from the project configuration file it?

There are two ways, @ CofigurationProperties bulk acquisition, @ Value is a single acquisition

@Value acquisition value and compare the value acquired @Con fi gurationProperties

                                                     @ConfigurationProperties                                                              @Value
功能                                              批量注入配置文件中的属性                                                           一个个指定
松散绑定(松散语法)                 支持                                                                                               不支持
SpEL                                            不支持                                                                                            支持
JSR303数据校验                          支持                                                                                               不支持
复杂类型封装                                支持                                                                                               不支持

如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value;

如果说,我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@Configurationproperties

除了全局配置文件外,我们还可以自己增加配置文件,然后使用命令行的方式即通过spring.config.location来改变默认的配置文件位置

java -jar xxx.jar  --spring.config.location=G:/application.properties(这里是我们增加配置文件的位置)

 

 

发布了19 篇原创文章 · 获赞 0 · 访问量 1979

Guess you like

Origin blog.csdn.net/jiankangzhu/article/details/104246708