Spring中的@Configuration和@Component有什么区别? [英] What is the difference between @Configuration and @Component in Spring?
问题描述
@ComponentScan
同时使用@Configuration
和@Component
创建bean.交换时,这两个注释都可以正常工作.那有什么区别?
@ComponentScan
creates beans using both @Configuration
and @Component
. Both these annotations work fine when swapped. What is the difference then?
推荐答案
@Configuration表示一个类声明一个或多个@Bean 方法,可以由Spring容器处理以生成bean 这些bean在运行时的定义和服务请求
@Configuration Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime
@Component表示带注释的类是组件".这样的 类在使用时被视为自动检测的候选者 基于注释的配置和类路径扫描.
@Component Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
@Configuration带有@Component元注释,因此 @Configuration类是组件扫描的候选对象
@Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning
您可以在此处看到更多信息:
You can see more here:
@Configuration也是@Component,但是@Component不能像@Configuration那样起作用.
A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.
这篇关于Spring中的@Configuration和@Component有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!