users@glassfish.java.net

Re: Using CDI inside an OSGi bundle

From: Sahoo <sanjeeb.sahoo_at_oracle.com>
Date: Thu, 22 Sep 2011 08:02:25 +0530

How are you instantiating MyTestClass? If you create your own instance
by calling new MyTestClass, then the injection manager can't inject the
component. If you do:

@Inject MyTestClass myTestClass;

then the injected myTestClass should have myBean field properly injected.

Sahoo
On Wednesday 21 September 2011 09:42 PM, Robert Weeks wrote:
> Hello -
>
> I am having a few issues and was hoping for some insight.
>
> If I have an OSGi bundle (a jar bundle deployed via /autodeploy/bundles), and within I declare a bean:
>
> @Stateless
> public class MyTestBean {
> ...
>
> I can see this via normal @EJB injection (within the same bundle) if using a container controlled class such as an MDB (@MessageDriven).
>
> But - if I am wanting to use CDI to be able to inject these resources into other classes that are not container controlled - such as:
>
> public class MyTestClass {
> @Inject
> MyTestBean myBean;
>
> shouldn't I be able to do this outside of container controlled classes within the bundle?
>
> I have set the 'beans.xml' inside the META-INF folder within the bundle jar.
>
> I can see this work in the controlled classes such as the MDB if I replace the @EJB MyTestBean with @Inject MyTestBean - but I cannot get a hold of it in any of my other POJOs.
>
> Is this how it is supposed to work? Are we not able to see these injections either within a bundle?
>
> Thanks for any thoughts.
>
>
> Robert Weeks
> Lead Developer - Framework and UI
> EXTENSION, INC.
> Email: rweeks_at_ext-inc.com
> Office: 260-797-0200 x4228
> General: 877-207-3753
> www.OpenTheRedBox.com
>
> Download the FREE EXTENSION Mobile App for iPhone and iPod Touch
> http://www.opentheredbox.com/iPhone_appDemo.php
>
> Download the FREE EXTENSION Mobile App for Android-Based Devices
> https://market.android.com/details?id=com.ext_inc.android&feature=search_result
>
> We're Exhibiting in an Area Near You
> http://www.opentheredbox.com/events.php
>