java - Spring 3中@Component和@Configuration的区别

标签 java spring spring-3

我遇到了 Spring 3 提供的两个注释(@Component@Configuration) 我对这些有点困惑。
这是我读到的关于 @Component

的内容

Put this “context:component” in the bean configuration file, it means, enable the auto-scanning feature in Spring. The base-package is indicate where are your components stored, Spring will scan this folder and find out the bean (annotated with @Component) and register it in Spring container.

所以我想知道 @Configuration 有什么用,那么如果 @Controller 将注册我的 bean 而无需在 spring 配置 XML 文件中声明它们。

最佳答案

来自 Book Pro Spring 集成

@Configuration 类和普通的 @Components 类一样,只是使用 @Bean 注释的方法用于工厂 bean。请注意,带有 @Bean 注释方法的 @Component 以相同的方式工作,只是不尊重范围并且重新使用 @Bean 方法调用(没有缓存),所以 @Configuration 是首选,即使它需要 CGLIB

关于java - Spring 3中@Component和@Configuration的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11903053/

相关文章:

java - 未调用 initBinder 方法来从 String 转换为 Enum

java - 通过 JQuery 向 Java 发送 JSON

java - 转换 JDBC 连接

java - 已指定限定符但得到 NoUniqueBeanDefinitionException

java - 使用准备好的语句返回时间戳

json - Spring 3.2 @ExceptionHandler @ResponseBody JSON 响应通过 Jackson

java - 调用非静态方法时遇到问题

java - Java 中使用 contains 不匹配字符串

java - Spring Batch 查找开始作业执行

java - 使用 Java Spring 3.0 @NumberFormat 注解