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

后端 未结 5 1130
感动是毒
感动是毒 2020-12-05 00:00

@ComponentScan creates beans using both @Configuration and @Component. Both these annotations work fine when swapped. What is the diff

5条回答
  •  鱼传尺愫
    2020-12-05 00:18

    @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

    You can see more here:

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

    A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.

提交回复
热议问题