[seam-issues] [JBoss JIRA] Commented: (SEAMVALIDATE-7) ConstraintValidator instances created incorrectly

Jozef Hartinger (JIRA) jira-events at lists.jboss.org
Thu Mar 10 06:04:46 EST 2011


    [ https://issues.jboss.org/browse/SEAMVALIDATE-7?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12587100#comment-12587100 ] 

Jozef Hartinger commented on SEAMVALIDATE-7:
--------------------------------------------

Gunnar,

the ConstraintValidators deployed in a Bean Deployment Archive (BDA) that fulfill CDI constraints (not final classes, appropriate constructor, etc...) are CDI beans. The container scans for these during bootstrap, creates the AnnotatedType and InjectionTarget metadata for each found CDI bean and lets CDI extensions manipulate these metadata by observing and manipulating bootstrap events (e.g. ProcessAnnotatedType, ProcessInjectionTarget, etc...)

Example usecases:
* An extension may for example want to add a qualifier to the bean (i.e. configuring the ConstraintValidator via the Seam Config extension). = wrapping AnnotatedType
* An extension may want to provide additional dependency injection options (i.e. besides support for @Inject, an extension may provide JAX-RS injection which is triggered by @Context) - this would be implemented by listening to the ProcessInjectionTarget of every bean and wrapping this InjectionTarget instance that contains the dependency injection logic implemented in the inject() method
* others...

The result of extension's metadata manipulation stays stored within the container and influences how the beans behave. Therefore, if you extract the bean metadata from scratch (the ConstraintValidator class only) on every instance creation, the metadata created during container bootstrap is never used and the bean can no longer be manipulated by CDI extensions.

Besides, performance is probably also an issue since you force the container to create the AnnotatedType and InjectionTarget every time.

So instead of creating bean metadata every time, simpy ask the container for an instance of a particular type as shown above.


Hope this makes sense. Should you have further questions don't hesitate to ask.

> ConstraintValidator instances created incorrectly
> -------------------------------------------------
>
>                 Key: SEAMVALIDATE-7
>                 URL: https://issues.jboss.org/browse/SEAMVALIDATE-7
>             Project: Seam Validation
>          Issue Type: Bug
>    Affects Versions: 3.0.0.CR1
>            Reporter: Jozef Hartinger
>            Assignee: Gunnar Morling
>            Priority: Critical
>             Fix For: 3.0.0.Final
>
>
> The current way of creating a ConstraintValidator instance is not correct:
> {code}
> AnnotatedType<T> type = beanManager.createAnnotatedType(key);
> InjectionTarget<T> it = beanManager.createInjectionTarget(type);
> CreationalContext<T> ctx = beanManager.createCreationalContext(null);
> T instance = it.produce(ctx);
> it.inject(instance, ctx);
> it.postConstruct(instance);
> return instance;
> {code}
> Since an AnnotatedType and InjectionTarget are created for every ConstraintValidator creation, it is not possible for a thirdparty extension to wrap these metadata during container startup. The correct way of creating a ConstraintValidator instance would be:
> {code}
> Set<Bean<?>> beans = beanManager.getBeans(key);
> Bean<?> bean = beanManager.resolve(beans);
> CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
> return (T) beanManager.getReference(bean, key, ctx);
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the seam-issues mailing list