Class MethodInvocationBeanProvider
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.providers.SimpleBeanProvider
-
- net.sf.jguiraffe.di.impl.providers.MethodInvocationBeanProvider
-
- All Implemented Interfaces:
BeanProvider
public class MethodInvocationBeanProvider extends SimpleBeanProvider
A specialized
BeanProvider
that creates beans by invoking a method.This
BeanProvider
implementation is initialized with aMethodInvocation
object describing the method to be invoked. Optional aDependency
can be provided for the instance, on which the method is to be invoked (if no such dependency is specified, the method to be invoked must be static). This class is intended for the following use cases:- It can be used for obtaining instances that are not created using a
constructor, but by invoking a static factory method. In this case the passed
in
MethodInvocation
must refer to this factory method and must provide the parameters for this invocation. - Another use case is the handling of factory classes: Some
objects are not directly created, but a method on a specific factory class is
used for this purpose. An example could be a
Connection
object that is obtained from aDataSource
. In this case the factory is defined as a separate bean, and a dependency to this bean is specified to this bean provider class. TheMethodInvocation
defines the method of this factory bean that has to be called for obtaining an instance.
As is true for other
SimpleBeanProvider
s, this provider is intended to be used together with a life-cycle-awareBeanProvider
. It does not provide any life-cycle support on its own.- Version:
- $Id: MethodInvocationBeanProvider.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description MethodInvocationBeanProvider(Dependency targetBean, MethodInvocation methodInv)
Creates a new instance ofMethodInvocationBeanProvider
and initializes it with the dependency to the target object (on which the method is to be invoked) and the description of the method invocation.MethodInvocationBeanProvider(Dependency targetBean, MethodInvocation methodInv, ClassDescription beanClsDsc)
Creates a new instance ofMethodInvocationBeanProvider
and initializes it with the dependency to the target object (on which the method is to be invoked), the description of the method invocation, and the class of the managed bean.MethodInvocationBeanProvider(MethodInvocation methodInv)
Creates a new instance ofMethodInvocationBeanProvider
and initializes it with the description of the method to invoke.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Object
getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this provider.Class<?>
getBeanClass(DependencyProvider dependencyProvider)
Returns the bean class of the bean managed by this provider.ClassDescription
getBeanClassDescription()
Returns the description of the class of the managed bean.Set<Dependency>
getDependencies()
Returns the dependencies of this bean provider.MethodInvocation
getInvocation()
Returns theMethodInvocation
for the method to be invoked.Dependency
getTargetDependency()
Returns the dependency to the target bean.String
toString()
Returns a string representation for this object.-
Methods inherited from class net.sf.jguiraffe.di.impl.providers.SimpleBeanProvider
getLockID, isBeanAvailable, setLockID, shutdown
-
-
-
-
Constructor Detail
-
MethodInvocationBeanProvider
public MethodInvocationBeanProvider(Dependency targetBean, MethodInvocation methodInv)
Creates a new instance ofMethodInvocationBeanProvider
and initializes it with the dependency to the target object (on which the method is to be invoked) and the description of the method invocation.- Parameters:
targetBean
- the dependency to the target bean (can be null)methodInv
- the description of the method to invoke (must not be null)- Throws:
IllegalArgumentException
- if theMethodInvocation
is undefined
-
MethodInvocationBeanProvider
public MethodInvocationBeanProvider(MethodInvocation methodInv)
Creates a new instance ofMethodInvocationBeanProvider
and initializes it with the description of the method to invoke. No dependency for the target instance is provided, so a static method must be specified.- Parameters:
methodInv
- the description of the method to invoke (must not be null)- Throws:
IllegalArgumentException
- if theMethodInvocation
is undefined
-
MethodInvocationBeanProvider
public MethodInvocationBeanProvider(Dependency targetBean, MethodInvocation methodInv, ClassDescription beanClsDsc)
Creates a new instance ofMethodInvocationBeanProvider
and initializes it with the dependency to the target object (on which the method is to be invoked), the description of the method invocation, and the class of the managed bean. Depending on the used dependency and/or the method invocation, it is not always possible to determine the class of the managed bean. With this constructor it can be explicitly set.- Parameters:
targetBean
- the dependency to the target bean (can be null)methodInv
- the description of the method to invoke (must not be null)beanClsDsc
- a description of the class of the managed bean (can be null)- Throws:
IllegalArgumentException
- if theMethodInvocation
is undefined
-
-
Method Detail
-
getTargetDependency
public Dependency getTargetDependency()
Returns the dependency to the target bean. This can be null if none is provided.- Returns:
- the dependency to the target bean
-
getInvocation
public MethodInvocation getInvocation()
Returns theMethodInvocation
for the method to be invoked.- Returns:
- the method invocation
-
getBeanClassDescription
public ClassDescription getBeanClassDescription()
Returns the description of the class of the managed bean. If a class description was explicitly set in the constructor, this description is returned. Otherwise this implementation tries to obtain the bean class from theMethodInvocation
object owned by this provider. Because a class is optional for a method invocation, result can be null. To avoid this, a validClassDescription
should always be set either on theMethodInvocation
or when an instance of this class is constructed.- Returns:
- a class description of the managed bean
-
getBean
public Object getBean(DependencyProvider dependencyProvider)
Returns the bean managed by this provider. If a target dependency is set, the corresponding bean will be fetched from the specified dependency provider and used as target instance for the method invocation. Otherwise the method is invoked on a null instance, so this has to be a static method.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the bean managed by this provider
-
getBeanClass
public Class<?> getBeanClass(DependencyProvider dependencyProvider)
Returns the bean class of the bean managed by this provider. This implementation delegates togetBeanClassDescription()
for obtaining the description of the bean class. If this is successful, the class is resolved; otherwise result is null.- Parameters:
dependencyProvider
- the dependency provider- Returns:
- the class of the managed bean
- See Also:
getBeanClassDescription()
-
getDependencies
public Set<Dependency> getDependencies()
Returns the dependencies of this bean provider. These are the parameter dependencies of theMethodInvocation
object. If a dependency for the target instance is provided, it will also be contained in the returned set.- Specified by:
getDependencies
in interfaceBeanProvider
- Overrides:
getDependencies
in classSimpleBeanProvider
- Returns:
- a set with the dependencies of this provider
- See Also:
Dependency
-
-