File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes spring in action - annotations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "spring in action - annotations" Watch "spring in action - annotations" New topic

spring in action - annotations

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Hello author,

Spring 3 has introduced annotations for injecting beans. Should we use it. What is your recommendation ?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

I personally never like to tie my code to any framework. I would prefer XML configuration.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Tommy Delson
Ranch Hand

Joined: Apr 13, 2011
Posts: 206
I've used XML configuration and Annotation, but I found that Spring Annotation is a choice to go due to it simple and straight forward. No more wiring and tons of xml configuration, I found that Annotation is far more efficient and maintenance free compared to xml.

The choice is yours...

"Your life is in your hands, to make of it what you choose."
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33108

I highly recommend this thread. It includes a variety of opinions and I particularly like Craig's opinion from earlier in the year.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Craig Walls
Ranch Hand

Joined: Sep 19, 2003
Posts: 335
In addition to that thread from earlier this year, let me make it clear that choosing to use annotations for dependency injection does not necessarily mean that you'll be placing Spring-specific annotations in your code.

One basic thing you can do is use javax.inject annotations (@Named and @Inject) instead of Spring annotations (@Component and @Autowired). These annotations are roughly equivalent and Spring supports them. So you won't be using Spring-specific annotations, you'll be using standard "passed through the JCP" annotations.

Also, with regard to the @Component annotation, you have another option: Create your own annotation, with runtime retention and annotate it with @Component. Now your custom annotation will work in place of @Component. So the only place you're using @Component is in the definition of your custom annotation--you'll use your annotation everywhere else.

Finally, you might consider using Spring's Java-based configuration. Yes, you'll be using Spring-specific annotations, but those annotations will be confined to configuration classes (which are like Spring's XML, but expressed in Java). Your application code won't need Spring annotations.

That said...and as I said before...use XML if you like, use annotations if you like, or use both (applying each where it makes the most sense). The nice thing is that Spring gives you the choice.

Spring in Action - Unleash POJO power in your applications!
Modular Java - Discover the secret weapon to modularity on the Java platform!
XDoclet in Action - Your complete guide to code generation with XDoclet.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927

Thanks for expert opinions. Is it possible to mix and match. Use xml and annotations in combintaion ? Also if I am using Spring annotations in a class and use the class in non -spring environment with no spring jar files in classpath, when the class gets loaded will it throw ClassNotFoundException as annotaions are not in classpath.
I agree. Here's the link:
subject: spring in action - annotations
It's not a secret anymore!