Package com.zfabrik.springframework
Class ComponentFactoryBean<T>
java.lang.Object
com.zfabrik.springframework.ComponentFactoryBean<T>
- All Implemented Interfaces:
org.springframework.beans.factory.Aware
,org.springframework.beans.factory.FactoryBean<T>
,org.springframework.context.ApplicationContextAware
public class ComponentFactoryBean<T>
extends Object
implements org.springframework.beans.factory.FactoryBean<T>, org.springframework.context.ApplicationContextAware
A factory bean that provides z2 components. Use like this
<bean id="...bean..." class="com.zfabrik.springframework.ComponentFactoryBean">
<property name="componentName" value="... z2 component ..."/>
<property name="className" value="... z2 component facet type as in IResourceHandle.as(Class)
..."/>
<property name="dependant" value="... z2 component to be made dependent on the providing component ..."/>
</bean>
The "dependant" property is optional. It defaults to the
java component of the context.
This factory bean can also be used meaningfully when using programmatic configuration. Instead of returning the result of a component lookup when providing z2 components as beans, return a factory bean instance:
@Bean FactoryBean<MyInterface> myBean() {
return new ComponentFactoryBean("module/component",MyInterface.class);
}
Note: Unfortunately, you cannot reliably return the result of the lookup as bean instance directly, as Spring will try
to apply configuration on the returned instance. Depending on the use of annotations in the actual implementation
class this may lead to unpredictable and unexpected results when configured in different consuming modules.
-
Field Summary
Modifier and TypeFieldDescriptionprotected String
protected String
protected org.springframework.context.ApplicationContext
protected String
protected IResourceHandle
Fields inherited from interface org.springframework.beans.factory.FactoryBean
OBJECT_TYPE_ATTRIBUTE
-
Constructor Summary
ConstructorDescriptionComponentFactoryBean(String componentName, Class<T> type)
Constructor useful for Spring Java configuration. -
Method Summary
Modifier and TypeMethodDescriptionboolean
static <T> ComponentFactoryBean<T>
Convenience factory method.void
setApplicationContext(org.springframework.context.ApplicationContext applicationcontext)
void
setClassName(String className)
void
setComponentName(String componentName)
void
setDependant(String dependant)
toString()
-
Field Details
-
componentName
-
className
-
dependant
-
rh
-
clz
-
ctxt
protected org.springframework.context.ApplicationContext ctxt
-
-
Constructor Details
-
ComponentFactoryBean
public ComponentFactoryBean() -
ComponentFactoryBean
Constructor useful for Spring Java configuration. In aConfiguration
annotated class, the a bean provided via a z2 lookup may be provided by actually providing the factory bean:
Note: Unfortunately, you cannot reliably return the result of the lookup as bean instance directly, as Spring will try to apply configuration on the returned instance. Depending on the use of annotations in the actual implementation class this may lead to unpredictable and unexpected results when configured in different consuming modules.@Bean FactoryBean<MyInterface> myBean() { return new ComponentFactoryBean("module/component",MyInterface.class); }
- Parameters:
componentName
- Name of component to retrieve.as
- Type facet to be supplied.
-
-
Method Details
-
lookup
Convenience factory method. Equivalent to usingComponentFactoryBean(String, Class)
.- Parameters:
componentName
- Name of component to retrieve.as
- Type facet to be supplied.- Returns:
- bean instance or
null
, if not found
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext applicationcontext) throws org.springframework.beans.BeansException- Specified by:
setApplicationContext
in interfaceorg.springframework.context.ApplicationContextAware
- Throws:
org.springframework.beans.BeansException
-
setComponentName
-
setClassName
-
setDependant
-
getObject
-
getObjectType
- Specified by:
getObjectType
in interfaceorg.springframework.beans.factory.FactoryBean<T>
-
isSingleton
public boolean isSingleton()- Specified by:
isSingleton
in interfaceorg.springframework.beans.factory.FactoryBean<T>
-
toString
-