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:
BeanProvider
for actually
creating an instance of the managed bean.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()
and fetchBean()
.
createBean()
, as its name implies, creates a new instance of
the managed bean class. This is done by invoking the
BeanProvider
for creating new beans. After that the
Invokable
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 a
InjectionException
exception being thrown.
The fetchBean()
method checks whether a bean instance has
already been created. If this is the case, it is directly returned. Otherwise
createBean()
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 the getBean()
method; all
other methods defined by the BeanProvider
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.
Modifier | Constructor and Description |
---|---|
protected |
LifeCycleBeanProvider(BeanProvider createProvider)
Creates a new instance of
LifeCycleBeanProvider and
initializes it with the BeanProvider for creating the bean
instance. |
protected |
LifeCycleBeanProvider(BeanProvider createProvider,
Invokable initinv)
Creates a new instance of
LifeCycleBeanProvider and
initializes it with the BeanProvider for creating the bean
instance and an Invokable for initializing it. |
Modifier and Type | Method and 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 the
BeanProvider that is responsible for creating
a new bean instance. |
Invokable |
getBeanInitializer()
Returns the
Invokable 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;
instead
fetchInitializedBeanInstance(Object, DependencyProvider)
is invoked |
void |
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 this
BeanProvider that it is no longer needed. |
String |
toString()
Returns a string representation of this object.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getBean
protected LifeCycleBeanProvider(BeanProvider createProvider, Invokable initinv)
LifeCycleBeanProvider
and
initializes it with the BeanProvider
for creating the bean
instance and an Invokable
for initializing it.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 beanIllegalArgumentException
- if the bean provider is nullprotected LifeCycleBeanProvider(BeanProvider createProvider)
LifeCycleBeanProvider
and
initializes it with the BeanProvider
for creating the bean
instance.createProvider
- the bean provider for creating a bean instance
(must not be null)IllegalArgumentException
- if the bean provider is nullpublic BeanProvider getBeanCreator()
BeanProvider
that is responsible for creating
a new bean instance.public Invokable getBeanInitializer()
Invokable
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.public Class<?> getBeanClass(DependencyProvider dependencyProvider)
BeanProvider
for creating new bean
instances.getBeanClass
in interface BeanProvider
dependencyProvider
- the dependency providerInjectionException
- if an error occurs determining the classpublic Set<Dependency> getDependencies()
getDependencies
in interface BeanProvider
Dependency
public Long getLockID()
getLockID
in interface BeanProvider
public void setLockID(Long lid)
setLockID
in interface BeanProvider
lid
- the ID of the locking transactionBeanProvider.getLockID()
public boolean isBeanAvailable()
isBeanAvailable
in interface BeanProvider
public void shutdown(DependencyProvider depProvider)
BeanProvider
that it is no longer needed. This is
just an empty dummy implementation. Derived classes that support shutdown
handling have to override it.shutdown
in interface BeanProvider
depProvider
- the DependencyProvider
public void initialize(DependencyProvider dependencyProvider)
initialize
in interface BeanInitializer
dependencyProvider
- the dependency providerpublic String toString()
protected Object createBean(DependencyProvider dependencyProvider)
dependencyProvider
- the dependency providerInjectionException
- if an error occursprotected Object fetchBean(DependencyProvider dependencyProvider)
createBean()
.
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.dependencyProvider
- the dependency providerInjectionException
- if an error occursprotected boolean canInitialize(DependencyProvider dependencyProvider)
dependencyProvider
- the dependency providerprotected boolean hasBean()
protected void resetBean()
fetchBean()
a completely new bean will be created.protected Object doCreateBean(DependencyProvider dependencyProvider)
createBean()
for actually creating the bean.dependencyProvider
- the dependency providerInjectionException
- if an error occurs@Deprecated protected void initBean(Object bean, DependencyProvider dependencyProvider)
fetchInitializedBeanInstance(Object, DependencyProvider)
is invokedcreateBean()
for each new bean instance. This implementation
invokes the initializer and notifies the DependencyProvider
about
the creation of a new bean.bean
- the bean to initializedependencyProvider
- the dependency providerInjectionException
- if an error occursprotected Object fetchInitializedBeanInstance(Object bean, DependencyProvider dependencyProvider)
createBean()
for each new bean instance. Its purpose is to
execute the initializer script on the bean. This implementation invokes
the initializer and notifies the DependencyProvider
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.bean
- the bean to initializedependencyProvider
- the dependency providerInjectionException
- if an error occursCopyright © 2016 The JGUIraffe Team. All rights reserved.