• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

XML vs annotations

 
Ranch Hand
Posts: 333
Scala Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Craig,

Spring in Action is concentrated on which approach?
Annotation based approach or XML based approach?
I am used Xml based configurations and would like to learn annotation based approach.
Which version of spring does it cover?
I am a big fan of "in Action" series.
Hopefully I am one of the fortunate winners

Regards,
-Pankaj.

 
author
Posts: 396
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

At the beginning of the writing phase, I tried to cater to both sides, XML and annotation-oriented config. Toward the end, I started favoring annotation-oriented configuration. So, to be honest, it's a bit uneven coverage throughout the book. But certainly annotation-oriented configuration is dealt with throughout most of the book. Without a doubt, future editions of the book will have little-to-no mention of the XML configuration.


Pankaj Shet wrote:Hi Craig,

Spring in Action is concentrated on which approach?
Annotation based approach or XML based approach?
I am used Xml based configurations and would like to learn annotation based approach.
Which version of spring does it cover?
I am a big fan of "in Action" series.
Hopefully I am one of the fortunate winners

Regards,
-Pankaj.

 
Pankaj Shet
Ranch Hand
Posts: 333
Scala Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your Reply Craig, but I don't understand, why annotation based configurations are given more importance now-a days..

Maintainece is more easier in XML than in annotation based configs, as if any changes come in the project, we don't have recompile the java code when using XML,
but we do need to recompile when using Annotation based approach.

So what are the advantages of annotations over XML based approach?
One is ease of use, what are the others?

I will not go for annotations just because we are signed up to write a code in java...

Regards,
-Pankaj


 
Ranch Hand
Posts: 90
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, annotations are a huge boost in productivity for a number of reasons including:
  • The configuration is checked to some degree by the type system. Thus, it's impossible to misspell the name of a configuration attribute when using annotation-based configuration because that results in a compilation error. On the other hand, XML-based configuration is susceptible to this.
  • Using your IDE's help popup, you can quickly look up the documentation for a particular configuration attribute within the editor. With XML configuration, you have to refer to the website.
  • You can refactor annotation-based configuration literally by moving a block of code containing configuration annotations to another component, and this automatically moves the configuration.
  • The configuration of a component is inline with the component being configured. You don't have to remember the two (or more) different locations of files that affect the same component. Think of Javadoc and how it allows programmers to specify the documentation for a Java class and its methods inline with the source code of the class, and now imagine that instead of Javadoc, programmers needed to maintain a separate XML document containing the documentation for a project's public classes, methods, and fields. You can imagine how difficult that would be.
  • Because annotations are Java code, there is no need to "switch" between writing Java and XML.
  • Using the IDE's "find references" feature, you can quickly see if the project is using a particular configuration attribute.
  •  
    Craig Walls
    author
    Posts: 396
    8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel has given some solid benefits of annotations over XML, so I won't repeat them here.

    As for the recompile argument: Generally speaking, your Spring configuration (XML or not) is contained within the deployable unit (JAR, WAR). Therefore, there's a build step anyway. Certainly you're not putting all of your Spring configuration XML external to the deployable unit, are you? Even if you decide to externalize a portion of your configuration (for administrative configuration purposes), it's a bad idea to externalize all of it (it leaves open the possibility of someone screwing up the config post-build).

    So, if you're going to rebuild the deployable unit anyway, then what's the problem with the configuration being in Java? If you decide to externalize a portion of it, you can still write that bit in XML, but leave everything else Java config'd and internal to the deployable.


    Pankaj Shet wrote:Thanks for your Reply Craig, but I don't understand, why annotation based configurations are given more importance now-a days..

    Maintainece is more easier in XML than in annotation based configs, as if any changes come in the project, we don't have recompile the java code when using XML,
    but we do need to recompile when using Annotation based approach.

    So what are the advantages of annotations over XML based approach?
    One is ease of use, what are the others?

    I will not go for annotations just because we are signed up to write a code in java...

    Regards,
    -Pankaj


     
    Pankaj Shet
    Ranch Hand
    Posts: 333
    Scala Spring Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes Craig and Daniel, I do agree with you both..
    Daniel's points summarizes ease of use.
    Thanks Craig for explaining the major drawback of XML

    I do completely agree with you

    it's a bad idea to externalize all of it (it leaves open the possibility of someone screwing up the config post-build).



    Annotations enforces configuration security.

    I definately need to learn Annotation based approach.

    Now I surely need to win this book. Hope luck favors me..

    Thanks Craig and and Daniel for your awesome explaination.

    Regards,
    -Pankaj.
     
    Bartender
    Posts: 1682
    7
    Android Mac OS X IntelliJ IDE Spring Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ill add one more more to the pile. I just love being able to set breakpoints within my @Bean configuration without combing through the XML parsing code to find out where I need to set it
     
    Craig Walls
    author
    Posts: 396
    8
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    While we're piling answers on, I've got one more.

    A Spring configuration class is itself a Spring bean. Therefore, it's possible to for Spring configuration to configure Spring configuration. (It's not a common thing to do, but when you need it, it sure is handy.)

    Bill Gorder wrote:Ill add one more more to the pile. I just love being able to set breakpoints within my @Bean configuration without combing through the XML parsing code to find out where I need to set it

     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!