Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

spring in action - annotations

 
Ranch Hand
Posts: 8943
Firefox Browser Spring Java
  • 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 ?
 
Ranch Hand
Posts: 10192
3
Mac PPC Eclipse IDE 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.
 
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...


 
author & internet detective
Posts: 39996
807
Eclipse IDE VI Editor Java
  • 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.
 
author
Posts: 396
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: 8943
Firefox Browser Spring Java
  • 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.
 
I think I'll just lie down here for a second. And ponder this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic