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 oft开发者_JAVA百科en 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?
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.
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.
Annotations are surely the way by which "newer" programming in java will continue. I use annotations for various uses...like @Scope
for scope of bean, @Required
for making dependency necessary, @Aspect
for configuring advices,@Autowired
for constructor injection using annotations. Since spring 2.5, annotation support has been good.
See here for spring tutorial, where annotation based issue are covered here.
I think that two cases that the usage of annotations could cause some problems. Firstly, if you want to write complex named queries (JPA) in your entities. I saw some entity code samples and asked myself whether the code is really java code or not. To many metadata in program code will reduce the readability of it which kills clean code principles.
Second problem is portability between JVM versions. Annotation is a feature 1.5+. If your software should support earlier JVM versions, then you may not use these.
Anyway, you can enjoy with annotations everytime without having any doubt and spare your time not changing IDE tabs to check XMLs if the property is still there or not, or entered correct etc.
For very small projects you could still XML version if you haven't too many stuff to be declared in spring. But, if you are in a huge project, the things could be very troublesome if you had 10 xml configs.
This will perhaps not help you much but at work they don't want to use autowiring because it needs a classpath scan (but that can be package-defined i think). So it increases the startup time of the application according to the size of the project.
精彩评论