Announcement Announcement Module
No announcement yet.
Accessing beans from static methods Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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?


    • #3
      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.



      • #4
        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
          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
            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.

            import javax.servlet.ServletContextEvent;
            import org.springframework.context.ApplicationContext;
            import org.springframework.web.context.ContextLoaderListener;
             * Save the copy of the application context loaded at Servlet startup
             * @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) {
            		ac = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
            	 * Release this class' static reference to the application context.
            	public void contextDestroyed(ServletContextEvent event) {
            		ac = null;
            	 * 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;