Look ma! No XML! - No Fluff Just Stuff

Look ma! No XML!

Posted by: Craig Walls on November 28, 2006

In the week or so leading up to The Spring Experience, it seems that the Spring gurus are blogging wonderful little tidbits of Spring knowledge.

A few days ago, I mentioned about Rod Johnson's blog entry on attribute-based property injection in Spring 2.0. Today Rod treats us with Spring JavaConfig, another Spring configuration option...this time, sans XML.

Almost every time I talk about Spring, I always have one dissenter who poo-poos Spring over its use of XML for configuration. Personally, I have no problem configuring Spring with XML, especially with the shortcuts available in the latest versions of Spring. But I can understand how some folks may not like XML. I explain to them that XML is only the favored option with Spring...but that Spring could be easily extended to support other configuration options such as property files, YAML, annotations, and Groovy.

Nevertheless, those other options are largely conceptual. Sure, I wrote a bit last year explaining how to use annotations to configure Spring. And the Pitchfork project adds EJB 3-style annotations to the mix. But XML is still the most widely used configuration option for Spring.

With Spring JavaConfig, however, Rod presents yet another way to configure Spring with annotations. It's not what you might think, though. Instead of mixing annotations in with your regular application code, the annotations are attached to a Java class whose whole purpose in life is to define the Spring configuration.

I encourage you to read Rod's article on the subject for all of the details. But for a quick teaser, here's how the "knight" example from Listing 1.8 of Spring in Action might look if configured using Spring JavaConfig:

package com.springinaction.chapter01.knight;

import org.springframework.beans.factory.annotation.Bean;
import org.springframework.beans.factory.annotation.Configuration;

@Configuration
public class KnightConfig {
  @Bean
  public Quest quest() {
    return new HolyGrailQuest();
  }

  @Bean
  public Knight knight() {
    KnightOfTheRoundTable knight = new KnightOfTheRoundTable("Bedivere");
    knight.setQuest(quest());
    return knight;
  }
}

Although I still think that I'll favor the XML approach in general, Spring JavaConfig is pretty cool and could open up the possibility for more dynamic configuration (imagine if/then and for loops in the @Bean-annotated methods). I'm eager to see where this goes.

Speaking of the Spring Experience, I sadly will not be attending this year. The date falls too close to the birth of my second child and I didn't think it would be prudent to fly across the country leaving my wife at home. But I'll be following the event as closely as I can through blogs (if you're going, PLEASE blog about it as much and as often as you can. I'm counting on Matt Raible to help me out with that, but I'm looking forward to read other bloggers who attend). I'm also counting on some of my other friends to send me some of the swag from the event. It'll be almost like I'm there!

Craig Walls

About Craig Walls

Craig Walls is a Principal Engineer, Java Champion, Alexa Champion, and the author of Spring AI in Action, Spring in Action, and Build Talking Apps. He's a zealous promoter of the Spring Framework, speaking frequently at local user groups and conferences and writing about Spring. When he's not slinging code, Craig is planning his next trip to Disney World or Disneyland and spending as much time as he can with his wife, two daughters, 1 bird and 2 dogs.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »