Skip to main content

CDI producer field question

5 replies [Last post]
ljnelson
Offline
Joined: 2003-08-04
Points: 0

CDI has the notion of producer fields.

A producer field is one that, as I understand it, can be used to bridge the worlds of CDI dependency injection and Java EE dependency injection.

I have a @SessionScoped object that contains an injection point. I want that injection point to receive the results of a producer field located in another class.

So:

<br />
// inside session scoped object<br />
@Inject<br />
private Frobnicator frobnicator;<br />

...and:
<br />
// somewhere else<br />
public class FrobnicatorProducer {<br />
  @Produces<br />
  @EJB<br />
  private Frobnicator frobnicator;<br />
}<br />

In Glassfish 3.1 b09, deploying this combination results in Weld telling me that the injection point has several potential injections, and so deployment fails.

The possible injections are listed as the session bean itself as well as the bean produced by the producer field.

So what is the proper way for declaring a producer field in such a way that the @EJB it is "producing" is actually the only possible injection for a given injection point?

Weld's documentation has only this to say on the subject: http://docs.jboss.org/weld/reference/1.0.0/en-US/html/resources.html#d0e...

Note that the only @EJB example here uses ejbLink, which I neither understand nor want to use.

Thanks,
Laird

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
hwellmann
Offline
Joined: 2008-08-16
Points: 0

There's an interesting thread on this subject:

http://seamframework.org/Community/InjectingWithEJBOrInject

The gist of it: Assuming your EJB is a local (stateless?) session bean in a CDI enabled archive (with a beans.xml) file, CDI is smart enough to resolve the @Injection without additional help. Only for remote beans it cannot know where to look, so in that case you need an @EJB producer field (with an explicit bean name).

I would expect your example to work if you simply delete the FrobnicateProducer (or at least the @Produces annotation).

At any rate, I can confirm that I'm able to @Inject local SLSBs without problems in Glassfish 3.0.1b21. (3.0 had a visibility bug when working with multiple JARs embedded into a WAR).

Regards,

Harald

ljnelson
Offline
Joined: 2003-08-04
Points: 0

> There's an interesting thread on this subject:
>
> http://seamframework.org/Community/InjectingWithEJBOrI
> nject

Yep, was reading that.

> The gist of it: Assuming your EJB is a local
> (stateless?) session bean in a CDI enabled archive
> (with a beans.xml) file, CDI is smart enough to
> resolve the @Injection without additional help.

Oh! {blink blink} Looks like http://seamframework.org/Community/InjectingWithEJBOrInject#comment108110 went right by me. I was looking at the Weld documentation instead of the spec, and it seemed to imply that you had to do the producer field bridge even in these cases. Serves me right. Thanks!

Best,
Laird

ljnelson
Offline
Joined: 2003-08-04
Points: 0

And this:

http://seamframework.org/Community/InjectingWithEJBOrInject#comment108140

...was implying that I had to use producer fields in all cases. Guess not!

Thanks,
Laird

szczyp

If you do, however, have a use case that you have a class that also has a producer of its default instances:

[code]
public class SomeClass {

@Produces
public static SomeClass produce() {
// whatever, some init that doesn't work well in constructors (see http://seamframework.org/Community/NormalscopedBeansAndConstructors)
}
}
[/code]

you would also get the ambiguity error. What you can do is:
a) introduce a constructor so that there is no default one, and no constructor is annotated @Inject - this makes the class violate the bean contract, and will not be discovered as bean; it producer, however, will be and as it is static, it can be called; I think that a private default constructor would also do the trick
b) you can write a portable extension that vetoes this particular bean class during bean discovery; you have experience with portable extensions in glassfish, judging by some other post on this forums ;d

ljnelson
Offline
Joined: 2003-08-04
Points: 0

My equivalent posting on the incredibly barren Weld-Users list is here: http://seamframework.org/Community/InjectingEJBReferencesFails