Akhtar
Akhtar

Reputation: 93

How to autowire a collection of a particular size

I am trying autowire a list of objects for a particular size but inspite searching through all stackoverflow related questions I am unable to see anything like this discussed before.I am surprised that this would never be needed?

In below example I see that the size of myListeners is always 1. How do i make myListeners list having 10 instances of MyListener

@Component
@Scope("prototype")
public class ListenerThreadPool {
private static final Logger LOGGER = LoggerFactory.getLogger(ObjectPool.class);

private  ExecutorService threadPool;
private  int poolSize;

@VisibleForTesting
final AtomicBoolean isStarted = new AtomicBoolean(false);


@Autowired
public void setMyObject(List<MyListener> myListeners) {
    this.myListeners = myListeners;
}

@Autowired
@VisibleForTesting
List<MyListener> myListeners;


public void setPoolSize(int poolSize) {
    this.poolSize=poolSize;
    this.threadPool = Executors.newFixedThreadPool(poolSize);
}

public void start() {
    if (isStarted.compareAndSet(false, true)) {

        for (int i = 0; i < myListeners.size(); ++i) {
            LOGGER.info("Starting listeners pool " + myListeners.get(i).toString());
            threadPool.submit(myListeners.get(i));
        }
    } else {
        LOGGER.warn("Cannot start listeners pool because it's already started");
    }
}

}

I tried something like below but I get error @Bean public List listMyListeners(@Value("${workflow.threads:10}") int threads) { return new ArrayList(10); }
@Autowired @Qualifier("listMyListeners") List myListener;

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: java.util.List com.groupon.mailman.messaging.MyListenerPool.myListener; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.groupon.mailman.messaging.MyListener] found for dependency [collection of com.groupon.mailman.messaging.MyListener]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=listMyListeners)}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.groupon.mailman.messaging.MyListener] found for dependency [collection of com.groupon.mailman.messaging.MyListener]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=listMyListeners)}

Upvotes: 0

Views: 151

Answers (1)

Artem
Artem

Reputation: 2500

If you're using @Autowired for java.util.List it will inject a list of all Java Beans matching the List’s Generic type.

So, obviously, you need more beans of type MyListener in your context(now it's only one).

Upvotes: 0

Related Questions