Annotation-only configuration

A couple of years I have been working with annotations on the java 5 platform. My opinion on annotations is mixed. Are they easy to use ? Yes. Are they powerful additions to the platform ? Absolutely. Can they be misused ? Definately.

One of the more common frameworks I have seen annotations being used for is Spring.

In Spring you can use annotations to wire beans and do many more things. For instance registering the beans itself is possible by using annotations.
Unfortunately as of Spring version 2.5.5 it is not (yet) possible to annotate configuration which is stored in property resourcebundles.
Spring javaconfig is almost ready for production use and therefor not recommended for production use but does promise to solve this problem.

Spring almost allows you to completely place the Spring configuration in annotations. Only some initialization definitions for the Spring container should still be placed in the applicationcontext.xml.
A componentscanner for instance is necessary to scan packages for components. Beans which have been annotated as @Component, @Service or @Repository are being picked-up by Spring and registered in the applicationcontext ready for use.
It is, however, not wise to try to do as much configuration as possible by using annotations.

What if someone creates a new implementation of one of the interfaces in the component-scanned package(s)? This implementation is annotated as component and placed on the classpath.
If no qualifiers were used on the autowired annotated beanproperties (why would you if there is only one implementation of that particular interface…) then Spring will not know which implementation to autowire and you’re left with exceptions and a non-working application. This is definitely not what you want in a production environment.

Bottom-line:
To prevent unexpected discovery of components on the classpath, make sure that your spring configuration does not contain a componentscanner. Define all necessary beans in the applicationcontext.xml. Then autowiring can be done safely using annotations.

By still using an applicatoncontext.xml you remain having a single place for your configuration. This keeps things simple and clean. It also allows you to define beans which need constructor arguments of external configuration being supplied to them.

Comments are closed.

Algemeen (9)
GlassFish (49)
Java (58)
Managed Services (10)
Oracle (120)
Private Cloud Hosting (2)
SOA/BPEL/ESB (11)
Software development (92)
Strategie (7)
WSO2 (2)

WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.

  • 071 - 82 000 82
  • Rijndijk 137 | 2394 AG Hazerswoude-Rijndijk
Oracle SOA specialized partner
Java
GlassFish
WSO2
DEMO
i-bridge
Rabobank
Greencat
Jan de Rijk
Reuma Revalidatie Rotterdam
Robeco
VU Medisch Centrum
CHS
LUMC
TomTom
TKP
NCCW
Erasmus MC
UMCG
VIR
ANWB
BVA Auctions
D-Reizen
STEDIN