Offering beans post container initialization?

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Offering beans post container initialization?

aalmiray
Hi guys,

I wonder if it's possible to offer a set of bean instances (not-managed by the container) so that their dependencies are satisfied by the container. As an example, you can grab a Spring BeanFactory and call autowire on it, like this

   app.applicationContext.getAutowireCapableBeanFactory()
         .autowireBeanProperties(instance, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)

Is there a similar API in Weld/CDI?

TIA
Andres
Reply | Threaded
Open this post in threaded view
|

Re: Offering beans post container initialization?

Marius Bogoevici-2
Andres,

You may want to look at this:

http://seamframework.org/Documentation/HowDoIDoNoncontextualInjectionForAThirdpartyFramework

HTH,
Marius

aalmiray wrote:

> Hi guys,
>
> I wonder if it's possible to offer a set of bean instances (not-managed by
> the container) so that their dependencies are satisfied by the container. As
> an example, you can grab a Spring BeanFactory and call autowire on it, like
> this
>
>     app.applicationContext.getAutowireCapableBeanFactory()
>           .autowireBeanProperties(instance,
> AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)
>
> Is there a similar API in Weld/CDI?
>
> TIA
> Andres
>
> --
> View this message in context: http://weld-development-discussions.46994.n3.nabble.com/Offering-beans-post-container-initialization-tp2656992p2656992.html
> Sent from the Weld development discussions mailing list archive at Nabble.com.
> _______________________________________________
> weld-dev mailing list
> [hidden email]
> https://lists.jboss.org/mailman/listinfo/weld-dev
>    
_______________________________________________
weld-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/weld-dev
Reply | Threaded
Open this post in threaded view
|

Re: Offering beans post container initialization?

mojavelinux
In reply to this post by aalmiray
Yes. What you want to accomplish is called "injecting into a non-contextual instance", or in other words "injecting into a non-bean instance". The object is an injection target.


Here's the basic idea.

1. Obtain the BeanManager
2. Create a CreationalContext, a context that supports the injection process
3. Create an AnnotatedType for the class of the object receiving the injections
4. Create an InjectionTarget from that AnnotatedType
5. Use InjectionTarget.inject(Object, BeanManager) to satisfy the injection points on the non-managed object

This operation is performed, amongst other places, in Arquillian's CDI test enricher. It's how Arquillian takes the test case instance and satisfies injection points.

-Dan

On Wed, Mar 9, 2011 at 15:02, aalmiray <[hidden email]> wrote:
Hi guys,

I wonder if it's possible to offer a set of bean instances (not-managed by
the container) so that their dependencies are satisfied by the container. As
an example, you can grab a Spring BeanFactory and call autowire on it, like
this

  app.applicationContext.getAutowireCapableBeanFactory()
        .autowireBeanProperties(instance,
AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false)

Is there a similar API in Weld/CDI?

TIA
Andres

--
View this message in context: http://weld-development-discussions.46994.n3.nabble.com/Offering-beans-post-container-initialization-tp2656992p2656992.html
Sent from the Weld development discussions mailing list archive at Nabble.com.
_______________________________________________
weld-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/weld-dev



--
Dan Allen
Principal Software Engineer, Red Hat | Author of Seam in Action
Registered Linux User #231597



_______________________________________________
weld-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/weld-dev
Reply | Threaded
Open this post in threaded view
|

Re: Offering beans post container initialization?

mojavelinux
On Wed, Mar 9, 2011 at 15:14, Dan Allen <[hidden email]> wrote:
Yes. What you want to accomplish is called "injecting into a non-contextual instance", or in other words "injecting into a non-bean instance". The object is an injection target.

I meant: in other words, "injecting into a non-managed instance".  

--
Dan Allen
Principal Software Engineer, Red Hat | Author of Seam in Action
Registered Linux User #231597



_______________________________________________
weld-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/weld-dev
Reply | Threaded
Open this post in threaded view
|

Re: Offering beans post container initialization?

mojavelinux
Also forgot to attach the code snippet:

  protected void injectNonContextualInstance(BeanManager manager, Object instance)
   {
      CreationalContext<Object> creationalContext =  manager.createCreationalContext(null);
      InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) manager.createInjectionTarget(
            manager.createAnnotatedType(instance.getClass()));
      injectionTarget.inject(instance, creationalContext);
   }

-Dan

On Wed, Mar 9, 2011 at 15:15, Dan Allen <[hidden email]> wrote:
On Wed, Mar 9, 2011 at 15:14, Dan Allen <[hidden email]> wrote:
Yes. What you want to accomplish is called "injecting into a non-contextual instance", or in other words "injecting into a non-bean instance". The object is an injection target.

I meant: in other words, "injecting into a non-managed instance".  

--
Dan Allen
Principal Software Engineer, Red Hat | Author of Seam in Action
Registered Linux User #231597





--
Dan Allen
Principal Software Engineer, Red Hat | Author of Seam in Action
Registered Linux User #231597



_______________________________________________
weld-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/weld-dev
Reply | Threaded
Open this post in threaded view
|

Re: Offering beans post container initialization?

aalmiray
In reply to this post by Marius Bogoevici-2
Nice! Got it running once I figured out I had to explicitly call inject() and postConstruct() once the bean instance was setup with NonContextual.Instance.

Now all I have to figure out is when exactly an instance must be destroyed/disposed and this puppy is good to go.

Btw, all this work is for a Weld Griffon plugin ;-)

Thanks!
Andres
Reply | Threaded
Open this post in threaded view
|

Re: Offering beans post container initialization?

mojavelinux
On Wed, Mar 9, 2011 at 15:45, aalmiray <[hidden email]> wrote:
Nice! Got it running once I figured out I had to explicitly call inject() and
postConstruct() once the bean instance was setup with
NonContextual.Instance.

Now all I have to figure out is when exactly an instance must be
destroyed/disposed and this puppy is good to go.

Btw, all this work is for a Weld Griffon plugin ;-)

That's awesome. It's exciting to see another use of Weld (and CDI) outside of Java EE. Thanks for branching us out!

I'm curious whether you think certain Seam modules would be relevant/useful within Griffon now that you (almost) have support for CDI in place? It would certainly help challenge some of the assumptions we make in the Seam modules and ultimately make them generally more applicable and robust.


-Dan

--
Dan Allen
Principal Software Engineer, Red Hat | Author of Seam in Action
Registered Linux User #231597



_______________________________________________
weld-dev mailing list
[hidden email]
https://lists.jboss.org/mailman/listinfo/weld-dev
Reply | Threaded
Open this post in threaded view
|

Re: Offering beans post container initialization?

aalmiray
mojavelinux wrote
That's awesome. It's exciting to see another use of Weld (and CDI) outside
of Java EE. Thanks for branching us out!
Here's the current code in case you're interested

    http://svn.codehaus.org/griffon/plugins/griffon-weld/trunk/

The plugin will be released as soon as Griffon 0.9.2 is out (a few weeks from now) due to some changes needed in core to better support Weld. However once the plugin is out you'll be able to launch a Griffon app and have any dependencies on its MVC group members resolved by Weld. Also, any other beans registered in the usual way will be active.

mojavelinux wrote
I'm curious whether you think certain Seam modules would be relevant/useful
within Griffon now that you (almost) have support for CDI in place? It would
certainly help challenge some of the assumptions we make in the Seam modules
and ultimately make them generally more applicable and robust.

http://github.com/seam and http://seamframework.org/Seam3
So which Seam modules exactly can be used in a non JEE environment? I thought Seam was all about webapps and JEE but clearly my understandings of Seam and Weld were completely mistaken.

Cheers,
Andres