spring中原型bean的@component和@scope之间的差异
我正在使用ServiceLocatoryFactoryBean定位原型beanspring中原型bean的@component和@scope之间的差异,spring,Spring,我正在使用ServiceLocatoryFactoryBean定位原型bean 无论何时,我都试图通过使用定义bean来访问原型bean @Component(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) 那我就要 Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'square'
@Component(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'square' available.
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@Component(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
创建名为“prototype”的组件
是名为“from class”的组件的作用域,因此您尝试查找名为“square”的组件,但有名为“prototype”的组件。可能您正在尝试注入名为
square
(来自错误堆栈跟踪)的bean@Component(value=ConfigurableBeanFactory.SCOPE\u PROTOTYPE)
与@Component(value=“PROTOTYPE”)
相同,这意味着带注释的类是Spring管理的bean,其逻辑名称是PROTOTYPE
。因为prototype!=square
,Spring无法找到具有此配置的名为square的组件。需要记住的一点是,传递给@Component
的值表示Spring托管bean的逻辑名称,而不是其生命周期范围。
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)