XML vs Annotation Based Spring Configuration: Why Not Do Both?


News: XML vs Annotation Based Spring Configuration: Why Not Do Both?

  1. There are benefits and drawbacks to using both XML and annotation based Spring configuration files. However, it doesn't have to be an 'either-or' type of decision. This quick tip shows you how to combine an annotation based configuration and an XML based configuration into one, single, easy to use, Spring beanFactory context.

    A Quick Tip: Combining Spring and XML Configurations into a Single beanFactory Context

    Recommended Books for Learning Spring

    Spring in Action  ~ Craig Walls
    Spring Recipes: A Problem-Solution Approach  ~ Gary Mak
    Professional Java Development with the Spring Framework ~ Rod Johnson PhD
    Pro Java EE Spring Patterns: Best Practices and Design Strategies ~ Dhrubojyoti Kayal

    Threaded Messages (8)

  2. The other way to do this is to use <context:component-scan> in your XML context which will pick up your @Configurations.

  3. Why XML?[ Go to top ]

    Why to use XML when we have annotations?

  4. Code wiring is NOT configuration[ Go to top ]

    Why to use XML when we have annotations?

    Because writing usernames and passwords (for example) into java code is extremely stupid idea.

  5. Why XML?[ Go to top ]

    Sometimes you are using a third party library that uses Spring XML beans.   For example, CXF, ActiveMQ, etc. 

    Also when doing unit testing and using @ContextConfiguration it is really handy to have some beans in XML and other beans wired via <component-scan>

  6. Anotations is a fashion[ Go to top ]

    Why to use XML when we have annotations?

    Anotations are useful for not - production code, and for (ugly) deployment dependent applications, it brokes many object-oriented paradigms and anotations was originally intended to other things, not to actually replace exisitng java components. Anotations are good, but used in that way are taking back the community to the BASIC era.


  7. All the different options for dependency injection that are available in Spring 3.0 were covered by Chris Beams in a webinar earlier this year:



    Adam FitzGerald


  8. advanced annotation configuration[ Go to top ]

    Spring's annotation based configuration is limited to very simple use cases.

    Spann, http://code.google.com/p/spann/ , is an advnaced annotation configuration extension for the spring framework. It resolves many limitations on the use of annotations in spring and is simple to use and extend, something like spring's xml schema based configuration, only with less configuration and, ofcouse, without xml but with annotations.

  9. Our company Lexaden has developed a number of productshttp://www.lexaden.com/main/entry/products_overview with Spring 3.1JPA 2.0Vaadin and Lexaden Web Flow - is a MVC framework and full web flow implementation for Vaadin component model.

    And I would like to sum up pros and cons of java annotation and xml configurations from our experience:

    • When it comes to JPA configuration since it is done once and usually are not changed quite often we stick to annotation configuration. You may have a concern regarding possibility to see a bigger picture of configuration - in this case we use MSQLWorkbench diagrams.
    • Xml configuration is very good to get a bigger picture of application but it maybe cumbersome to find some errors until runtime. In this case Spring @Configuration annotation sounds as a better choice since it let us see a bigger picture as well and also allows to validate configuration on compile time.
    • As for Spring configuration we combine both approaches: use @Configuration annotation with Services and Query interfaces http://www.lexaden.com/main/entry/spring_3_1_and_jpa and xml configuration for dataSource and spring configuration stuff like context:component-scan base-package="com.lexaden.platform"
    • But xml configuration bits java annotations when it comes to flow configurationhttp://www.lexaden.com/main/entry/annotations_vs_xml_configuration since it is extremely important to see a bigger picture of the whole business process. And it sounds cumbersome to have it implemented with annotations approach.

    As showcase you can try out Lexaden Administration http://admin.lexaden.com (login/password - demo/demo)