651.288.7000 info@intertech.com

This post is part of my series on some of the new Spring 4 features. As of Spring 4, generic types can be used to determine what bean should be dependency injected (either by XML or annotation).

Wiring By Generic Type

An example can help clarify this new feature.  Assume you defined your DAO by generic interface (if you haven’t studied Java generic typing, you may want to visit here to learn more).

Now consider a case were two beans are created in the container using this interface (the example below uses Spring’s Java Configuration option for this purpose).

As of Spring 4, the generic type can be used by the container to determine which bean (by type) to dependency inject.  Continuing the example, below the Dao<Person> property would get the Dao<Person> bean defined in the Java Configuration above (and not the Dao<Organization> bean).

In the Past

In versions of Spring prior to Spring 4, generics did not help qualify which bean would be dependency injected into the dao property.  For example, in an older Spring application (prior to Spring 4), attempting the wiring as laid out here would have resulted in a NoUniqueBeanDefinitionException.

Wrap Up

I hope your 2014 is setting up to be a good one.  If learning Spring is on your list of New Year resolutions, consider taking the Complete Spring Framework class from Intertech. Visit our web site and learn more about our Spring classes.

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!

Pin It on Pinterest

Share This