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

标签 spring

@ComponentScan 使用 @Configuration@Component 创建 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 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

你可以在这里看到更多:

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

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

关于spring - Spring中的@Configuration和@Component有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39174669/

相关文章:

java - JSessionId 在登录后发生变化,Angular 5 正在通过请求发送新的 JSessionID 并丢弃旧的 jsessionid

java - 使用 Hibernate 删除表不起作用

java - postgresql 查询的大型结果集

spring - 如何为多个包指定单个切入点

java 通用问题。为什么这个有效?

Java CRUD 应用程序卡在 "Update"上!

spring - 为什么 hibernate 实体图获取嵌套的惰性集合

java - Grails 应用程序未部署在 tomcat 中

java - Spring Security 方法级安全性与 Java 配置 + REST 身份验证?

spring - 无法下载 Spring 框架 jar 文件