6 Replies Latest reply on Feb 28, 2009 11:06 PM by sherkan777

    Component.getInstance(...) problem

    sherkan777

      Hi,
      I try to get APLICATION scope component instance from conversation scope another component by:


      Component.getInstance



      Is this possible?


      Here is my STSB


      @Stateful
      @Scope(ScopeType.APPLICATION)
      @Name("sguniNewsAction")



      and conversation bean



      SGUniNewsAction action = (SGUniNewsAction)Component.getInstance(SGUniNewsAction.class, false);
      action.loadHotNews();



      after that I get



      Class cast exception error




        • 1. Re: Component.getInstance(...) problem
          swd847

          if SGUniNewsAction is the bean class, you need to use the local interface in your code. Post the bean src and I will give an example if you are still hacing trouble.

          • 2. Re: Component.getInstance(...) problem
            sherkan777

            Jep, this is STSB


            My bean which I want to get:


            @Stateful
            @Scope(ScopeType.APPLICATION)
            @Name("sguniNewsAction")
            public class SGUniNewsAction extends GameSystem implements SGUniNewsConsole {
            
            .....
            
                    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
                    public void loadHotNews() {...}
            }
             



            My bean from which i want to get SGUniNewsAction:


            @Stateful
            @Scope(ScopeType.CONVERSATION)
            @Restrict("#{s:hasRole('moderator')}") 
            @Name("newsAction")
            public class NewsAction extends PersonSystem implements NewsConsole {
            .....
            
            
            @End
            @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
            public void addNews() { 
            ....
            SGUniNewsAction action = (SGUniNewsAction)Component.getInstance(SGUniNewsAction.class, false);
            action.loadHotNews();
            }


            • 3. Re: Component.getInstance(...) problem
              swd847

              This is what you want:


              SGUniNewsConsole action = (SGUniNewsConsole)Component.getInstance(SGUniNewsAction.class, false);
              
              



              With ejbs you always reference them through the Local or Remote interface, this is because the object that is returned is not an SGUniNewsAction, but a proxy that implements SGUniNewsConsole and wraps a SGUniNewsAction instance, with several layers of interceptors in between (this is how it provides all those neat things such as dependency injection and container managed transactions).

              • 4. Re: Component.getInstance(...) problem
                sherkan777

                hmmmm, so...how can I get it if this is proxy?


                Can u show me pls, a small example?

                • 5. Re: Component.getInstance(...) problem
                  swd847

                  You call it like this:


                  SGUniNewsConsole action = (SGUniNewsConsole)Component.getInstance("sguniNewsAction", false);
                  
                  


                  • 6. Re: Component.getInstance(...) problem
                    sherkan777

                    Thanks Stuart, you got beer! :)