651.288.7000 info@intertech.com

Spring 4 offers an array of new features.  As with all releases, some of the new features are big and impactful.  Others are tiny but helpful.

Autowiring Collections

One of the new small features in Spring 4 is the ability to use @Order with autowiring.  There has always been the ability to have Spring automatically collect all beans of a given type and inject them into a collection or array.  Since Spring 2.5, this was most commonly accomplished by annotation as shown below (injecting an Employee and Customer bean into the List of people on Organization).

In this case, however, there is no order associated to the list of beans added to the Organization’s people.  You could use XML to define the beans and then, in most cases, the beans will be added to the people list in the order they are defined in the XML.  However, you may be trying to avoid or minimize the amount of XML in your applications.  Also, most developers know better than to depend on the order of the elements in an XML to preserve any order at runtime.  Well, Spring 4 provides a solution.

@Order and autowiring

The @Order annotation has been in the Spring Framework for some time (since 2.0 per the documentation).  It has always been used to provide order to components like AspectJ aspects.  Now, in Spring 4, it can order beans wired to an ordered collection.  Add the @Order annotation to the bean classes and specify the order precedence (as shown below).  Lower values have higher priority – meaning they will appear first in the collection.


Wrap Up

There are many new features in Spring 4 and I am busy updating our Complete Spring Framework class to teach them to you. Keep an eye out on this blog for more examples of Spring 4 features. Better yet, contact us to sign up for an upcoming Spring class.

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