4 Replies Latest reply on Mar 11, 2013 8:25 AM by crinaldi

    @Produces and @IOCProvider Fail Injection

    crinaldi Newbie

      I'm developing with Errai 2.2.0.Final and I need to make a few providers.

      I made the example of the theory:

       

      1) ---- Provider -----

      public interface TimeService {

        public String getTime();

      }

       

       

      @IOCProvider

      @Singleton

      public class TimeServiceProvider implements Provider<TimeService> {

        @Override

        public TimeService get() {

          return new TimeService() {

            public String getTime() {

              return "It's midnight somewhere!";

            }

          };

        }

      }

       

      Then inject it as follows in my application:

       

      @EntryPoint

      public class App {

       

          @Inject

          private Instance<TimeService> time;

       

         @PostConstruct

          public void init() {

               TimeService a = Preconditions.checkNotNull(time.get(), "TimeService is Null");

               GWT.log(a.getTime());

          }

      }

       

      This gives the following error in Development Mode:

       

      java.lang.RuntimeException: critical error in IOC container bootstrap

      at org.jboss.errai.ioc.client.Container.bootstrapContainer(Container.java:69)

      at org.jboss.errai.ioc.client.Container.onModuleLoad(Container.java:34)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

      at java.lang.reflect.Method.invoke(Method.java:601)

      at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)

      at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)

      at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:525)

      at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)

      at java.lang.Thread.run(Thread.java:722)

       

      Caused by: org.jboss.errai.ioc.client.container.IOCResolutionException: no matching bean instances for: com.logikas.samples.errai.client.presenter.configure.TimeService

      at org.jboss.errai.ioc.client.container.IOCBeanManager.lookupBean(IOCBeanManager.java:475)

      at org.jboss.errai.enterprise.client.cdi.InstanceProvider$InstanceImpl.get(InstanceProvider.java:86)

      at com.logikas.samples.errai.client.local.App.init(App.java:32)

       

       

      2) -- @Produces ------

      I also tried with @ Produces as follows:

       

       

      @Produces

      @Singleton

      public EventBus getEventBus(){

            return new SimpleEventBus();

      }

       

      But when the EventBus injected, can not find the reference.

       

      ....

      .....

         @Inject

         private Provider<EventBus> bus;

       

         @PostConstruct

         private void init(){

             

              EventBus b = Preconditions.checkNotNull(bus.get(), "Event Buss is Null");

         }

       

       

      java.lang.RuntimeException: critical error in IOC container bootstrap

      at org.jboss.errai.ioc.client.Container.bootstrapContainer(Container.java:69)

      at org.jboss.errai.ioc.client.Container.onModuleLoad(Container.java:34)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

      at java.lang.reflect.Method.invoke(Method.java:601)

      at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)

      at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)

      at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:525)

      at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)

      at java.lang.Thread.run(Thread.java:722)

       

      Caused by: org.jboss.errai.ioc.client.container.IOCResolutionException: no matching bean instances for: com.google.gwt.event.shared.EventBus

      at org.jboss.errai.ioc.client.container.IOCBeanManager.lookupBean(IOCBeanManager.java:475)

       

      Thanks for advance!!!!

       

       

       

        • 1. Re: @Produces and @IOCProvider Fail Injection
          Mike Brock Master

          Can you show a complete paste of the entire TimeServiceProvider by any chance? 

          • 2. Re: @Produces and @IOCProvider Fail Injection
            crinaldi Newbie

            Mike, thank for answering..

             

            Here the complete code:

             

            //Interface TimeService

            public interface TimeService {

                public String getTime();

            }

             

            ------------------------------------------

            import com.logikas.samples.errai.client.presenter.configure.TimeService;

            import javax.inject.Provider;

            import javax.inject.Singleton;

            import org.jboss.errai.ioc.client.api.IOCProvider;

             

             

            /**

            * Description of TimeServiceProvider

            *

            * @see TimeService

            * @author Cristian Rinaldi <csrinaldi@gmail.com>

            */

            @IOCProvider

            @Singleton

            public class TimeServiceProvider implements Provider<TimeService> {

             

                @Override

                public TimeService get() {

                   return new TimeService() {

                        public String getTime() {

                           return "Is time!!!!!!!!";

                       }

                    };

               }

            }

             

            --------------------------------------------------------------------

             

            import com.google.common.base.Preconditions;

            import com.google.gwt.core.client.GWT;

            import com.logikas.samples.errai.client.presenter.configure.TimeService;

            import javax.annotation.PostConstruct;

            import javax.inject.Inject;

            import javax.inject.Provider;

             

             

            import org.jboss.errai.ioc.client.api.EntryPoint;

             

             

            /**

            * Description of App

            *

            * @author Cristian Rinaldi <csrinaldi@gmail.com>

            */

             

            @EntryPoint

            public class App {

             

                @Inject

                private Provider<TimeService> timeService;

             

                @PostConstruct

                public void init() {

                    TimeService tm = Preconditions.checkNotNull(timeService.get(), "Time Service is null");

                    GWT.log(tm.getTime());

                }

            }

             

            This is the error in compilation time:

            Generating Extensions Bootstrapper...

            generating IOC bootstrapping class...

            reachability analysis disabled. errai may generate unnecessary code.

            enable reachability analysis with -Derrai.compile.perf.perform_reachability_analysis=true

                           Checking ErraiApp.properties for configured types ...

            org.jboss.errai.ioc.rebind.ioc.exception.UnsatisfiedDependenciesException:  @> com.logikas.samples.errai.client.local.App

            - field org.jboss.errai.codegen.meta.MetaField:com.logikas.samples.errai.client.local.App.timeService could not be satisfied for type: javax.inject.Provider

              Message: ambiguous injection type (multiple injectors resolved): javax.inject.Provider  @Any  @Default :

                 matching> org.jboss.errai.ioc.rebind.ioc.injector.TypeInjector:com.logikas.samples.errai.client.provider.TimeServiceProvider  @Any  @Default

                 matching> org.jboss.errai.ioc.rebind.ioc.injector.ContextualProviderInjector:javax.enterprise.inject.Instance  @Any  @Default

              Note: configure an alternative to take precedence or remove all but one matching bean.

             

             

                      at org.jboss.errai.ioc.rebind.ioc.exception.UnsatisfiedDependenciesException.createWithSingleFieldFailure(UnsatisfiedDependenciesException.java:39)

                      at org.jboss.errai.ioc.rebind.ioc.injector.api.InjectionTask.doTask(InjectionTask.java:119)

                      at org.jboss.errai.ioc.rebind.ioc.injector.InjectUtil.handleInjectionTasks(InjectUtil.java:160)

                      at org.jboss.errai.ioc.rebind.ioc.injector.InjectUtil.access$000(InjectUtil.java:70)

                      at org.jboss.errai.ioc.rebind.ioc.injector.InjectUtil$2.generateConstructor(InjectUtil.java:148)

                      at org.jboss.errai.ioc.rebind.ioc.injector.TypeInjector._getType(TypeInjector.java:160)

                      at org.jboss.errai.ioc.rebind.ioc.injector.TypeInjector.getBeanInstance(TypeInjector.java:109)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCProcessorFactory$4.handle(IOCProcessorFactory.java:377)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCProcessorFactory$6.process(IOCProcessorFactory.java:593)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCProcessorFactory.process(IOCProcessorFactory.java:543)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCBootstrapGenerator.generateExtensions(IOCBootstrapGenerator.java:354)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCBootstrapGenerator._generate(IOCBootstrapGenerator.java:328)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCBootstrapGenerator.generate(IOCBootstrapGenerator.java:157)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator.generateIOCBootstrapClass(IOCGenerator.java:96)

                      at org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCGenerator.generate(IOCGenerator.java:65)

                      at com.google.gwt.core.ext.GeneratorExtWrapper.generate(GeneratorExtWrapper.java:48)

                      at com.google.gwt.core.ext.GeneratorExtWrapper.generateIncrementally(GeneratorExtWrapper.java:60)

                      at com.google.gwt.dev.javac.StandardGeneratorContext.runGeneratorIncrementally(StandardGeneratorContext.java:647)

                      at com.google.gwt.dev.cfg.RuleGenerateWith.realize(RuleGenerateWith.java:41)

                      at com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.rebind(StandardRebindOracle.java:78)

                           [ERROR] Error generating extensions

            org.jboss.errai.ioc.rebind.ioc.exception.UnsatisfiedDependenciesException:  @> com.logikas.samples.errai.client.local.App

            - field org.jboss.errai.codegen.meta.MetaField:com.logikas.samples.errai.client.local.App.timeService could not be satisfied for type: javax.inject.Provider

              Message: ambiguous injection type (multiple injectors resolved): javax.inject.Provider  @Any  @Default :

                 matching> org.jboss.errai.ioc.rebind.ioc.injector.TypeInjector:com.logikas.samples.errai.client.provider.TimeServiceProvider  @Any  @Default

                 matching> org.jboss.errai.ioc.rebind.ioc.injector.ContextualProviderInjector:javax.enterprise.inject.Instance  @Any  @Default

              Note: configure an alternative to take precedence or remove all but one matching bean.

             

            ----------------------------------------------------------------------------------------------------------------------------

            Other problem is with injection of EventBus. But for this, I did not make a Provider, only anotate the method with @Produces, but in the moment of injection, no find of implementation. The build process finishes successfully, the error occurs in Development Mode when the application loads. EventBus is not a Interface is a concrete class.

            Below there some source code, the exemplifies  what I mean:

             

            import com.google.gwt.event.shared.EventBus;

            import com.google.gwt.event.shared.SimpleEventBus;

            import javax.annotation.PostConstruct;

            import javax.enterprise.inject.Produces;

            import javax.inject.Singleton;

             

             

            /**

            * Description of MVPInitializer

            * @author Cristian Rinaldi <csrinaldi@gmail.com>

            */

            @Singleton

            public class MVPInitializer {

              

                @Produces

                @Singleton

                public EventBus getEventBus(){

                    return new SimpleEventBus();

                }

             

                @PostConstruct

                private void init(){

                  //TODO       

                }

            }


            -------------------------------------

            import com.google.common.base.Preconditions;

            import com.google.gwt.core.client.GWT;

            import com.google.gwt.event.shared.EventBus;

            import javax.annotation.PostConstruct;

            import javax.inject.Inject;

            import javax.inject.Provider;

             

             

            import org.jboss.errai.ioc.client.api.EntryPoint;

             

             

            /**

            * Description of App

            *

            * @author Cristian Rinaldi <csrinaldi@gmail.com>

            */

             

             

            @EntryPoint

            public class App {

             

                @Inject

                private Provider<EventBus> eventBus;

             

                @PostConstruct

                public void init() {

                    EventBus eb = Preconditions.checkNotNull(eventBus.get(), "EventBus is null");

                    GWT.log(eb.toString());

               }

            }

             

            The Error:

            java.lang.RuntimeException: critical error in IOC container bootstrap

            at org.jboss.errai.ioc.client.Container.bootstrapContainer(Container.java:69)

            at org.jboss.errai.ioc.client.Container.onModuleLoad(Container.java:34)

            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

            at java.lang.reflect.Method.invoke(Method.java:601)

            at com.google.gwt.dev.shell.ModuleSpace.onLoad(ModuleSpace.java:396)

            at com.google.gwt.dev.shell.OophmSessionHandler.loadModule(OophmSessionHandler.java:200)

            at com.google.gwt.dev.shell.BrowserChannelServer.processConnection(BrowserChannelServer.java:525)

            at com.google.gwt.dev.shell.BrowserChannelServer.run(BrowserChannelServer.java:363)

            at java.lang.Thread.run(Thread.java:722)

             

            Caused by: org.jboss.errai.ioc.client.container.IOCResolutionException: no matching bean instances for: com.google.gwt.event.shared.EventBus

            at org.jboss.errai.ioc.client.container.IOCBeanManager.lookupBean(IOCBeanManager.java:475)

            at org.jboss.errai.enterprise.client.cdi.InstanceProvider$InstanceImpl.get(InstanceProvider.java:86)

            at com.logikas.samples.errai.client.local.App.init(App.java:27)

             

             

            Thanks a lot!!

            • 3. Re: @Produces and @IOCProvider Fail Injection
              Mike Brock Master

              The problem as I see it is you're trying to inject the Provider directly. While a top-level provider is wired like a regular bean, it's outside of the regular creational context. Have you tried just directly injecting EventBus?

               

              ie. @Inject EventBus eventBus; into your App class? The bean manager will take care of calling provide() for you.

              • 4. Re: @Produces and @IOCProvider Fail Injection
                crinaldi Newbie

                Mike, thanks for response....!!

                I try with @Inject EventBus and work!!!!!

                But my question is:

                   It is conceptually wrong to inject a Provider of the way I did?  or this is a bug and will be covered?

                 

                Mike, I take this to say:

                  The Framework that are developing is AWESOME!

                 

                Congratulations!!! and Thanks!!!