摘要:
主要使用的注解类 @PropertySource, @EnableConfigurationProperties, @ConfigurationProperties
- 属性值:prefix/value, 代表属性文件中的key前缀,即公共部分。如:
@ConfigurationProperties("mq.config-prefix")
- classpath: 即jar包内的文件路径;如
classpath:config/testconfig.properties - path:jar同级目录下文件路径;如
path:config/testconfig.properties
- 指定那些类是需要自动装配在配置类中
备注如下
- 属性文件中key格式,支持<横杠-> <下划线_> < 驼峰命名>. 如下
config.properties, javaBean中属性的命名一律驼峰命名
mq.queue_config.month-queue=month
mq.queue-config.yearQueue=year
mq.queueConfig.exchangeName=fanout-exchange
@ConfigurationProperties中的prefix,如果属性文件中如上形式,那么prefix必须用-连接,即@ConfigurationProperties(prefix = "mq.queue-config")
附上官网链接 link.

本文详细解析了Spring Boot中配置属性的使用方法,包括@PropertySource、@EnableConfigurationProperties和@ConfigurationProperties注解的应用,以及如何将配置文件内容映射到JavaBean实体中,特别强调了属性文件中key的格式要求和@ConfigurationProperties的prefix使用技巧。
479

被折叠的 条评论
为什么被折叠?



