本文来自网易云社区html
大部分比萨店也提供某种形式的自动配置。你能够点荤比萨、素比萨、香辣意大利比萨,或者是自动配置比萨中的极品——至尊比萨。在下单时,你并无指定具体的辅料,你所点的比萨种类决定了所用的辅料。但若是你想要至尊比萨上的所有辅料,还想要加墨西哥胡椒,又不想放蘑菇该怎么办?你偏心辣食又不喜欢吃菌类,自动配置不适合你的口味,你就只能本身配置比萨了吗?固然不是,大部分比萨店会让你以菜单上已有的选项为基础进行定制。java
为了使应用能适应不一样的环境,SpringBoot支持外化配置。可使用.properties文件、YAML文件、环境变量、命令行参数等方式。spring
SpringBoot能从多种属性源得到属性,包括如下几处:apache
命令行参数json
JVM系统属性安全
操做系统环境变量app
随机生成的带random.*前缀的属性(在设置其余属性时,能够引用它们,好比${random.long})dom
应用程序之外的application.properties或者application.yml文件spring-boot
打包在应用程序内的application.properites或者application.yml文件ui
经过@propertySource标注的属性源
默认属性
优先级由高到低,即在上面的列表中“命令行参数”的优先级最高,会覆盖下面其余属性源的相同配置。
这里只挑选了部分比较常见的属性源,详细信息能够参考官网教程:24. Externalized Configuration
默认状况下SpringApplication将任何可选的命令行参数(以'--'开头,好比,--server.port=9000)转化为property,并将其添加到Spring Environment中。如上所述,命令行属性老是优先于其余属性源。
若是不想用这个特性能够用SpringApplication.setAddCommandLineProperties(false)来禁用。
SpringBoot支持YAML格式的配置文件文件后缀为yml,是一种更易读写的通用的数据串行化格式。
在SpringBoot中.yml效果至关于.properties,一般状况下两者是能够互相替换的,好比下面2种配置文件在SpringBoot中是等效的:
application.properties
server.port=8020 server.address=127.0.0.1
application.yml
server: port: 8020 address: 127.0.0.1
.yml文件在配置数据的时候具备面向对象的特性,更易阅读。
虽然.yml配置和.properties基本等效,但也有略微区别,.yml配置不能用@propertySource注解加载。
SpringBoot会从如下位置加载.properties或.yml配置文件:
当前目录下的/config目录
当前目录
classpath下的/config目录
classpath的根目录
优先级也是从高到低,高优先级的配置文件会覆盖低优先级的配置。
使用.properties或.yml配置文件能够对SpringBoot的自动配置进行覆盖,好比在默认的状况下http端口为8080,咱们能够像上面的例子那样在修改成8020。
SpringBoot提供了上百个这样能够覆盖的配置,具体能够查阅官网或者查看autoconfiguration包的的META-INF/spring-configuration-metadata.json和META-INF/additional-spring-configuration-metadata.json文件(这2个文件用来给IDE作输入提示用,有一些简单的描述)。
有时候使用@Value("#{property}")注解注入配置会比较笨重,SpringBoot提供一个类型安全的方案,用强类型的Bean对象来替代属性。
@ConfigurationProperties用法以下
@ConfigurationProperties(prefix = "acme")public class AcmeProperties { private boolean enabled; private final Security security = new Security(); // 省略getter、setter public static class Security { private String username; private String password; private List<String> roles = new ArrayList<>(Collections.singleton("USER")); // 省略getter、setter } }
@ConfigurationProperties的参数prefix表示前缀,AcmeProperties里的每一个属性的名称即是具体的配置名。好比成员变量enabled绑定属性acme.enabled。
上面这个例子表示AcmeProperties对象分别绑定下列属性:
acme.ebalbe:布尔类型
acme.security.username:String类型
acme.security.pasword:String类型
acme.security.roles:类型是一个String集合
绑定配置以后还须要注册Spring上下文中,有3种方式:
在java配置类中用@EnableConfigurationProperties注解激活
@Configuration@EnableConfigurationProperties(AcmeProperties.class)public class MyConfiguration { }
直接在AcmeProperties加@Component注解
@Component@ConfigurationProperties(prefix="acme")public class AcmeProperties { // ... see the preceding example}
在配置类中与@Bean注解组合
@Beanpublic AnotherComponent anotherComponent() { ... }
注册以后,能够在任意地方使用@Autowire注解注入使用。
Spring Boot使用一些宽松的规则用于绑定Environment属性到@ConfigurationProperties beans,因此Environment属性名和bean属性名不须要精确匹配。常见的有虚线匹配大写(好比,context-path绑定到contextPath)和将环境属性转为大写字母(好比,PORT绑定port)。
好比:
@ConfigurationProperties(prefix="acme.my-project.person")public class OwnerProperties { private String firstName; // 省略getter、setter}
能够绑定到:
Property | Note |
---|---|
acme.my-project.person.firstName | 标准的驼峰式命名 |
acme.my-project.person.first-name |
虚线表示,适用于.properties和.yml |
acme.my-project.person.first_name |
下划线表示,适用于.properties和.yml |
ACME_MYPROJECT_PERSON_FIRSTNAME |
大写形式,适用于环境变量 |
Feature | @ConfigurationProperties |
@Value |
---|---|---|
Relaxed binding | Yes | No |
Meta-data support | Yes | No |
SpEL evaluation |
No | Yes |
SpringBoot默认提供了大量的自动配置,咱们能够经过启动时添加--debug参数来查看当前的配置信息。
激活的配置以下:
Positive matches:----------------- CodecsAutoConfiguration matched: - @ConditionalOnClass found required class 'org.springframework.http.codec.CodecConfigurer'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition) CodecsAutoConfiguration.JacksonCodecConfiguration matched: - @ConditionalOnClass found required class 'com.fasterxml.jackson.databind.ObjectMapper'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)...
未激活的配置以下:
Negative matches:----------------- ActiveMQAutoConfiguration: Did not match: - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition) AopAutoConfiguration: Did not match: - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice', 'org.aspectj.weaver.AnnotatedElement' (OnClassCondition) ...
能够清楚的看到每一个配置的条件,未激活的配置能够从这里直观的看到缘由。
debug能够用-Ddebug或--debug来启用,也能够在.properties或.yml文件中配置debug的值为true。
用@EnableAutoConfiguration注解的exclude参数去除指定的自动配置:
import org.springframework.boot.autoconfigure.*;import org.springframework.boot.autoconfigure.jdbc.*;import org.springframework.context.annotation.*;@Configuration@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})public class MyConfiguration { }
本节介绍了SpringBoot自定义配置的几种方式。经过属性外化,咱们能够灵活选择不一样的属性元来应对不一样的场景。利用YAML可让配置文件结构化更易于阅读,特别是有多层结构的属性。@ConfigurationProperties提供类型安全的属性使用方式,使用起来更加直观。组合使用YAML和@ConfigurationProperties相信能让项目的配置变得更加清晰。
SpringBoot不只仅是帮助咱们快速搭建一个可用的项目,在使用便利性上也提供了更多的姿式。
网易云新用户大礼包:https://www.163yun.com/gift
本文来自网易实践者社区,经做者金港生受权发布。