Why I like Spring @Autowired for List types

(Last Updated On: January 29, 2018)

Spring Framework dependency injection is great, and almost every Java developer uses it nowadays. Using @Autowired to inject Java Beans is trivial, but we can also use this annotation for java.util.List, or java.util.Map as well. The former will inject a list of all Java Beans matching the List’s Generic type, while the latter will create a map of these beans mapped by their names.

How I’ve been taking advantage of this feature?

Since I was developing an application which has a framework module and a specific customer implementation module, there were cases where I needed to add a common logic in the framework module which would detect all Beans of a given type, even the ones defined in the specific module.

In the following example, I will demonstrate how this works. My task was to have a MongoDB schema management component. The manager is included in the framework module, and auto scanned by spring.

@Component
public class MongoSchemaManager implements InitializingBean {

    @Autowired(required = false)
    private List<MongoCollectionDropOperator> mongoCollectionDropOperators;

    @Autowired(required = false)
    private List<MongoCollectionCreateOperator> mongoCollectionCreateOperators;

    @Override
    public void afterPropertiesSet() {
        if (mongoCollectionDropOperators != null) {
            for (MongoCollectionOperator mongoCollectionOperator : mongoCollectionDropOperators) {
                mongoCollectionOperator.execute();
            }
        }
        if (mongoCollectionCreateOperators != null) {
            for (MongoCollectionOperator mongoCollectionOperator : mongoCollectionCreateOperators) {
                mongoCollectionOperator.execute();
            }
        }
    }
}

This component gets injected the MongoCollectionCreateOperator and MongoCollectionDropOperator references, and it discovers those
even when added in specific module spring contexts.

So from one of our application module we get this mongo collection creator:

<bean id="messageMongoDbCollectionCleaner" class="mongo.storage.MongoCollectionDropOperator">
    <constructor-arg value="${mongo.messageCollection}"/>
</bean>

and from other module we have:

<bean id="sdcmMongoCollectionCreateOperator" class="mongo.storage.MongoCollectionCreateOperator">
    <constructor-arg value="baseSDCM"/>
    <constructor-arg>
        <bean class="org.springframework.data.mongodb.core.CollectionOptions">
            <constructor-arg name="size" value="1073741824" />
            <constructor-arg name="maxDocuments" value="1000000" />
            <constructor-arg name="capped" value="true" />
        </bean>
    </constructor-arg>
</bean>

If you enjoyed this article, I bet you are going to love my Book and Video Courses as well.

This is all it takes, and it works great since the mongo schema management logic lies in the core, and the operators may be added dynamically without altering the schema management source code.

Subscribe to our Newsletter

* indicates required
10 000 readers have found this blog worth following!

If you subscribe to my newsletter, you'll get:
  • A free sample of my Video Course about running Integration tests at warp-speed using Docker and tmpfs
  • 3 chapters from my book, High-Performance Java Persistence, 
  • a 10% discount coupon for my book. 
Get the most out of your persistence layer!

Advertisements

4 thoughts on “Why I like Spring @Autowired for List types

  1. will it still work for ChildClasses ?
    eg.class MyClass extends mongo.storage.MongoCollectionDropOperator
    with bean of MyClass.

    1. Thanks, I use both @PostConstruct and the InitializingBean interface in my projects. As you said it’s preferred to use the standard annotations,but I don’t really mind using Spring interfaces either since I haven’t work on any project where I’d change it to Guice or JEE.

Leave a Reply

Your email address will not be published. Required fields are marked *