Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 ?

share|improve this question
1  
"Spring folks are bringing in so much changes over the configuration" - could you please give an example? That would help readers better understand your problem and answer your question –  gnat Apr 10 '13 at 9:18
9  
The question isn't that good, but the title sure is funny. –  Florian Margaine Apr 10 '13 at 9:52
1  
@gnat in the process of teaching myself spring, I googled around and came across same things expressed in many different ways. the spring documentation says one way of doing things, some tutorial says yet another way, both being right, the learning curve is so high.My only question is .. is there any clear documentation which shows all the possible ways of doing a single thing in spring ? –  tito Apr 10 '13 at 11:34
1  
@tito the question as you state it in the comments, "is there any documentation", sounds like a resource request. Resource requests are not quite welcome at Programmers. As far as I understand, one would rather present an underlying problem instead (as far as I can see, you did just that in question text, "getting confused to use") - a problem that was intended to be solved with particular resource requested –  gnat Apr 10 '13 at 11:36
2  
@tito I think your problem is mixing old tutorials with new documentation. Spring has become very "convention over configuration", where you don't need to express as much as earlier. However, old tutorials (especially pre-3.1) do a lot of stuff now unnecessary. –  Matsemann Apr 18 '13 at 9:11

4 Answers 4

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....

share|improve this answer
    
the level of configurations needed has gone way beyond the grasp for a small time developer like me,who have to juggle around with N number of frameworks to get a project done. –  tito Apr 10 '13 at 11:46
1  
I've got methods with ten annotations, each of which does a different useful thing… –  Donal Fellows Apr 10 '13 at 12:48
1  
On the other hand, it's also possible to have a JPA entity with a single @Entity annotation on the class and nothing else. If that isn't convention over configuration, I don't know what is. If you feel the need to have everything work exactly the way you want it, don't complain that you end up with a lot of configuration - be glad that it's possible at all. –  Michael Borgwardt Apr 11 '13 at 17:24
2  
I don't get why 4 annoations makes you sad, what would the equivalent xml file look like ? –  NimChimpsky Apr 25 '13 at 11:55
    
Oh, the XML would be far, far worse :-) –  Martijn Verburg Apr 25 '13 at 13:38
<annotation-driven />

You have to specify XML schema where this comes from.

Most likely its in the context of JPA Transaction handling strategy when defining the Transaction Manager ( see 9.5.6. Using @Transactional in Spring's docs )

When you define annotation-driven transaction handling - Spring AOP automagically creates aspects for your method to start ( or check for existing ) transaction before the invokation of a method and then commit ( or rollback in case of exception ) after the method ivokation has ended.

share|improve this answer

I have found that annotations based IoC/Bean creation is handy when you have a singleton implementation but may need to swap it out.

In constrast to a situation where you may need to reuse a bean repeatedly with different dependent classes/instances.

In those cases I declare the bean in config and usually do constructor injection of whatever dependency I need. Then, in the class that will use said bean(s) I @Autowire and then @Qualifier("") -- that's how factories are suppose to work.

A singleton is a factory method that only returns 1 result. When this doesn't handily apply that's when you need to mix it up.

As for using component-scan vs. not this really is subject to the above criteria and good judgement. I generally am very explicit about package component-scanning. That way I can create a different application context for testing where I can mock out external dependencies (like a db) while still testing the rest of my IoC setup.

In addition, I don't @Component any library code in my project. You never know when/how you will need to use a bean in there and if you @Component it, you may accidentally pick it up in a scan and you limit it's reusability. For these cases I generally have an application context defined in the library with some handy bean declaration defaults that my main project CAN included with imports into it's application context.

No religion here. Just experiences to pass on

share|improve this answer

Spring offers options, originally there was only the xml based wiring. Later annotation based wiring was added.
It is now possible (and many people do so) to use a mix of both.
There is ample documentation included with Spring and/or downloadable from the springsource website. There's professional training (never done it, can't vouch for it), and quite a few good books (I like APress' Pro Spring, pick the right release for the version of Spring you're employing).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.