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 Details

    • componentName

      protected String componentName
    • className

      protected String className
    • dependant

      protected String dependant
    • rh

      protected IResourceHandle rh
    • clz

      protected Class<T> clz
    • ctxt

      protected org.springframework.context.ApplicationContext ctxt
  • Constructor Details

    • ComponentFactoryBean

      public ComponentFactoryBean()
    • ComponentFactoryBean

      public ComponentFactoryBean(String componentName, Class<T> type)
      Constructor useful for Spring Java configuration. In a Configuration annotated class, the a bean provided via a z2 lookup may be provided by actually providing the factory bean:
      
       @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.

      Parameters:
      componentName - Name of component to retrieve.
      as - Type facet to be supplied.
  • Method Details

    • lookup

      public static <T> ComponentFactoryBean<T> lookup(String componentName, Class<T> as)
      Convenience factory method. Equivalent to using ComponentFactoryBean(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 interface org.springframework.context.ApplicationContextAware
      Throws:
      org.springframework.beans.BeansException
    • setComponentName

      public void setComponentName(String componentName)
    • setClassName

      public void setClassName(String className)
    • setDependant

      public void setDependant(String dependant)
    • getObject

      public T getObject() throws Exception
      Specified by:
      getObject in interface org.springframework.beans.factory.FactoryBean<T>
      Throws:
      Exception
    • getObjectType

      public Class<T> getObjectType()
      Specified by:
      getObjectType in interface org.springframework.beans.factory.FactoryBean<T>
    • isSingleton

      public boolean isSingleton()
      Specified by:
      isSingleton in interface org.springframework.beans.factory.FactoryBean<T>
    • toString

      public String toString()
      Overrides:
      toString in class Object