Over a million developers have joined DZone.

Why I like Spring @Autowired for List Types

· DevOps Zone

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 have I been taking advantage of this feature?

Since I was developing an application that 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.

02.publicclassMongoSchemaManager implementsInitializingBean {
04.    @Autowired(required = false)
05.    privateList<MongoCollectionDropOperator> mongoCollectionDropOperators;
07.    @Autowired(required = false)
08.    privateList<MongoCollectionCreateOperator> mongoCollectionCreateOperators;
10.    @Override
11.    publicvoidafterPropertiesSet() {
12.        if(mongoCollectionDropOperators != null) {
13.            for(MongoCollectionOperator mongoCollectionOperator : mongoCollectionDropOperators) {
14.                mongoCollectionOperator.execute();
15.            }
16.        }
17.        if(mongoCollectionCreateOperators != null) {
18.            for(MongoCollectionOperator mongoCollectionOperator : mongoCollectionCreateOperators) {
19.                mongoCollectionOperator.execute();
20.            }
21.        }
22.    }

This MongoCollectionCreateOperator and MongoCollectionDropOperator List references are discovered even when being added in other modules' Spring contexts.

So, from one of our application's customer-specific Modules we get this Mongo collection creator:

2.    <constructor-argvalue="${mongo.messageCollection}"/>

And from another customer-specific module we have:

02.    <constructor-argvalue="baseSDCM"/>
03.    <constructor-arg>
04.        <beanclass="org.springframework.data.mongodb.core.CollectionOptions">
05.            <constructor-argname="size"value="1073741824"/>
06.            <constructor-argname="maxDocuments"value="1000000"/>
07.            <constructor-argname="capped"value="true"/>
08.        </bean>
09.    </constructor-arg>

This is all it takes, and it works great since the Mongo schema management logic lies in the core module and the operators are discovered without altering the schema management source code, and without having to explicitly define the core bean in every customer specific application. 


Published at DZone with permission of Vlad Mihalcea , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}