spring中原型bean的@component和@scope之间的差异

spring中原型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'

我正在使用ServiceLocatoryFactoryBean定位原型bean

  • 无论何时,我都试图通过使用定义bean来访问原型bean

    @Component(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    
  • 那我就要

    Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'square' available. 
    
  • 但是,当我使用

    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) 
    
  • 那我就不会出错了。在这种情况下,我得到两个独立的bean

    请帮助我理解为什么会这样

    @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)