6 Replies Latest reply on Mar 13, 2013 5:11 PM by shadogray

    Can't Use CDI In My View Class

    chaluwa

      Got a view class (SchoolManager) that does not extend or implement anything (just a plain class), but is composed of UI component classes. I noticed that I can't annotate it with @Singleton, because I get "the dreaded" IOC Bootstraper deferred binding error (that I've always complained about in this forum) when I do. However, this error does not show up at build / compile time, but when I try to see changes in Dev mode.

       

      Secondly, I get another error when I attempt to inject a Caller<T> object in the same class. Simply placing @Inject Caller<SchoolService> into the class causes errors.

      I want my view classes to be singletons and I need to invoke remote methods to create a new School, as well as fetch the ones existing. Am I missing somthing.

        • 1. Re: Can't Use CDI In My View Class
          shadogray

          I'm not really sure, but I assume that it doesn't make sense to set @Singleton on a GWT "view" (see http://stackoverflow.com/questions/6382473/singleton-in-gwt-project)

          On the client it seems to be quite useless. So perhaps it's worth trying w/o @Singleton?

          • 2. Re: Can't Use CDI In My View Class
            mdhirsch30345

            I have plenty of UI classes that are @Singletons, but all of them extend Composite since I want to use them in my display.  You might try making your class extend composite or Widget.  I'm not sure that you can display a non-widget class at all, anyway.

            • 3. Re: Can't Use CDI In My View Class
              chaluwa

              Thanks Michael. So what exactly does the @Singleton annotation mean / do to a UI class? even one that extends Composite.

              • 4. Re: Can't Use CDI In My View Class
                mdhirsch30345

                I use it when I have a widget that I want to display, but no matter how it is displayed I want to show the same instance of the widget.  Like, perhaps, a settings panel.  You don't need two different settings panels, so you create a Singleton.  Any class that wants to display the settings panel would create an instance, like

                @Inject

                Instance<SettingsPanel> spInst;

                 

                Then later when you want to display it:

                spInst.get().display()

                 

                So the panel lies hidden until requested, and whenever it is requested the same object is displayed.

                • 5. Re: Can't Use CDI In My View Class
                  chaluwa

                  Thanks Michael, am grateful. I have @Inject private Caller<MyRpcService> cloud; in the view class that I want to annotate with @Singleton (or @ApplicationScoped if its the right / better way) but I am getting a null pointer exception at the point where I try to do cloud.call(...), meaning the Caller object is not been injected.

                   

                  Do I need to annotate a view class (with @Singleton or @ApplicationScoped, or whatever) to correctly inject a Caller, and why is my Caller object still null. Am also confused which import to use for the @Singleton (javax.ejb or javax.inject) and @ApplicationScoped (javax.enterprise.context or javax.ejb) annotations since I see several.

                   

                  Right now, since my Caller object is null, am using the MessageBuilder API. Thanks for help in advance.

                  • 6. Re: Can't Use CDI In My View Class
                    shadogray

                    You are right, these annotations have become a mess..

                    But as a rule of thumb: do not use the javax.ejb.* annotations, its javax.enterprise.context.ApplicationScoped and javax.inject.Singleton

                    You can see this e.g. in https://github.com/errai/errai/blob/master/errai-ioc/src/main/java/org/jboss/errai/ioc/rebind/ioc/bootstrapper/IOCBootstrapGenerator.java

                     

                    If your injection targets are null, please check how you instantiate the class. If you call the constructor youself, injection will _not_ happen.

                    Injection only happens, if you get the objects by calls to the CDI framework!

                     

                    Such cases are e.g.: @EntryPoint, @Page if using transitions, @Inject on fields of CDI-aware objects, @Inject Instance<T> xxx,

                    It is easy to overlook a selfinstantiating constructor, which will produce a non-CDI-aware object.