java - @Import 注册非@Component bean
问题描述
我注意到,即使没有注释的类@Component
在注释中声明时也会注册到 Spring 上下文中@Import
。
@Configuration
@Import({MyBean.class})
class MyConfig {
@Bean
Object object(MyBean myBean) { // this works
return new Object();
}
}
class MyBean {} // no annotation here
这种行为在文档中不是很清楚@Import
。
这是想要的吗?它是否记录在某处?@component
导入非类和@Component
类之间有什么区别吗?
文档说:
允许导入@Configuration 类、ImportSelector 和 ImportBeanDefinitionRegistrar 实现,以及常规组件类
什么是“常规组件类”?任何类或用 注释的类@Component
?
解决方案
它在java 文档中声明使用ImportResource来导入不是@Configuration
如果需要导入 XML 或其他非@Configuration bean 定义资源,请改用@ImportResource 注解。
推荐阅读
- python - Selenium 没有通过链接文本找到元素
- wcf - WCF MaxPendingChannels 设置与 MaxConnections
- react-native - 为什么反应原生动画会在持续时间到期之前完成?
- c++ - 在 C++ 中实现图形类时遇到问题
- javascript - 如何使用 JavaScript 计算 onLoad 和 onChange?
- c# - 如何从两个类中获取某个 UI 控件
- php - PHP中是否有合并两个多维关联数组的函数?
- python - 修复包含漂亮汤方法的列表推导的语法错误
- powershell - 在所有物理(有线)适配器上设置静态 IP 地址
- python - 根据单词之间的数字范围选择字符串