Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: getting bean refrence witout using WebApplicationContextUtils class

  1. #1
    Join Date
    Mar 2006
    Posts
    4

    Default 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. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  3. #3
    Join Date
    Mar 2006
    Posts
    4

    Default

    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

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  5. #5
    Join Date
    Mar 2006
    Posts
    4

    Default

    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

  6. #6
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    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

  7. #7
    Join Date
    Mar 2006
    Posts
    4

    Default

    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

  8. #8
    Join Date
    Aug 2004
    Posts
    2,715

    Smile

    Quote 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

  9. #9
    Join Date
    Jan 2006
    Location
    Colorado
    Posts
    19

    Default 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();
       }
    }

  10. #10
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    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 at 02:59 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •