SpringBoot 配置文件

application.properties / application.yaml

the priority of properties is higher than yaml

yaml style
server:
  port: 8080


properties style
server.port=8080

xml style
<server>
    <port>8080</port>
</server>

yaml

yaml语法

实体类中属性的注入
@Value("Dog")
private String name;
@Value("3")
private int age;


yaml属性值注入
step1: 在application.yaml 配置实体类的对象属性
    user:
      name: user1
      age: 1
      happy: false
      birth: 2000/10/5
      maps: {k1: v1,k2: v2} 
      lists:
        - hobby1
        - hobby2
      dog:
        name: dog3
        age: 3
step2: 在实体类上添加注解@ConfigurationProperties(prefix = "user")
    @ConfigurationProperties(prefix = "user") 将application.yaml中对象的属性值注入    到实体类中
step3:使用时 
     @Autowired
     private User user;

特点:

  • 大小写敏感
  • 使用缩进表示层级关系

特定配置文件

@PropertySource("classpath:specific.properties") //指定配置文件
specific.properties与application.properties同级目录
    在属性上使用 注解 @Value("${name1}") 进行注入 //name1是specific.properties的键值对

@Validated

@Validated: @Validated下的注解 判断实体类中的属性是否符合数据格式,不符合会报错
    //组合注解 JSR-303
    @Length(min = 6, max = 19, message = "用户名长度是6-18位")
    @ApiModelProperty(value = "用户名", required = true)
    private String username;
    @ApiModelProperty(value = "密码", required = true)
    private String password;
    //不能是空的
    @NotEmpty
    @ApiModelProperty(value = "用户头像")
    private String icon;
    @Email(message = "邮箱格式错误")
    @ApiModelProperty(value = "邮箱")
    private String email;
    @NotNull
    @ApiModelProperty(value = "用户昵称")
    private String nickName;
    @ApiModelProperty(value = "备注") 
    private String note;
    ————————————————
    版权声明:本文为CSDN博主「不知所终,不知所起」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/weixin_44440642/article/details/106335653
    

配置文件加载位置

  1. file:./config/ equal springboot-02-config/config/application.yaml priority4 high
  2. file:./ equal springboot-02-config/application.yaml priority3
  3. classpath:/config/ equal resources/config/application.yaml priority2
  4. classpath:/ equal resources/application.yaml priority1 default
多种配置文件 为了不同阶段的开发
    命名格式:application-xxx.yaml
    配置文件的切换:在application.yaml中 用 spring.profiles.active=xxx 进行切换

多种文档模块
    application.yaml
        server:
          port: 8080
        ---
        server:
          port: 8081
          
        spring:
          profiles: dev
        ---

application.properties与spring.factories的联系

HttpEncodingAutoConfiguration 已经在 spring.factories 注册
例子:HttpEncodingAutoConfiguration

@Configuration(proxyBeanMethods = false) //表示配置类
@EnableConfigurationProperties(ServerProperties.class) //自动配置属性 
    ServerProperties.class
        @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) //ServerProperties.class 上的注解
        private Integer port; //ServerProperties some attribute
        private InetAddress address;
        private ForwardHeadersStrategy forwardHeadersStrategy;
        
        application.properties style
            server.port
            server.address
            server.forwardHeadersStrategy
//ConditionalOn 判断当前配置是否生效
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "server.servlet.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {

在spring.factories中配置的属性,都会有对应的xxxProperties.class
xxxProperties.class 由 yyyAutoConfiguration.class 装配且使用xxxProperties.class的默认值
可以通过application.properties改变默认值

application.properties

#可以理解 那些配置的生效
debug = true 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值