@Configuration、@ConfigurationProperties用法

转:https://blog.csdn.net/qq_45545968/article/details/110355408

@Configuration、@ConfigurationProperties用法
代码示例:

application.properties中配置的属性值

配置方式一:使用@Configuration + @Value方式

调用方式:

运行结果:

 

配置方式二:使用@ConfigurationProperties获取配置信息,通过@Configuration或@Component注解使spring @ComponentScan能够扫到该类;经操作@Component替换为@Configuration也可以获取同样结果。

运行结果:

配置方式三:
首先创建一个类,并通过@ConfigurationProperties获取配置,注意此处没用@Component,所以该配置类不会被spring @componentScan扫描到。

该类通过@Component
@EnableConfigurationProperties(UserConfig04.class)使得获或配置信息得类生效

运行方式同上。

 配置方式四: 通过类方法加@Bean的方式使获取文件的类生效

结果方式同上:

注意文中的@Configuration 可以替换为@Component运行结果是一样的,但是两者是有不同的,@Configuration会为配置类生成CGLIB代理Class,@Component不会;看了篇博文有解释:
@Component和@Configuration作为配置类的差别



posted @ 2022-11-23 15:40  不死码农  阅读(302)  评论(0编辑  收藏  举报