Announcement Announcement Module
Collapse
No announcement yet.
getting bean refrence witout using WebApplicationContextUtils class Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getting bean refrence witout using WebApplicationContextUtils class

    Hi,

    mine is a web application.
    getting a bean ref. through WebApplicationContextUtils is ok.
    but how will i get a bean ref w/o using WebApplicationContextUtils coz i do nt have servletContext with me.

    actually i am implementing a ServletContextListener. so in contextIntialized method servletContext is available but in others method it is not.

    Pls, tell me any alternate way to get the bean ref

    I have searched the forums but didn't get the idea.
    regards
    Puneet Jain

  • #2
    The idea is to retrieve the context once (using the servlet context) and store it in an appropriate place to retrieve it from, afterwards.

    An elegant solution might be, to define a bean that implements ApplicationContextAware in your context. This bean might store the passed in context in a static variable and could then allow access to it with a static getter.

    Regards,
    Andreas

    Comment


    • #3
      will that mean suppose i implement the ApplicationContextAware and than whereever i want to get the bean ref from spring context i call the static method, then why to implement ApplicationcontextAware, i guess we can do it in a normal class.
      pls correct me if i am wrong in last line.

      regards
      pj

      Comment


      • #4
        Sure, you can get this done without ApplicationContextAware. In that case you have to apply WebApplicationContextUtils to retrieve the context once and assign it to a static variable.

        The approach I proposed has the advantage, that the setting of the context will be performed implicitly, since the bean is defined within the context itself. You do not need to write the initialization code yourself.

        Regards,
        Andreas

        Comment


        • #5
          i am confused noe, pls correct me .
          I have to implement the ApplicationcontextAware.
          then i have to define the implementing class in applicationcontext.xml
          and what will i have to ginve in the <ref> tag. i am not clear.

          regards
          pj

          Comment


          • #6
            It should look like this:

            The holder bean
            Code:
            public class AppCtxHolder implements ApplicationContextAware {
            
                private static ApplicationContext ctx;
            
                public AppCtxHolder() {}
                
                public void setApplicationContext(ApplicationContext applicationContext) {
                    ctx = applicationContext;
                }
                
                public static ApplicationContext getApplicationContext() {
                    return ctx;
                }
            }
            The definition
            Code:
            <beans>
                <bean id="appCtxHolder" class="xyz.AppCtxHolder"/>
            </beans>
            Usage:
            Code:
            ...
            ApplicationContext ctx = AppCtxHolder.getApplicationContext();
            // use ctx
            ...
            Hope that makes things clearer,
            Andreas

            Comment


            • #7
              Thanks very much, its clear to me now.
              but meanwhile i have read that people saying do not make ur code spring dependent n provided many alternate sol by just adding a new class inbetween.
              should i say layer of indirection solves every problem in j2ee

              Comment


              • #8
                Originally posted by puneetjains
                should i say layer of indirection solves every problem in j2ee
                Almost

                If you can achieve what you want without passing the application context around, that's fine. Less dependencies are always better.

                Regards,
                Andreas

                Comment


                • #9
                  why not just have a static service var in the bean?

                  Thanks, Andreas, I've used your proposed solution and it works well. But what would be wrong with just wiring bean normally and have the setter assign to a private static variable?

                  For example, even though the following class is not a Singleton, (because the Spring container isn't the only code that instantiates the class) the ras service is static so once it is set by Spring, it is always there. No special ApplicationContext code is necessary:

                  Code:
                  public class AccountClientImpl {
                  
                    private static RemoteAccountService ras;
                    
                    public void setRemoteAccountService(RemoteAccountService service) {
                        ras = service;
                    }
                    
                    public String getHelloWorld() {
                      return ras.getHelloWorld();
                     }
                  }

                  Comment


                  • #10
                    Classic GoF singletons can be evil, so making this static isn't the problem it's exposing this via a static method that's the problem. There are lots of articles and discussions on this one.
                    http://www.springframework.org/docs/...and-singletons
                    Last edited by karldmoore; Aug 27th, 2007, 02:59 PM.

                    Comment


                    • #11
                      There are some more articles here.
                      http://blogs.msdn.com/scottdensmore/...25/140827.aspx
                      http://www.softwarereality.com/design/singleton.jsp
                      http://www.javalobby.org/java/forums/t91076.html
                      http://blogs.kahaf.com/ishaq/archives/3
                      Last edited by karldmoore; Aug 27th, 2007, 02:58 PM.

                      Comment


                      • #12
                        So, are you saying that since this class is not a Singleton and the method to access the variable is not static, that all is good?

                        Comment


                        • #13
                          Originally posted by hotwater View Post
                          So, are you saying that since this class is not a Singleton and the method to access the variable is not static, that all is good?
                          Well I'm saying that if there is no static access that's a good thing, but then what's the point of making this property static in the first place?
                          Last edited by karldmoore; Aug 27th, 2007, 02:58 PM.

                          Comment


                          • #14
                            The reason I've set the property to static is that otherwise it will be lost when non-Spring code does a new AccountClientImpl().

                            Comment

                            Working...
                            X