Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices when using Spring 3 annotations

I'm looking for some best practices when using Spring 3 annotations.

I'm currently moving to Spring 3 and from what I've read so far I see a lot of accent placed on using annotations and moving away from XML configuration.

Actually what is recommended is a mix of both styles, with annotations covering things that won't change often or from one run to the next (e.g. a @Controller will remain like that for the life time of the application), while the things that change and must be configurable go into XML (e.g. a mail smtp address, endpoints for web services that your application talks to etc).

My question is what should go into annotations and to what extent?

At which point annotations make things harder instead of easier? Is the technology (Spring 3) fully adopted as to be able to make such statements or does it take some more time for people to gain experience with it and then reflect on the issue?

like image 940
ElenaT Avatar asked Apr 21 '11 20:04

ElenaT


2 Answers

It is always difficult to get real advanced information.

The easy tutorial with "look on my blog, I copied the hello word tutorial from Spring Source website... Now you can put fancy annotations everywhere, it the solution of all of our problems including cancer and starvation." is not really usefull.

If you remember right spring core had several purposes, among them:

  • to be non intrusive
  • to change any implementation/configuration of a bean at any time
  • to give a centralized and controlled place to put your configuration

Anotation fail for all theses needs:

  • They introduce coupling with spring (you can use standard anotation only but as soon as you have at least one spring anotation this is no longer true)
  • You need to modify source code and recompile to change bean implementation or configuration
  • Annotations are everywhere in your code. It can be difficult to find what bean will be really used just by reading the code or XML configuration file.

In fact we have shifted our focus:

  • We realized that we almost never provide several implementations of our services.
  • We realised that being dependant of an API is not that bad.
  • We realized that we use spring not only for real dependancy injection anymore, but mainly to increase productivity and reduce java code verbosity.

So I would use anotations when it make sence. When it is purerly to remove boilerplate code, verbosity. I would take care of using the XML configuration file for thing that you want to make configurable, even if it is only to provide a stub implementation of the service in unit tests.

like image 192
Nicolas Bousquet Avatar answered Oct 20 '22 16:10

Nicolas Bousquet


I use @Value for properties that are configured in external properties file via PropertyPlaceholderConfigurer, as kunal noted.

There is no strict line for when to use xml, but I use xml:

  • when the bean is not a class I control
  • when the object is related to the infrastructure or configuration rather than to the business logic.
  • when the class has some primitive properties that I would like configurable, but not necessarily via externalized configurations.

In response to your comment: spring is very widely adopted, but "good" and "bad" are very subjective. Even my lines are not universal truths. XML, annotations and programmatic configuration all exists for a purpose, and each developer / company have their preferences.

As I said - there is no strict line, and no universal good practice for annotations.

like image 35
Bozho Avatar answered Oct 20 '22 16:10

Bozho