当前位置:网站首页>@Component与@Configuration区别
@Component与@Configuration区别
2022-06-29 16:45:00 【小小代码熊】
@Configuration本质上还是@Component,并且@Configuration标记的类必须符合下面的要求:
1.配置类不能是 final 类、都必须声明为static
2.配置注解通常为了通过 @Bean 注解生成 Spring 容器管理的类,它通常是为了表示这是一个配置类,通过这个类来注册多个Bean
3.配置类能在方法中声明,不能是 private
重要区别
如果使用 @Configuration 注解修饰的类,并且该注解中的 proxyBeanMethods 属性的值为 true,则会为这个 bean 创建一个代理类,该代理类会拦截所有被 @Bean 修饰的方法,在拦截的方法逻辑中,会从容器中返回所需要的单例对象。
使用 @Component 注解修饰的类,则不会为这个 bean 创建一个代理类。 那么我们就会直接执行用户的方法,所以每次都会返回一个新的对象。
如果我们将 @Configuration 注解中的 proxyBeanMethods 属性的值设置为 false,那么它的行为是否就会跟 @Component 注解一样?
@Configuration(proxyBeanMethods = false)
public class Config {
@Bean
public Hello hello(){
return new Hello();
}
}

通过结果,我们可以得出结论是:如果将 @Configuration 注解中的 proxyBeanMethods 属性的值设置为 false,那么它的行为就会跟 @Component 注解一样。所以最大区别其实就是,一个使用了单例模式一个没有
边栏推荐
- 【无标题】
- What is the strength of a software testing engineer who can get a salary increase twice a year?
- Huaxia Fund: sharing of digital transformation practice achievements in the fund industry
- MySQL进阶——存储引擎
- 【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
- A simple but scalable feature normalization method
- Tool chain empowers hundreds of companies, horizon opens the "Matthew effect" of mass production of intelligent driving
- Practice | extreme optimization of script errors - make script errors clear at a glance
- 最高81.98%!超百所“双一流”高校本科深造率公布
- 知道创宇为能源行业资产管理助力,入选工信部2021物联网示范项目
猜你喜欢
随机推荐
InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析
C language microblog user management system
【南京大学】考研初试复试资料分享
实践 | 移动端图片上传旋转、压缩的解决方案
C winfrom chart chart control bar chart and line chart
我的远程办公初体验 | 社区征文
Telnet+ftp to control and upgrade the equipment
6.25atcoderabc257e - addition and multiplication 2
Simulink仿真模式
Calibration of monocular camera and binocular camera with kalibr calibration tool
Review of mathematical knowledge: curve integral of type I
卷妹带你学数据库---5天冲刺Day4
A simple but scalable feature normalization method
[day 28] given a string s, please judge whether it is a palindrome string | palindrome judgment
Paper notes: e (n) equivariant graph neural networks
Metadata management Apache Atlas Compilation integration deployment and testing
图文带你彻底弄懂MySQL事务原子性之UndoLog
Why does selenium become the first choice for web automated testing? (source code attached)
flink sql rownumber 报错。谁遇到过啊?怎么解决?
How to configure logback? 30 minutes for you to thoroughly learn the code to stay up late and knock







