Results 1 to 6 of 6

Thread: Accessing beans from static methods

  1. #1
    Join Date
    Jan 2005
    Posts
    6

    Default Accessing beans from static methods

    I am converting an existing application and have come across the following scenario.

    I have an object (Foo) which has a static method on it called count(). The count method grabs a DAO implementation and executes a SQL query to count instances of this object in the database.

    I want this method to be static because it does not relate to any instance of Foo, and I want it to be part of Foo because that is what it does; it counts Foos.

    In my new Spring enabled environment, my DAO implementations (they are hibernate specific) are defined as static beans.

    So how can I get hold of the Spring maintained instances from my static method? I want Spring to do it for me so that I can adhere to DI and I also do not want to make any Spring specific calls.

    Is this impossible?
    The only way I can think of doing this is to revert to the old pattern of a service locator (singleton) providing me with access into the Spring application context, and the getBean method calls. But then, it feels like a step backwards away from Spring ideals.

    Any thoughts?

  2. #2
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    Personally, I'd implement a FooHelper, but that's just because (unless I'm missing something) you can't really do this no.

    You could use a ServiceLocator (in this a BeanFactoryLocator locating a Spring BeanFactory) but you don't really want to do this either I think!

    Anybody else has ideas?
    Alef Arendsen
    SpringSource
    http://www.springsource.com

  3. #3

    Default

    I don't think having a singleton to hold your context is necessarily a step backwards. Spring richclient for example uses that technique to hold the context of the running ui application. The singleton doesn't just have to serve the context either, it can serve the DAO instance directly so only the singleton has any spring calls in it.

    As long as you will only need one of your dao context per classloader, I don't see a problem with using a singleton in this case.

    Cheers

  4. #4
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    I agree with Alef.

    To me, this sounds like two different objects...

    I want this method to be static because it does not relate to any instance of Foo, ...
    I'd create a Foos (or FooHelper as suggested) class that deal with collections of Foo instances, and provides helper methods.

  5. #5
    Join Date
    Jan 2005
    Posts
    6

    Default

    Thanks for your replies, it seems to me that both suggestions are workable. I have decided to go for the singleton access to the spring context so that I can keep my class nice and OO with the methods relating to the Foo class contained in the Foo class.

    Thanks again

  6. #6
    Join Date
    Jun 2005
    Location
    Highlands Ranch, CO, USA
    Posts
    1

    Default A possible implementation of static access to App Context

    Here's an implementation, based on the ideas discussed in this post that I worked up.

    It comes with the warning to my team members that it should only be used sparingly and that we should migrate towards wiring everything together with Spring's application context via ActionSupport.getWebApplicationContext(). We derived our base action class from ActionSupport.

    Code:
    import javax.servlet.ServletContextEvent;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.context.support.WebApplicationContextUtils;
    
    /**
     * Save the copy of the application context loaded at Servlet startup
     * 
     * http://forum.springframework.org/viewtopic.php?t=1762&highlight=static+access+application+context
     * http://forum.springframework.org/viewtopic.php?t=3224&highlight=static+access+application+context
     * http://forum.springframework.org/viewtopic.php?t=2354&highlight=static+access+application+context
     * 
     * @author 	Matthew McCullough (Ambient Ideas, LLC)
     * @since 	Jun 23, 2005
    
     */
    public class SpringStaticAppContextListener extends ContextLoaderListener {
    
    	private static ApplicationContext ac;
    
    	/**
    	 * Store the web application context into a static member variable.
    	 */
    	public void contextInitialized(ServletContextEvent event) {
    		super.contextInitialized(event);
    		ac = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
    	}
    	
    	/**
    	 * Release this class' static reference to the application context.
    	 */
    	public void contextDestroyed(ServletContextEvent event) {
    		ac = null;
    		super.contextDestroyed(event);
    	}
    	
    	/**
    	 * Get the singleton instance of the Application Context that was
    	 * loaded for this web application.
    	 * 
    	 * @return intialized ApplicationContext for this web app.
    	 */
    	public static ApplicationContext getApplicationContext() {
    		return ac;
    	}
    }
    <=====================>
    Matthew McCullough
    Ambient Ideas, LLC
    Highlands Ranch, CO 80129
    USA
    1+ (720) 249-2766
    http://www.AmbientIdeas.com
    <=====================>

Similar Threads

  1. Replies: 5
    Last Post: Jan 23rd, 2008, 04:30 PM
  2. Intercepting static methods?
    By dune in forum AOP
    Replies: 4
    Last Post: Jan 29th, 2007, 05:05 AM
  3. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  4. Accessing methods from beans in the Flow Scope
    By jackcholt in forum Web Flow
    Replies: 4
    Last Post: May 26th, 2005, 10:28 PM
  5. Replies: 7
    Last Post: Nov 3rd, 2004, 06:41 AM

Posting Permissions

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