Spring Configuration – Resolving Common Confusions

javaspring

Somewhere I read Spring offers convenience over configuration. But Spring folks are bringing in so much changes over the configuration , that I am now really getting confused to use the xml configuration or the annotation.

I would like anyone to suggest a surefire methodology or rule of thumb in using xml and annotations.


Examples at SO to show that many beginners like me are getting confused over the configuration.

  • link-1

    I don't seem to grasp the functionality behind <context:annotation-config> and <context:component-scan>.

    From what I've read they seem to handle different annotations (@Required, @Autowired etc vs @Component, @Repository, @Service etc) but also from what I've read they register the same bean post processor classes.

    To confuse me even more, there is an annotation-config attribute on <context:component-scan>

  • link 2

    I still have the component scan tag:

    <context:component-scan base-package="com.mycompany.maventestwebapp" />
    

    but I have also another tag (that look like have similar task), this one:

    <annotation-driven />
    

    What is the difference between these two tags?
    An other "strange" thing is that the previous example (that don't use the annotation-driven tag) is very similar to the project create by STS using the Spring MVC Template project but if I delete the annotation-driven tag from its configuration file the project don't run and give me the following error: HTTP Status 404 –

Spring 3.2 no longer need cglib for proxying , but lower versions uses cglib. A quote from the springsource blog

In order to generate such proxies, Spring uses a third party library
called cglib. Unfortunately, this project is not active anymore. In
Spring 3.2, it is very likely that Spring will be using Javassist
instead by default.

Are these enough to suggest that Spring is Confusion over configuration ?

Best Answer

Spring aims to provide you a framework where there is "convention over configuration". However the reality is that Spring applications do need a certain amount of configuration.

In the Spring 2.5.x and earlier versions, the common idiom was to provide this configuration via XML. With Spring 3.0+, the idiomatic way is to use annotations (something that Java EE6/7 also encourage).

As a side note, it can be amusing (saddening?) to see an annotated JPA entity, it's rather easy to add 4+ annotations to a single field....