|
From: Andy D. <an...@ma...> - 2005-06-16 23:05:41
|
OK, so I've had a hidden agenda all along. :) My main reason for wanting to
annotate properties with their constraints is to provide "smart" handling of
these in other areas of the application. There are various places in my
application where having knowledge of the constraints on a property can come
in handy. As it is now, this knowledge is typically coded in one lump in one
method, with no way to "introspect" those constraints on a per-property
basis.
- Andy
On Thursday 16 June 2005 03:54 pm, Oliver Hutchison wrote:
> 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);
> ?
>
> 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):
>
>
> Assert.true(!getBeanFactory().containsBean(getViewPrototypeBeanName()),
> "View bean '" + getViewPrototypeBeanName() + "' must be a prototype
> (singleton=\"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=\"false\").")
>
> I hope I'm not giving you ideas here ;-)
>
> Just my 2c.
>
> Ollie
|