@ConfigurationProperties和@EnableConfigurationProperties配合使用

https://blog.csdn.net/u010502101/article/details/78758330

@ConfigurationProperties注解主要用来把properties配置文件转化为bean来使用的,而@EnableConfigurationProperties注解的作用是@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在IOC容器中是获取不到properties配置文件转化的bean的。使用如下:

1、spring boot启动时默认是加载application.properties配置文件的,假设该配置文件中内容为:

local.host=127.0.0.1
local.port=8080

2、创建一个类ComponentProperties,把配置文件转化为bean来使用。@ConfigurationProperties注解可以把properties文件转化为bean,然后使用@Component注解把该bean注入到IOC容器中。

原文地址:https://www.cnblogs.com/newlangwen/p/9232167.html