JBoss.org Community Documentation

10.3. Using factories

If you have a number of classes that share the same interface then you may want to use a factory to create your POJO instances. JBoss Microcontainer provides support for static or non-static factory methods which can also take parameters if necessary.

  • Static factory method

    
    @Factory (factoryClass="org.jboss.example.MyFactory",
              factoryMethod="newInstance")
    public class SimpleBean {
       ...
    }
    
    <bean name="SimpleBean" class="org.jboss.example.SimpleBean">
        <constructor factoryClass="org.jboss.example.MyFactory"
                     factoryMethod="newInstance"/>
    </bean> 
  • Static factory method with parameters

    
    @Factory (factoryClass="org.jboss.example.MyFactory",
              factoryMethod="newInstance",
              parameters={@Value(string=@StringValue("a string")),@Value(string=@StringValue("7"))})
    public class SimpleBean {
       ...
    }
    
    <bean name="SimpleBean" class="org.jboss.example.SimpleBean">
        <constructor factoryClass="org.jboss.example.MyFactory"
                     factoryMethod="newInstance">
            <parameter>a string</parameter>
            <parameter>7</parameter>
        </constructor>
    </bean> 
  • Non-static factory method

    
    @Factory (factory=@Value(javabean=@JavaBeanValue("org.jboss.example.MyOtherFactory")),
              factoryMethod="createBean")
    public class SimpleBean {
        ...
    }
    
    <bean name="MyOtherFactory" class="org.jboss.example.MyOtherFactory"/>

    <bean name="SimpleBean" class="org.jboss.example.SimpleBean">
        <constructor factoryMethod="createBean">
            <factory bean="MyOtherFactory"/>
        </constructor>
    </bean>
  • Non-static factory method with parameters

    
    @Factory (factory=@Value(javabean=@JavaBeanValue("org.jboss.example.MyOtherFactory")),
              factoryMethod="createBean",
              parameters={@Value(string=@StringValue("a string")),@Value(string=@StringValue("7"))})
    public class SimpleBean {
        ...
    }
    
    <bean name="MyOtherFactory" class="org.jboss.example.MyOtherFactory"/>

    <bean name="SimpleBean" class="org.jboss.example.SimpleBean">
        <constructor factoryMethod="createBean">
            <factory bean="MyOtherFactory"/>
            <parameter>a string</parameter>
            <parameter>7</parameter>
        </constructor>
    </bean>

For the special case where a bean implements its own static factory method in order to create itself then you can also use a shorthand notation:

  • Bean implementing its own static factory method

    
    public class SimpleBean {

        @FactoryMethod
        public static newInstance(@StringValue("a string") String name, @StringValue("5") int level) {
            ...
        }
    }
    
    <bean name="SimpleBean" class="org.jboss.example.SimpleBean">
        <constructor factoryMethod="newInstance">
            <parameter>a string</parameter>
            <parameter>5</parameter>
        </constructor>
    </bean> 

    Note that the @FactoryMethod annotation doesn't have a parameter attribute as the method parameters themselves are annotated.

Note

Factory method parameters can also be configured using JavaBeans and injected bean references if necessary just like constructor parameters.