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

时间:2016-01-21 18:18:53

标签: java spring dictionary annotations javabeans

尝试在config类

中创建具有原型范围的map 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?

1 个答案:

答案 0 :(得分:0)

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

您可以在SpringConfig课程中添加原型范围的bean - see here