[cdi-dev] Extensions and spec-related observer method injection points question

Antoine Sabot-Durand antoine at sabot-durand.net
Fri Feb 17 12:30:05 EST 2017


Check https://issues.jboss.org/browse/WELD-2338

Regarding Extension being beans, it's the case, but rather special beans
since you can't perform field, constructor or initializer injection in them.
For observer that might be called during bootstrap (observing Object for
instance) they will throw exception if they inject parameters as well since
their matching beans are not resolvable yet.
A good practice could be: use observers with personal payload without
parameter injection and only for specific operation during bootstrap
(Romain wrote an interesting post on the topic:
https://blog-rmannibucau.rhcloud.com/#/post/share-data-in-cdi-extensions)
Create a specific bean to declare your observers and inject the extension
and other beans as parameter.

If you really want to have your observers in your extension, inject
BeanManager and perform the resolution with it...

Antoine




On Fri, Feb 17, 2017 at 5:56 PM Laird Nelson <ljnelson at gmail.com> wrote:

> On Thu, Feb 16, 2017 at 10:08 PM Matej Novotny <manovotn at redhat.com>
> wrote:
>
> > Concretely, I'd like to do this:
> >
> > // In my portable extension
> > private static final void doSomethingAtStartup(@Observes
> > @Initialized(ApplicationScoped.class) final Object event, final
> Frobnicator
> > someBean) {
> > someBean.doSomething();
> > }
>
> While you cannot do this, you can still get hold of BeanManager and use it
> to resolve your bean.
>
>
> Right; that's what I'm doing at the moment.
>
>
> But I would say this could create quite some confusion if in some observer
> you could inject certain beans and in others you couldn't.
>
>
> Sure.
>
>
> Even in your sample, you can only inject AppScoped beans, so imagine you
> do such observer for, say, SessionScoped, what can you inject there?
> SessionScoped for sure, how about Req? Conversation?
>
>
> First, thanks to Martin, Antoine, yourself, etc. for looking at this.  The
> issue is not holding me up, and I can make headway, etc.  My remarks here
> are just because I'm curious.  :-)
>
> Forgetting about confusion, couldn't I inject any bean I want in a
> non-container-lifecycle-event-observing observer method in my extension?  I
> understand it's not supported right now, and I also understand Antoine's
> excellent point that observing a payload of type Object would, in a
> portable extension, be too "broad".  Let's forget about that for a moment,
> and pretend that it's not an issue, and just focus on resolution issues.
>
> Surely this:
>
> void MyObserver(@Observes Object payload, SomeBean bean) { ... }
>
> ...would work (again, forgetting about the fact that due to resolution
> rules this would also be called for lifecycle events; I'm interested purely
> in resolution issues of that SomeBean parameter)?
>
> My point is, a portable extension "becomes" a bean (in application scope,
> according to the specification), and so therefore is eligible to use other
> beans, no matter what scope they're in, some of whose Contexts might be
> active, and some of whose might not be active.  Obviously trying to use a
> bean before the container has gotten through AfterDeploymentValidation
> won't work, but after that point...?
>
> Like I said, now I'm just curious.  :-)
>
> Best,
> Laird
> _______________________________________________
> cdi-dev mailing list
> cdi-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/cdi-dev
>
> Note that for all code provided on this list, the provider licenses the
> code under the Apache License, Version 2 (
> http://www.apache.org/licenses/LICENSE-2.0.html). For all other ideas
> provided on this list, the provider waives all patent and other
> intellectual property rights inherent in such information.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/cdi-dev/attachments/20170217/044f7b1c/attachment-0001.html 


More information about the cdi-dev mailing list