Class LifeCycleBeanProvider
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.providers.LifeCycleBeanProvider
-
- All Implemented Interfaces:
BeanInitializer
,BeanProvider
- Direct Known Subclasses:
FactoryBeanProvider
,SingletonBeanProvider
public abstract class LifeCycleBeanProvider extends Object implements BeanProvider, BeanInitializer
An abstract base class for
BeanProvider
implementations with life-cycle support.A
LifeCycleBeanProvider
has the following properties:- A (usually simple)
BeanProvider
for actually creating an instance of the managed bean. - An optional
Invokable
object for initializing the newly created bean instance.
This base class provides basic functionality for the creation and initialization of beans. It also implements the methods related to life-cycle support of the
BeanProvider
interface in a meaningful way. There are two methods that are intended to be called by concrete sub classes:createBean()
andfetchBean()
.createBean()
, as its name implies, creates a new instance of the managed bean class. This is done by invoking theBeanProvider
for creating new beans. After that theInvokable
object is called on the newly created bean. The creation of a bean through the bean provider may cause an endless loop if there are cyclic dependencies (e.g. bean A needs bean B as a constructor argument and vice verse). Such cycles are detected and lead to aInjectionException
exception being thrown.The
fetchBean()
method checks whether a bean instance has already been created. If this is the case, it is directly returned. OtherwisecreateBean()
is called for creating a new instance. Depending on their semantics derived classes decide, which of these methods to call. This decision must be implemented in thegetBean()
method; all other methods defined by theBeanProvider
interface are already implemented by this base class.Implementation note: This class is intended to be used together with a correct implementation of the
BeanContext
interface. It is not thread-safe by itself, but if the bean context handles transactions properly, it can be used in an environment with multiple threads accessing the bean context concurrently.- Version:
- $Id: LifeCycleBeanProvider.java 208 2012-02-11 20:57:33Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
LifeCycleBeanProvider(BeanProvider createProvider)
Creates a new instance ofLifeCycleBeanProvider
and initializes it with theBeanProvider
for creating the bean instance.protected
LifeCycleBeanProvider(BeanProvider createProvider, Invokable initinv)
Creates a new instance ofLifeCycleBeanProvider
and initializes it with theBeanProvider
for creating the bean instance and anInvokable
for initializing it.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected boolean
canInitialize(DependencyProvider dependencyProvider)
Checks whether initialization of the bean is now possible.protected Object
createBean(DependencyProvider dependencyProvider)
Creates and initializes a new bean instance.protected Object
doCreateBean(DependencyProvider dependencyProvider)
Creates a new bean instance.protected Object
fetchBean(DependencyProvider dependencyProvider)
Returns the bean instance created by this provider.protected Object
fetchInitializedBeanInstance(Object bean, DependencyProvider dependencyProvider)
Returns the initialized bean instance.Class<?>
getBeanClass(DependencyProvider dependencyProvider)
Returns the class of the bean managed by this provider.BeanProvider
getBeanCreator()
Returns theBeanProvider
that is responsible for creating a new bean instance.Invokable
getBeanInitializer()
Returns theInvokable
object responsible for initializing the newly created bean.Set<Dependency>
getDependencies()
Returns the dependencies of this bean provider.Long
getLockID()
Returns the ID of the locking transaction.protected boolean
hasBean()
Returns a flag whether a bean instance has already been created.protected void
initBean(Object bean, DependencyProvider dependencyProvider)
Deprecated.This method is not called any more during bean creation; insteadfetchInitializedBeanInstance(Object, DependencyProvider)
is invokedvoid
initialize(DependencyProvider dependencyProvider)
Performs initialization.boolean
isBeanAvailable()
Returns a flag whether the bean managed by this provider is available.protected void
resetBean()
Resets any so far created bean.void
setLockID(Long lid)
Sets the ID of the locking transaction.void
shutdown(DependencyProvider depProvider)
Notifies thisBeanProvider
that it is no longer needed.String
toString()
Returns a string representation of this object.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface net.sf.jguiraffe.di.BeanProvider
getBean
-
-
-
-
Constructor Detail
-
LifeCycleBeanProvider
protected LifeCycleBeanProvider(BeanProvider createProvider, Invokable initinv)
Creates a new instance ofLifeCycleBeanProvider
and initializes it with theBeanProvider
for creating the bean instance and anInvokable
for initializing it.- Parameters:
createProvider
- the bean provider for creating a bean instance (must not be null)initinv
- an optional invocation object with initialization code for the newly created bean- Throws:
IllegalArgumentException
- if the bean provider is null
-
LifeCycleBeanProvider
protected LifeCycleBeanProvider(BeanProvider createProvider)
Creates a new instance ofLifeCycleBeanProvider
and initializes it with theBeanProvider
for creating the bean instance.- Parameters:
createProvider
- the bean provider for creating a bean instance (must not be null)- Throws:
IllegalArgumentException
- if the bean provider is null
-
-
Method Detail
-
getBeanCreator
public BeanProvider getBeanCreator()
Returns theBeanProvider
that is responsible for creating a new bean instance.- Returns:
- the bean provider for creating new bean instances
-
getBeanInitializer
public Invokable getBeanInitializer()
Returns theInvokable
object responsible for initializing the newly created bean. This method never returns null. If no initializer was set, a default initializer object (that does not have any effect) is returned.- Returns:
- the invocation object used for initialization
-
getBeanClass
public Class<?> getBeanClass(DependencyProvider dependencyProvider)
Returns the class of the bean managed by this provider. This class is determined by theBeanProvider
for creating new bean instances.- Specified by:
getBeanClass
in interfaceBeanProvider
- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the class of the managed bean
- Throws:
InjectionException
- if an error occurs determining the class
-
getDependencies
public Set<Dependency> getDependencies()
Returns the dependencies of this bean provider. This implementation obtains the dependencies of the creation bean provider and the invocation object for initialization and returns a union.- Specified by:
getDependencies
in interfaceBeanProvider
- Returns:
- the dependencies of this bean provider
- See Also:
Dependency
-
getLockID
public Long getLockID()
Returns the ID of the locking transaction. If this method returns a non null value, this bean provider must not be used by a concurrent transaction.- Specified by:
getLockID
in interfaceBeanProvider
- Returns:
- the ID of the locking transaction
-
setLockID
public void setLockID(Long lid)
Sets the ID of the locking transaction. This indicates that this bean provider is blocked by the specified transaction.- Specified by:
setLockID
in interfaceBeanProvider
- Parameters:
lid
- the ID of the locking transaction- See Also:
BeanProvider.getLockID()
-
isBeanAvailable
public boolean isBeanAvailable()
Returns a flag whether the bean managed by this provider is available. This implementation checks whether the bean is currently created. If this is the case, it is not available.- Specified by:
isBeanAvailable
in interfaceBeanProvider
- Returns:
- a flag whether the managed bean is available
-
shutdown
public void shutdown(DependencyProvider depProvider)
Notifies thisBeanProvider
that it is no longer needed. This is just an empty dummy implementation. Derived classes that support shutdown handling have to override it.- Specified by:
shutdown
in interfaceBeanProvider
- Parameters:
depProvider
- theDependencyProvider
-
initialize
public void initialize(DependencyProvider dependencyProvider)
Performs initialization. This method is called by the dependency provider if initialization has to be delayed because of cyclic dependencies. It invokes the initializer.- Specified by:
initialize
in interfaceBeanInitializer
- Parameters:
dependencyProvider
- the dependency provider
-
toString
public String toString()
Returns a string representation of this object. This string also contains information about the bean provider used for creating the bean and the invocation object for the initialization.
-
createBean
protected Object createBean(DependencyProvider dependencyProvider)
Creates and initializes a new bean instance. This method is reentrant, which is necessary if there are cycles in the dependencies. In this case a bean that is only partly initialized may be returned. It is also possible that the cycles cannot be resolved, then an exception is thrown.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the newly created bean instance
- Throws:
InjectionException
- if an error occurs
-
fetchBean
protected Object fetchBean(DependencyProvider dependencyProvider)
Returns the bean instance created by this provider. If no instance has been created yet, this is done now by invokingcreateBean()
. Otherwise the bean instance is directly returned. If the dependencies contain cyclic references, it is possible that a bean instance is returned, which has not yet been fully initialized. Cycles that cannot be resolved cause an exception.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the bean instance managed by this provider
- Throws:
InjectionException
- if an error occurs
-
canInitialize
protected boolean canInitialize(DependencyProvider dependencyProvider)
Checks whether initialization of the bean is now possible. This method tests whether all dependencies required for the bean's initialization are currently available.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- a flag whether initialization can now be performed
-
hasBean
protected boolean hasBean()
Returns a flag whether a bean instance has already been created. This can be used by derived classes for adapting their behavior. This implementation returns true if and only if a bean instance has been created and fully initialized.- Returns:
- a flag whether a bean instance has been created
-
resetBean
protected void resetBean()
Resets any so far created bean. This method can be called by derived classes to reset this bean provider. In a following call tofetchBean()
a completely new bean will be created.
-
doCreateBean
protected Object doCreateBean(DependencyProvider dependencyProvider)
Creates a new bean instance. This method is called bycreateBean()
for actually creating the bean.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the new bean instance
- Throws:
InjectionException
- if an error occurs
-
initBean
@Deprecated protected void initBean(Object bean, DependencyProvider dependencyProvider)
Deprecated.This method is not called any more during bean creation; insteadfetchInitializedBeanInstance(Object, DependencyProvider)
is invokedInitializes a newly created bean instance. This method is called bycreateBean()
for each new bean instance. This implementation invokes the initializer and notifies theDependencyProvider
about the creation of a new bean.- Parameters:
bean
- the bean to initializedependencyProvider
- the dependency provider- Throws:
InjectionException
- if an error occurs
-
fetchInitializedBeanInstance
protected Object fetchInitializedBeanInstance(Object bean, DependencyProvider dependencyProvider)
Returns the initialized bean instance. This method is called bycreateBean()
for each new bean instance. Its purpose is to execute the initializer script on the bean. This implementation invokes the initializer and notifies theDependencyProvider
about the creation of a new bean. Note that the bean returned by the initializer script is the result of this method. Thus it is possible that a different bean than passed to the method becomes the managed bean of this provider. However, if the initializer returns null, the passed in bean is returned.- Parameters:
bean
- the bean to initializedependencyProvider
- the dependency provider- Returns:
- the bean instance
- Throws:
InjectionException
- if an error occurs- Since:
- 1.1
-
-