SpringBoot中的各种注解
1、@ConfigurationProperties
它的作用是来获取application.yml / application.properties配置的配置项内容,与 添加了该注解的类或方法 的属性进行绑定。
它的生效需要结合@Bean或者 @Component 或者 @EnableConfigurationProperties
(1)结合@Bean
(2) 结合@Component
(3) 结合@EnableConfigurationProperties
如果一个配置类只使用了@ConfigurationProperties注解,而没有使用@Component,那么在IOC容器中是获取不到properties 配置文件转化的bean。 @EnableConfigurationProperties 相当于把使用 @ConfigurationProperties 的类进行了一次注入。
创建配置项类 JdbcProperties :
将配置项类JdbcProperties 注入到配置类 JdbcConfig中,使用注解 @EnableConfigurationProperties