简体   繁体   English

在@Configuration类中使用工厂创建映射bean

[英]create map bean with a factory in a @Configuration class

Trying to create a map bean with prototype scope in config class 尝试在config类中创建具有原型范围的map bean

@Configuration
public class SpringConfig {

   public SpringConfig() {
   }


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

But spring complains 但是春天抱怨

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=)}

How does one define a prototype map bean using annotations only (no xml)? 如何仅使用注释(不使用xml)定义原型Map Bean?

The error is occurring because Spring is trying to inject Composite into your method, but there is no such bean that corresponds to that class. 发生错误是因为Spring尝试将Composite注入到您的方法中,但是没有与该类相对应的bean。

You can add a prototype scoped bean inside your SpringConfig class - see here . 您可以在SpringConfig类中添加原型作用域的SpringConfig 请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM