Constructor Based Dependency Injection in Spring


This is a series and it would make a lot of sense if read in a sequence. You can find all the posts under the heading “Spring Beginners to Professionals“. We now know a lot about Spring beans and in the last post we mentioned about the dependency injection. It precisely means, how to connect two beans when one bean is composed in another bean. So let us start with practical implementation of Constructor Based Dependency Injection in Spring.

Constructor Based Dependency Injection

Constructor Based Dependency Injection is the one in which the wiring between beans is achieved using constructors. When such a dependency injection is performed, the container invokes a constructor with one or more arguments, each representing a dependency. In our example of OrderClient we need to inject a CSVDataStore. Let us create a constructor in OrderClient which accepts an argument of type DataStore.

In the configuration XML, we have to supply the dependency to the OrderClient as a constructor arg element and pass the reference of the csvDataStore bean

Now if we run the application, we can fetch a valid data store and subsequently the list of orders. It currently returns an empty list.

In the annotation configuration this can be done by invoking the constructor and passing the dependency as an argument manually as shown here:

At this point, you can run the code from the previous post and it should not throw any error.

This is no fun! The real power of annotation based constructor injection happens when the beans are not defined in the @Configuration class but are defined as individual classes annotated with @Component. You can use this annotation on any java class and Spring will treat that class as a Spring component.

Components in Spring

Components in Spring needs a separate discussion, but for our current situation let us just consider that a bean is a component. Or may I say, if the @Component annotation appears on a class, then Spring can treat the class declaration as a bean definition.

The way to declare beans using @Component annotation is fairly simple.

So, once Spring detects this class, it can create a bean definition out of this. How will it automatically detect the class is a topic for separate post where we can talk about component scanning.

Back to Constructor Based Dependency Injection

Coming back to our discussion on Constructor Injection. Let us say that this class has a property named dataStore and a constructor that accepts an argument of type DataStore. Then Spring will automatically detect the bean and inject it if an Autowired annotation is put on the constructor like this:

And now when you run the application, you will get an empty order list unlike the case of a null datastore.  But wait, this is not all. There is a catch.

What if there are more than one bean of type DataStore, for e.g.: a mySQLDataStore bean. This might confuse Spring and Spring might need some more help from us to inject the correct dependency.

Another more serious ambiguities in constructor arguments slip in when we use the XML based configuration. For e.g.: Arguments in XML are always represented as strings, but what if they mismatch? Who resolves the mismatching or ambiguity?


This post defines the constructor based injection and helps us understand the problems or constructor argument resolution.  So there is a need to discuss about the Constructor Argument Resolution Strategies in Spring and that is the topic for our next post. In the next post we will discuss the constructor argument resolution in detail.

Stay connected.