Simplified Configuration with Spring and the @Configuration Annotation
For a few years there has been a Spring Module called Java Config that stood silently in the corner waiting for the right time to pounce. Well, Spring 3.0 is that right time. With the release of Spring 3.0, SpringSource has pulled this module into the core Spring Framework as @Configuration.
Updated inversion of control (IoC) tutorials and dependency injection examples
TheServerSide has been updating its resources in terms of the Spring framework, Java EE contexts and various different approaches to inversion of control (IoC) and dependency injection (DI). Check out these newer resources on the topic:
- Inversion of control explained fast with a Spring IoC example
- Spring MVC Java web development with Spring Boot
- Drawbacks to IoC and how to solve inversion of control problems
- Spring IoC vs. Google Guice's inversion of control approach
- Getting started with Google Guice
- Getting started with the Spring IoC container
- The beauty of Spring without XML: Annotation based IoC
But what is @Configuration? Why should we use it when we have XML and annotations for configuration already? Why do we need yet a third way to configure our applications?
This is like trying to solve the XML versus annotation religious wars. (Don't even ask about curly braces.) The fact is, XML and Annotations both have very valid pros and cons.
Spring Configuration Options: XML vs. Annotations
The XML camp talks about separation of concerns. XML configuration is outside the classes. The whole configuration can be viewed easily in only a few files. Furthermore, configuration can change without recompiling, etc.
Cons of XML could be that we have all these String values that need to be converted. Typos occur all over the place, they are difficult to debug, and they are not type safe because of the Strings.
The Annotations party, like myself, like the type safety of annotations. It can also self document a class, so that you can look in the class and truly see what is being injected by Spring.
The cons of Annotations is that it clutters the POJO, sometimes where you might say it is not a POJO anymore. If you change, add or delete, annotations require a recompilation and deployment.
So, who wins the debate?
Guess what: they are all correct. There is no best answer to this debate. So wouldn't it be great if we could get the 'pro' benefits of both styles and remove the cons.
With @Configuration, we can do just that.
@Configuration is configuration using Java classes. Each @Configuration Java class is its own configuration. Since it is Java, it is type safe, we can keep it as a POJO (with just a couple of annotations, we promise not too much), and we can have a clean separation of concerns. We will also have a single resource representing the application's configuration, and we can change it without having to change our application code.
So how do we use @Configuration?
First we create a Java class and add one annotation to it @Configuration.
Then we create methods with code to instantiate the application's classes and call other methods to configure the dependencies.
We add one annotation to these methods. @Bean.
Here is an example of an @Configuration configuration class.
In our example we are creating the infrastructure beans of a standalone application using JDBC. We have two methods: dataSource() and transactionManager(). Each are annotated with @Bean, which means the returned object in each method will be a Spring Bean added to the ApplicationContext. To handle dependencies, we have the transactionManager.setDataSource call the dataSource() method. That is all!
There are other annotations that you can add to your Configuration class that match almost everything that you can do in either XML or class annotations, including @Scope and @Primary. These configuration classes are easily tested because they are POJOs. Try writing a unit test with xml or annotations configurations.
It should be evident that in the @Configuration, that the code in the methods would have been the exact code you would have had to written in the first place if you were not using Spring. So you might ask, then why even use Spring? Well, we still need all the powerful goodness of Spring to manage our beans and their lifecycle, and to be able to add Spring enterprise abstractions through dynamic proxies. As well as Templates, Exporters and FactoryBeans that Spring provides. But those are other tutorials in themselves. For now, just enjoy the @Configuration.
Here are some other helpful tips, tricks, tutorials and examples by Cameron McKenzie (@cameronmcnz):