|
From: Oliver H. <Ol...@ou...> - 2005-06-16 22:54:34
|
Are you really saving that much using annotations to validate required
bean attributes? Since the introduction of the Assert class coding these
afterPropertiesSet constraint checks has been made really simple. What's
the difference between typing
@@RequiredDependency()
And
Assert.notNull(property);
?=20
Not much. Slightly less typing is required, but by using the annotations
you introduce a set of dependencies that your POJO would never have had
before. If you're only going to be creating the object using a bean
factory this is acceptable, however if you want instantiate these
annotated objects outside of a bean factory (say in a test) your also
going to have to create all infrastructure needed to do the required
validation.
There's also the issue of how you express more complex constraints using
annotations. From Andy's example there's this (rewritten to use Assert):
=09
Assert.true(!getBeanFactory().containsBean(getViewPrototypeBeanName()),
"View bean '" + getViewPrototypeBeanName() + "' must be a prototype
(singleton=3D\"false\").");
To express this in an annotation you'd probably have to introduce some
kind of expression language (Groovy, OGNL?) for the constraint checking
and some kind of template processor for the exception message generation
- suddenly something that was reasonably simple to do in Plain Old Java
is requiring a considerable amount of support code. I'd imagine your
annotation would look something like this:
@@RequiredDependency("!
beanFactory.containsBean(viewPrototypeBeanName)", "View bean
'${viewPrototypeBeanName}' must be a prototype =
(singleton=3D\"false\").")
I hope I'm not giving you ideas here ;-)
Just my 2c.
Ollie
|