Spring Java Configuration

I have found that a lot of Spring developers I know still do not know about or use Spring Java Configuration (aka JavaConfig). Spring 3.0 introduced this feature which allows Spring to be configured entirely in Java – no more XML!! I really enjoy using JavaConfig because, well… it’s Java! This means it has all the benefits of Java type-safety and IDE support:

IDE refactoring tools will automatically update JavaConfig because it is regular Java

IDE automatic import feature can be used instead of typing fully-qualified class names in XML

IDE drill-down feature

IDE auto-complete feature

I think you get it, anything that works with Java..

Some of the features above do come built into more advanced IDEs that have Spring support, but it is just nice to not rely on custom IDE Spring support to get these features. The compile-time checking is a big one for me. I refactor my code frequently, and changing field names or moving or renaming a Java file frequently breaks my XML Spring configuration. Of course, I usually don’t realize it until I deploy the application and get a Spring initialization error. Now, my IDE automatically updates the JavaConfig references when I refactor my code, because it’s just regular java. Also, if I forget to use my IDE’s refactor tools, I still get a compile error in the JavaConfig which alerts me before deployment that I have a problem.

Let me show a basic example of how XML configuration translates into JavaConfig. First, XML configuration:

It is a pretty-simple one-to-one conversion. Each <beans> document is translated into a Java class annotated with @Configuration. Each <bean> element is translated into a single method annotated with @Bean. The name of the method corresponds to the bean ID. To reference another bean in JavaConfig, you just call the method for the desired bean (see personService above).

One other observation I have had about Spring JavaConfig is that it is more natural to use constructor injection over setter injection – because it results in a one-line method. This is a plus to me, as I usually prefer constructor injection. If a class requires certain dependencies to function properly, I prefer to require them in the constructor. That way, I can ensure that the class is initialized into a valid state. I have noticed that, for whatever reason, constructor injection seems to be rarely used in XML configuration. Maybe constructor-arg is too hard to remember? I don’t know, but I like that JavaConfig seems to be bringing that back some.

Now, to bootstrap the JavaConfig Application Context, Spring has provided a new ApplicationContext implementation: AnnotationConfigApplicationContext. It is used the same as we are used to using ClassPathXmlApplicationContext:

This post just scratched the surface of JavaConfig. I will follow-up with posts on how to import beans from other @Configuration classes or XML files as well as how to use custom namespaces. Also check out my post on how to set up a Spring-MVC application using JavaConfig.

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial3>

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

4 comments

Excellent post. I am planning to use Java config for my next project. One correction: for line ApplicationContext ctx = new AnnotationConfigApplicationContext(PersonService.class); Should it be “ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);”

Hey Byron, maybe I find a little mistake you have made. In the snippet code of your “Translated to JavaConfig”, you maybe refer “return personDao();” to “return personDao;”, otherwise it is an infinite recursion.

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Join Us

With 1,043,221 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.