Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

spring in action - annotations

 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello author,

Spring 3 has introduced annotations for injecting beans. Should we use it. What is your recommendation ?
 
Joe Harry
Ranch Hand
Posts: 10091
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally never like to tie my code to any framework. I would prefer XML configuration.
 
Tommy Delson
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I highly recommend this thread. It includes a variety of opinions and I particularly like Craig's opinion from earlier in the year.
 
Craig Walls
author
Ranch Hand
Posts: 363
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic