简体   繁体   中英

create map bean with a factory in a @Configuration class

Trying to create a map bean with prototype scope in config class

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

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.

You can add a prototype scoped bean inside your SpringConfig class - see here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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