美文网首页
Java Springboot 配置文件说明

Java Springboot 配置文件说明

作者: MicoCube | 来源:发表于2019-03-19 20:10 被阅读0次
  • application.yml 文件会被优先加载,
    而如果同时存在 application.properties 文件,并且存在相同的配置,
    那么则会用 application.properties 文件中的配置覆盖之前的配置;
    也就是说哪个文件被最后加载,哪个才具有最高级别
  • 官方推荐Using YAML Instead of Properties,但是YAML files cannot be loaded by using the @PropertySource annotation.
    So, in the case that you need to load values that way, you need to use a properties file
  • Spring Boot尝试在@ConfigurationProperties使用Spring的@Validated注释注释时验证类。您可以javax.validation
    直接在配置类上使用JSR-303 约束注释。为此,请确保符合条件的JSR-303实现位于类路径上,然后将约束注释添加到字段中
@ConfigurationProperties(prefix="acme")
@Validated
public class AcmeProperties {

    @NotNull
    private InetAddress remoteAddress;

    // ... getters and setters

}

You can also trigger validation by annotating the @Bean method that creates the configuration properties with @Validated.

  • springboot的一些默认属性可以看下default.en.properties

  • springboot + profile(不同环境读取不同配置)

    • 不同环境的配置设置一个配置文件,例如:dev环境下的配置配置在application-dev.properties中;prod环境下的配置配置在application-prod.properties中。
      在application.properties中指定使用哪一个文件,比如spring.profiles.active=dev,指定dev环境
    • 建议
      • 各个环境公共的配置写在application.properties中
      • 各个模块独有的配置配置在自己的application-{xxx}.properties文件中
      • 程序读取的时候优先读取application.properties中选中的profile的配置,若读不到才会从application.properties去读
  • 获取配置文件值及springboot的默认配置值

相关文章

网友评论

      本文标题:Java Springboot 配置文件说明

      本文链接:https://www.haomeiwen.com/subject/xitomqtx.html