spring Spring中@Configuration和@Component有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39174669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:11:52  来源:igfitidea点击:

What is the difference between @Configuration and @Component in Spring?

spring

提问by gaurav sood

@ComponentScancreates beans using both @Configurationand @Component. Both these annotations work fine when swapped. What is the difference then?

@ComponentScan使用@Configuration和 来创建 bean @Component。交换时,这两个注释都可以正常工作。那有什么区别呢?

回答by reos

@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 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 is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning

@Configuration 表示一个类声明了一个或多个@Bean 方法,并且可能会被 Spring 容器处理以在运行时为这些 bean 生成 bean 定义和服务请求

@Component 表示一个带注释的类是一个“组件”。在使用基于注释的配置和类路径扫描时,此类类被视为自动检测的候选对象。

@Configuration 是用@Component 元注释的,因此@Configuration 类是组件扫描的候选者

You can see more here

你可以在这里看到更多

http://docs.spring.io/spring-framework/docs/4.0.4.RELEASE/javadoc-api/org/springframework/context/annotation/Configuration.html

http://docs.spring.io/spring-framework/docs/4.0.4.RELEASE/javadoc-api/org/springframework/context/annotation/Configuration.html

@Configuration is also a @Component but a @Component cannot act like a @Cofinguration.

@Configuration 也是@Component,但@Component 不能像@Cofinuration 那样工作。

回答by Juan Rada

Actually answer is not complete, is it true that:

其实答案并不完整,是不是真的:

@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.

@Component 表示一个带注释的类是一个“组件”。在使用基于注释的配置和类路径扫描时,此类类被视为自动检测的候选对象。

But you do can create i.e MyConfiguration.java class then stereotype with @Componentand add @Beansdeclaration to it. In this way it will looks as a configuration, main difference is that when annotated class with @Configuration@Beanannotated methods are proxy using CGLIBwhich made in code calls after the first one to return bean from context instead of execute method again and create another instance as happens when using @Component with @Bean

但是您可以创建 ie MyConfiguration.java 类,然后使用构造型@Component@Beans为其添加声明。通过这种方式,它将看起来像一个配置,主要区别在于,当带有带@Configuration@Bean注释方法的带注释类使用CGLIB代理时,CGLIB在第一个代码调用之后从上下文返回 bean 而不是再次执行方法并创建另一个实例将@Component 与@Bean 一起使用

回答by Sachin Ghumbre

@Configuration- It is like beans.xml but java based bean configuration. It means class annotated with this annotation is the place where beans are configured which will be candidate for auto-detection. In this class, methods are annotated with @Bean which return object of the class.

@Configuration- 它就像 beans.xml 但基于 java 的 bean 配置。这意味着用此注释注释的类是配置 bean 的地方,它将成为自动检测的候选对象。在这个类中,方法用@Bean 注释,返回类的对象。

Example:

例子:

@Configuration
public class ConfigClass {

    @Bean
    public UserClass getObject() {
        return new UserClass();
    }
}

@Component- You cannot autowire (@Autowire) any class if it is not marked with @Component. It means when you want to autowire any class using annotation that class should be annotated with @Component.

@Component- 如果没有用 @Component 标记,则不能自动装配 (@Autowire) 任何类。这意味着当您想使用注释自动装配任何类时,该类应该用@Component 进行注释。

Example:

例子:

@Component
public class A { .... }

public class B { 
    @Autowired
    A a;
    .....
    .....
}

Spring Document for reference:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

Spring 文档供参考:https : //docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

回答by ha9u63ar

@Componentis imported by default with @Configuration. controllers, service, and repostory are children components (along with Configuration). They are also candidate for auto-detection.

@Component默认情况下使用@Configuration. 控制器、服务和存储库是子组件(与配置一起)。它们也是自动检测的候选者。

回答by Nandish

Apart from the differences highlighted by reos.

除了reos突出显示的差异。

The reason why @Configuration cannot be replaced by @Component is as below:

@Configuration不能被@Component替换的原因如下:

The difference is in how the inter bean dependency is handled. Refer the link for a detailed explanation with example: Difference between Configuration and Component

不同之处在于如何处理豆间依赖关系。有关示例的详细说明,请参阅链接: 配置和组件之间的差异