Spring-boot(三)通过微信熟悉熟悉Spring-boot properties配置文件及相关注解

前言:日常项目中除了会使用yml作为Springboot的配置文件,同样也会使用properties,使用非常简单,下面简单介绍两种。

  1. 对象类型的基本语法
    即:java类.对象.属性=属性值
	wechats.chats.imageUrl=zhangImageUrl
	wechats.contacts.imageUrl=contactsImageUrl
  1. 数组类型的基本语法
	wechats.tmptest[0]=tmptest1
	wechats.tmptest[0]=tmptest2

properties配置文件本身比较简单,但其重点在于配合注解需要注意的一些特点:

  1. @ConfigurationProperties(prefix = “wechats”)
  • ConfigurationProperties的prefix代表当前类对应配置文件中wechats,即:将类和配置文件中配置项绑定
    该标签默认从全局配置文件中取值
  1. @Component
  • 在当前类的类名上加@Component,代表当前类被注入到Spring容器中,否则无法完成映射
  1. @PropertySource(value = (“classpath:application-other.properties”))
  • 通过@PropertySource可以指定当前类映射哪个配置文件,在配置文件较多的时候还是比较实用的
  • 通过@PropertySource 貌似,貌似,貌似 只能匹配properties文件,yml目前无法使用该注解
  1. EnableAutoConfiguration
  • SpringbootConfiguration中的其中一个,即:自动帮我们把tomcat之类的配置进行默认配置!
发布了90 篇原创文章 · 获赞 1103 · 访问量 80万+

猜你喜欢

转载自blog.csdn.net/u010569419/article/details/103142164
今日推荐