Singleton FactoryBeans?

View: New views
2 Messages — Rating Filter:   Alert me  

Singleton FactoryBeans?

by Dain Sundstrom :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I see this pattern in most of our factory beans:

public class ConnectionManagerFactoryBean implements FactoryBean {
     private ConnectionManager connectionManager;

     public Object getObject() throws Exception {
      if (connectionManager == null) {
                this.connectionManager = createNewConnectionManager();
      }
         return connectionManager;
     }

     public Class getObjectType() {
         return ConnectionManager.class;
     }

     public boolean isSingleton() {
         return true;
     }
}

It seems like we are implementing the singleton pattern even though  
the FactoryBean is returning true from isSingleton.  Is this the  
normal way to do things in spring?  Is there a bug in spring that  
causes it to call getObject many times on a singlton FactoryBean?  If  
neither of these is true, I'd like to remove this code.

-dain

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email


Re: Singleton FactoryBeans?

by James.Strachan :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

AFAIK there's no bug in the spring use of isSingleton() so I guess
that code can be removed.

The main reason it was there in this particular class is probably a
hang-over from some refactoring. There was once some fairly complex
factory beans where objects were lazily created with defaults if they
were not dependency injected (to try minimise the amount of XML
spagetti required to configure JCA stuff).

As an aside I'm becoming an increased fan of 'clever' factory beans
that allow minimal configuration and generally do the right thing,
letting you override and dependency inject where you want but allowing
you to omit reams of XML if you prefer. Kinda like using
convention/defaults over configuration.

Already Jencks does some of this (e.g. allowing you on a JCAContainer
to just specify thread pool size without configuring the whole work
manager, context and so forth). I'd like us to do a better job of this
in Jencks - to make it mindnumbingly simple to configure the common
stuff folks wanna do without much XML - as I still hear from folks
that its a bit too complex to configure Jencks in Spring.

(Using the xbean-spring flavour of XML helps in this but the same
concept applies there too)


On 7/19/06, Dain Sundstrom <dain@...> wrote:

> I see this pattern in most of our factory beans:
>
> public class ConnectionManagerFactoryBean implements FactoryBean {
>      private ConnectionManager connectionManager;
>
>      public Object getObject() throws Exception {
>         if (connectionManager == null) {
>                 this.connectionManager = createNewConnectionManager();
>         }
>          return connectionManager;
>      }
>
>      public Class getObjectType() {
>          return ConnectionManager.class;
>      }
>
>      public boolean isSingleton() {
>          return true;
>      }
> }
>
> It seems like we are implementing the singleton pattern even though
> the FactoryBean is returning true from isSingleton.  Is this the
> normal way to do things in spring?  Is there a bug in spring that
> causes it to call getObject many times on a singlton FactoryBean?  If
> neither of these is true, I'd like to remove this code.
>
> -dain
>
> ---------------------------------------------------------------------
> To unsubscribe from this list please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


--

James
-------
http://radio.weblogs.com/0112098/

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email