Dude, where's my XML? - No Fluff Just Stuff

Dude, where's my XML?

Posted by: Craig Walls on March 16, 2007

I've read plenty of bashing of Spring's XML configuration lately. I have written my fair share of Spring XML and I can understand the concerns. I can't say that I've felt the same pain that the complainers have felt, but I do understand their concerns.

I don't find the XML configuration any more verbose or harder to read than some of the Java-based configuration options. Annotations are nice, but throw enough of those on a method or a class and it becomes a jumbled mess, too.

Refactoring and string identifiers is a more valid concern, but it isn't really an issue for me, as I'm using the latest and greatest SpringIDE in Eclipse (really awesome plugin, btw). Yeah, I'm leaning on a tool to help me with this, but so is everyone else when they lean on their IDE to refactor Java code (try refactoring a method name with notepad if you don't believe me).

Nevertheless, I understand the concerns. I understand how some folks don't like putting their configuration separate from their code and how others have complaints about string identifiers in Spring not being refactorable. I get it. I still use XML to wire up my beans in Spring. But I understand why others are uneasy with it.

But did you know that XML is not the only way to configure Spring? I know that I've already mentioned it at least once on this blog. But with the recent Spring XML sucks noise in blogspace, I feel that it bears repeating.

As a reminder, Spring has always been able to support multiple configuration options. XML just happens to be the "standard" out of the box way to configure it. But there are plenty alternatives, none of which are XML-based:

  • Spring JavaConfig - Annotation-based configuration for Spring...with a twist.
  • Pitchfork - EJB 3-style dependency injection and AOP using annotations.
  • Spring Annotations - A basic approach to Spring configuration using annotations.
  • Craig's Spring annotations - My own blog entry on how to extend Spring to support annotations. (Not intended to be a full solution as much as a "here's how to extend Spring" article.
  • Jacn - Pure-Java (ie, no annotations) approach to configuring Spring
  • SpringBean Builder - Groovy builder for Spring configuration. (Part of Grails project.)
  • Springy - A JRuby-based DSL for building Spring configuration files.
  • That list has no less than three annotation-based options (four if you count my own simplistic approach), one pure Java/no-annotations option, and a couple of script-based options. I especially like Spring JavaConfig as it brings Spring configuration directly into Java with annotations and without string identifiers. And I'm starting to think to find appeal in the scripting language options.

    But if none of these strikes your fancy, then it's really not too difficult to extend Spring to support custom configuration options. In fact, the first thing I thought when I saw Guice was that it probably wouldn't be too difficult to build a Guice-like configuration on top of Spring's already established container, either by extending the container or through a post-processor. (I've not thought it through in detail, though.)

    (And I'm still waiting for someone to build a YAML configuration for Spring.)

    I will grant that none of these options has become mainstream and you don't see too much talk about them. I'm doing my part to let everyone know that they exist...if one of them looks promising to you, please give it a try and blog about it. But rather than gripe about how Spring sucks because its XML sucks, give one of these alternate configuration styles a spin.

    P.S. If anyone knows about a Spring config option that I've not listed here, please let me know.

    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 »