0
点赞
收藏
分享

微信扫一扫

第五章 SpringBoot.2.X⾥⾯的配置⽂件

大沈投资笔记 2022-01-23 阅读 32

1.SpringBoot2.x常⻅配置⽂件形式

  • 常⻅的配置⽂件格式
    在这里插入图片描述
  • Springboot⾥⾯常⽤xx.yml
    YAML(Yet Another Markup Language)
    写 YAML 要⽐写 XML 快得多(⽆需关注标签或引号) 使⽤空格 Space 缩进表示分层,不同层次之间的缩进可以使⽤不同的空格数⽬
    注意:key后⾯的冒号,后⾯⼀定要跟⼀个空格,树状结构
    在这里插入图片描述Springboot⾥⾯常⽤ xx.properties(推荐)
    Key=Value格式
    在这里插入图片描述

2.SpringBoot注解配置⽂件映射属性和实体类实战


⽅式⼀ : 配置⽂件加载
  • Controller上⾯配置 @PropertySource({“classpath:resource.properties”})

  • 增加属性 @Value("${test.name}") private String name;

  • 实现
    创建src\main\resources\pay.properties
    在这里插入图片描述创建src\main\java\com\example\demo\config\WXConfig.java
    在这里插入图片描述

⽅式⼆:实体类配置⽂件

  • 添加 @Component 注解
  • 使⽤ @PropertySource 注解指定配置⽂件位置
  • 使⽤ @ConfigurationProperties 注解,设置相关属性
  • 必须 通过注⼊IOC对象Resource 进来 , 才能在类中使⽤获取的配置⽂件值
  • @Autowired private ServerSettings serverSettings;
    在这里插入图片描述
举报

相关推荐

0 条评论