users@glassfish.java.net

Re: Weld/CDI issues with ManagedBeans, _at_Produces methods.

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 05 Jan 2010 13:13:26 +0100

On Jan 5, 2010, at 12:58 PM, glassfish_at_javadesktop.org wrote:

> Hi.
>
> I'm attempting to get CDI working in a webapp in glassfish v3.
> Basic @Inject injection and activating alternatives are working, but
> any @ManagedBean that is injected with an object produced by a
> @Produces method is failing.
>
> Consider the following code for the producer method:
>
> @ManagedBean("Producer")
> public class Producer {
>
> private Platform platform;
>
> @PostConstruct
> public void postConstruct() {
> platform = getPlatform();
> }
>
> /** Identify platform using database. */
> private Platform getPlatform() {...}
>
> @Produces
> public FirewallService getFirewallService() {
> return new FirewallService(platform);
> }
> }
>
> and the following code for the bean being injected with the object
> being produced:
>
> @ManagedBean("ConfigurationBean")
> public class ConfigurationBean extends AbstractBean<Configuration> {
>
> @Inject
> private FirewallService firewallService;
> ...
> }
>
> I'm using JNDI from my JAX-RS resources to obtain references to the
> @ManagedBeans.

It is not clear from your email whether this is only an issue with
JNDI look up, is that the case? for example does the reference to
FirewallService get injected into the instance of ConfigurationBean?

Note that you should be able to inject onto JAX-RS resources if you
annotate the resource class with a CDI scope-based annotation, such as
@RequestScoped, or @ManagedBean (meaning Jersey will defer to GF to
instantiate but will manage the instance, by default in the pre-
request scope). There is a restriction, however, constructor injection
of JAX-RS references, such as UriInfo, are not currently supported for
CDI constructed or managed beans.

Paul.


> It is successful for any @ManagedBean without injection, with
> injection and inject with alternatives activated. When I request a
> @ManagedBean injected using a producer method, I only get the
> following error:
>
> javax.naming.NamingException: Lookup failed for 'java:module/
> ConfigurationBean' in SerialContext [Root exception is
> javax.naming.NameNotFoundException: No object bound to name
> java:module/ConfigurationBean]
> at
> com
> .sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:
> 442)
>
> I get no error from Weld. Under debugging I can see that the
> producer method is being called. Also, I can introspect the JNDI
> context when debugging and see the 'java:module/ConfigurationBean'
> name when listing the names available under the 'java:module' context.
>
> While I can see how to get this working now using alternatives, I
> may want to use @Produces methods in the future.
> [Message sent by forum member 'beamso' (beamso_at_gmail.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=378783
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>