【问题标题】:create map bean with a factory in a @Configuration class在 @Configuration 类中使用工厂创建地图 bean
【发布时间】:2016-01-21 18:18:52
【问题描述】:

尝试在配置类中创建具有原型范围的地图 bean

@Configuration
public class SpringConfig {

   public SpringConfig() {
   }


   @Bean
   @Scope("prototype")
   public Map<String, Composite> getCompositesMap() {
      return new LinkedHashMap<String, Composite>();
   }
}

但是春天抱怨

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.eclipse.swt.widgets.Composite] found for dependency [map with value type org.eclipse.swt.widgets.Composite]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, mappedName=, description=, name=, type=class java.lang.Object, authenticationType=CONTAINER, lookup=)}

如何仅使用注释(无 xml)定义原型映射 bean?

【问题讨论】:

    标签: java spring dictionary annotations javabeans


    【解决方案1】:

    发生错误是因为 Spring 试图将 Composite 注入到您的方法中,但没有对应于该类的此类 bean。

    您可以在 SpringConfig 类中添加一个原型作用域 bean - see here

    【讨论】:

    • 了解可以提供“复合”bean。这不是问题。问题是首先没有必要要求“复合”bean。地图是空的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2014-12-31
    • 1970-01-01
    相关资源
    最近更新 更多