Class SingletonBeanProvider
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.providers.LifeCycleBeanProvider
-
- net.sf.jguiraffe.di.impl.providers.SingletonBeanProvider
-
- All Implemented Interfaces:
BeanInitializer
,BeanProvider
public class SingletonBeanProvider extends LifeCycleBeanProvider
A specialized life-cycle supporting
BeanProvider
implementation for creating singleton beans.When the
getBean()
method of an instance of this class is called for the first time, all dependencies are resolved, and a new bean instance is created (using the specified creation bean provider). Following calls togetBean()
always return this same bean instance. In fact, this provider then behaves like a constant bean provider: it does not declare any dependencies any more and does not need any synchronization support (i.e. thegetLockID()
method will always return null). This means that providers of this type can be efficiently used in a multi-threaded environment once they are initialized.Instances of this class can also be initialized with an
Invokable
to be called when the bean is no more needed. ThisInvokable
is triggered by theshutdown()
method. This mechanism makes it possible to perform cleanup when a bean store is closed.- Version:
- $Id: SingletonBeanProvider.java 208 2012-02-11 20:57:33Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description SingletonBeanProvider(BeanProvider createProvider)
Creates a new instance ofSingletonBeanProvider
and initializes it with the bean provider for creating a bean instance.SingletonBeanProvider(BeanProvider createProvider, Invokable initinv)
Creates a new instance ofSingletonBeanProvider
and initializes it with the bean provider for creating a bean instance and the invocation object for performing initialization.SingletonBeanProvider(BeanProvider createProvider, Invokable initInv, Invokable shutdownInv)
Creates a new instance ofSingletonBeanProvider
and initializes it with the bean provider for creating a bean instance andInvokable
objects for initializing and releasing the bean managed by this provider.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this bean provider.Set<Dependency>
getDependencies()
Returns the dependencies of this bean provider.Long
getLockID()
Returns the ID of the locking transaction.Invokable
getShutdownHandler()
Returns theInvokable
object that is called when this provider is shut down.void
shutdown(DependencyProvider depProvider)
Tells this provider that it is no more needed.-
Methods inherited from class net.sf.jguiraffe.di.impl.providers.LifeCycleBeanProvider
canInitialize, createBean, doCreateBean, fetchBean, fetchInitializedBeanInstance, getBeanClass, getBeanCreator, getBeanInitializer, hasBean, initBean, initialize, isBeanAvailable, resetBean, setLockID, toString
-
-
-
-
Constructor Detail
-
SingletonBeanProvider
public SingletonBeanProvider(BeanProvider createProvider, Invokable initInv, Invokable shutdownInv)
Creates a new instance ofSingletonBeanProvider
and initializes it with the bean provider for creating a bean instance andInvokable
objects for initializing and releasing the bean managed by this provider.- Parameters:
createProvider
- the bean provider used for creating a new bean instance (must not be null)initInv
- the (optional) invocation object for performing initializationshutdownInv
- the (optional)Invokable
object to be called on shutdown- Throws:
IllegalArgumentException
- if the bean provider is undefined
-
SingletonBeanProvider
public SingletonBeanProvider(BeanProvider createProvider, Invokable initinv)
Creates a new instance ofSingletonBeanProvider
and initializes it with the bean provider for creating a bean instance and the invocation object for performing initialization.- Parameters:
createProvider
- the bean provider used for creating a new bean instance (must not be null)initinv
- the (optional) invocation object for performing initialization- Throws:
IllegalArgumentException
- if the bean provider is undefined
-
SingletonBeanProvider
public SingletonBeanProvider(BeanProvider createProvider)
Creates a new instance ofSingletonBeanProvider
and initializes it with the bean provider for creating a bean instance.- Parameters:
createProvider
- the bean provider used for creating a new bean instance (must not be null)- Throws:
IllegalArgumentException
- if the bean provider is undefined
-
-
Method Detail
-
getShutdownHandler
public Invokable getShutdownHandler()
Returns theInvokable
object that is called when this provider is shut down. This can be null if no shutdown handler was set.- Returns:
- the
Invokable
to be called on shutdown
-
getBean
public Object getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this bean provider. This implementation will create a bean on first access and then always return this instance.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the bean managed by this provider
- Throws:
InjectionException
- if an error occurs
-
getDependencies
public Set<Dependency> getDependencies()
Returns the dependencies of this bean provider. If already a bean has been created, this implementation returns null because there is no need any more for resolving dependencies.- Specified by:
getDependencies
in interfaceBeanProvider
- Overrides:
getDependencies
in classLifeCycleBeanProvider
- Returns:
- the dependencies of this bean provider
- See Also:
Dependency
-
getLockID
public Long getLockID()
Returns the ID of the locking transaction. As long as no bean has been created yet, this implementation behaves like the method of the base class. After that it will always return null, because from now on there is no need for synchronization any more.- Specified by:
getLockID
in interfaceBeanProvider
- Overrides:
getLockID
in classLifeCycleBeanProvider
- Returns:
- the ID of the locking transaction
-
shutdown
public void shutdown(DependencyProvider depProvider)
Tells this provider that it is no more needed. This implementation invokes the shutdown handler if a bean was already created.- Specified by:
shutdown
in interfaceBeanProvider
- Overrides:
shutdown
in classLifeCycleBeanProvider
- Parameters:
depProvider
- theDependencyProvider
-
-