Class ConstantBeanProvider
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.providers.SimpleBeanProvider
-
- net.sf.jguiraffe.di.impl.providers.ConstantBeanProvider
-
- All Implemented Interfaces:
BeanProvider
,Dependency
public final class ConstantBeanProvider extends SimpleBeanProvider implements BeanProvider, Dependency
A concrete implementation of the
BeanProvider
interface that will always return the same bean instance.When an instance of this class is created, the managed bean instance (and optionally the target class if a type conversion is required) is passed in. On its first call the
getBean(DependencyProvider)
method checks whether a type conversion if required. If this is the case, the conversion is performed, and the resulting object is stored. Later invocations will then always return this bean instance. The other methods defined by theBeanProvider
interface are implemented as dummies: There are no dependencies, and synchronization support is not needed.In addition to the
BeanProvider
interface, theDependency
interface is also implemented. This makes it possible to have static dependencies, which are always resolved to aBeanProvider
returning a constant bean. Dependencies of this type may seem strange first, but they make sense in some cases. For instance when a method is to be invoked, its parameters need to be defined. The current parameter values may be dependencies to other beans in the current bean store; so they need to be defined using dependencies. In simple cases however constant values need to be passed (e.g. integers, flags, or string values). For this purpose such a constant dependency can be used.Instances of this class can be created using the static
getInstance()
factory methods. In any case the constant value of the dependency must be passed in. It is also possible to specify the class of this dependency. In this case the class will try a type conversion as described above.- Version:
- $Id: ConstantBeanProvider.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Field Summary
Fields Modifier and Type Field Description static ConstantBeanProvider
NULL
Constant for a dependency that will be resolved to null.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
getBean()
Returns the bean managed by this provider.Object
getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this provider.Class<?>
getBeanClass(DependencyProvider dependencyProvider)
Returns the class of the managed bean.static ConstantBeanProvider
getInstance(Class<?> valueClass, Object value)
Returns an instance of this class that refers to the specified value of the given class.static ConstantBeanProvider
getInstance(Object bean)
Creates a new instance of this class and initializes it with the bean to be managed.BeanProvider
resolve(BeanStore store, DependencyProvider depProvider)
Returns theBeanProvider
thisDependency
refers to.String
toString()
Returns a string representation of this object.-
Methods inherited from class net.sf.jguiraffe.di.impl.providers.SimpleBeanProvider
getDependencies, getLockID, isBeanAvailable, setLockID, shutdown
-
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
getDependencies, getLockID, isBeanAvailable, setLockID, shutdown
-
-
-
-
Field Detail
-
NULL
public static final ConstantBeanProvider NULL
Constant for a dependency that will be resolved to null.
-
-
Method Detail
-
getBean
public Object getBean()
Returns the bean managed by this provider. This is the same bean as was passed to the constructor. No type conversion has been performed.- Returns:
- the bean passed to the constructor
-
getBean
public Object getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this provider. This implementation operates on the bean that was passed when this object was created. If necessary, type conversion is performed (the result of this conversion is cached, so that the conversion is only done on first access).- Specified by:
getBean
in interfaceBeanProvider
- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the bean managed by this provider
-
getBeanClass
public Class<?> getBeanClass(DependencyProvider dependencyProvider)
Returns the class of the managed bean. If a class was passed on creation time, it is directly returned. Otherwise, the class is obtained from the managed bean. If the bean is null, the typejava.lang.Object
is returned.- Specified by:
getBeanClass
in interfaceBeanProvider
- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the class of the managed bean
-
resolve
public BeanProvider resolve(BeanStore store, DependencyProvider depProvider)
Returns theBeanProvider
thisDependency
refers to. This implementation simply returns the this pointer.- Specified by:
resolve
in interfaceDependency
- Parameters:
store
- the bean storedepProvider
- the dependency provider- Returns:
- the
BeanProvider
thisDependency
refers to
-
toString
public String toString()
Returns a string representation of this object. This string will contain the value of this bean provider.
-
getInstance
public static ConstantBeanProvider getInstance(Object bean)
Creates a new instance of this class and initializes it with the bean to be managed.- Parameters:
bean
- the bean to be managed- Returns:
- the new instance of this class
-
getInstance
public static ConstantBeanProvider getInstance(Class<?> valueClass, Object value)
Returns an instance of this class that refers to the specified value of the given class. If necessary, a conversion will be performed to convert the value to the given class.- Parameters:
valueClass
- the class of this bean provider (can be null)value
- the value of the managed bean- Returns:
- the instance representing this value
- Throws:
IllegalArgumentException
- if a conversion is necessary, but cannot be performed
-
-