JBoss.org Community Documentation

10.2. Calling constructors

Creating POJO instances is performed either by calling a constructor or using a factory method. The <constructor> element is used in both cases and parameters can be passed if necessary using nested <parameter> elements. Annotations also exist for either case with parameter values represented as annotations of the constructor parameters themselves.

Important

At present it is not possible to create a POJO instance only using annotations. This is because classes are not automatically loaded and scanned whenever they are added to the classpath. Doing so would significantly reduce the performance of the deployment process as all classes would need to be loaded, even if they were not yet required. Instead you must always define beans using a deployment descriptor as this tells the microcontainer which classes to load. These classes are subsequently scanned for annotations such as @Constructor and @Factory to discover how to create instances.


<bean name="SimpleBean" class="org.jboss.example.SimpleBean"/>
  • Default constructor

    
    @Constructor
    public TestConstructorBean() {
     ...
    }
    
    <bean name="Test" class="org.jboss.example.constructor.TestConstructorBean"/>       
  • Constructor with parameters

    
    @Constructor
    public TestConstructorBean(@StringValue("this is a test contructor") String testString, @StringValue("25") int testNumber) {
     ...
    }
    
    <bean name="Test" class="org.jboss.example.constructor.TestConstructorBean">
        <constructor>
            <parameter>this is a test constructor</parameter>
            <parameter>25</parameter>
        </constructor>
    </bean>
  • Choosing between two constructors with the same number of parameters

    
    public TestConstructorBean(String testString, int testNumber) {
        ...
    }

    @Constructor
    public TestConstructorBean(@StringValue("this is a test constructor") String testString, @StringValue(value="25", type="long") long testNumber) {
        ...
    }
    
    <bean name="Test" class="org.jboss.example.constructor.TestConstructorBean">
        <constructor>
            <parameter>this is a test constructor</parameter>
            <parameter class="long">25</parameter>
        </constructor>
    </bean>

    Note

    You only need to include the class attribute on enough parameters to resolve the ambiguity.

You can also specify javabeans as parameters if required using the @JavaBeanValue annotation or <javabean> element. In this particular case the annotation is not very useful as it can only call the default constructor of the javabean class to create an instance. If you want to use a different constructor or inject properties then you should use the XML element instead.


@Constructor
public TestConstructorBean(@StringValue("this is a test constructor") String testString, @JavaBeanValue("org.jboss.test.Person") Person testPerson) {
    ...
}

<bean name="Test" class="org.jboss.example.constructor.TestConstructorBean">
    <constructor>
        <parameter>this is a test constructor</parameter>
        <parameter>
            <javabean xmlns="urn:jboss:javabean:2.0" class="org.jboss.test.Person">
                <constructor>
                    <property name="age">21</property>
                </constructor>
                <property name="firstName">Mark</property>
                <property name="lastName">Newton</property>
            </javabean>
        </parameter>
    </constructor>
</bean>

Finally you can inject references to other beans or their properties into constructor parameters giving you complete control of the construction of your POJOs.


@Constructor
public TestConstructorBean(@Inject(bean="TestPerson") Person testPerson, @Inject(bean="TestAddress", property="street") String street) {
    ...
}

<bean name="Test" class="org.jboss.example.constructor.TestConstructorBean">
    <constructor>
        <parameter>
            <inject bean="TestPerson"/>
        </parameter>
        <parameter>
            <inject bean="TestAddress" property="street"/>
        </parameter>
    </constructor>
</bean>