JBoss.org Community Documentation
Using PropertyEditors to create instances of objects from strings is fine in many situations but sometimes you need to inject a return value from a method call. As the method is responsible for creating the value we refer to it as a value factory. Hence the @ValueFactory annotation or <value-factory> element can be used in place of @StringValue or <value> to call a method and inject the returned value during deployment.
@ValueFactory(bean="SimpleBean",
method="getFullName")
public void setName(String name) {
...
}
<bean name="PropHolder" class="org.jboss.test.kernel.config.support.PropHolder">
<property name="name">
<value-factory bean="SimpleBean" method="getFullName"/>
</property>
</bean>
If you need to pass any parameters to the method then you can use multiple @Parameter annotations or nested <parameter> elements.
@ValueFactory(bean="SimpleBean",
method="getFullName",
parameters="{@Parameter(string=@StringValue("Bob")),
@Parameter(string=@StringValue("Smith"))}")
public void setName(String name) {
...
}
<bean name="PropHolder" class="org.jboss.test.kernel.config.support.PropHolder">
<property name="name">
<value-factory bean="SimpleBean" method="getFullName">
<parameter>Bob</parameter>
<parameter>Smith</parameter>
</value-factory>
</property>
</bean>
Alternatively if only one parameter is required then you can use the parameter
attribute to create a shorthand version. However this only works for values that can be created from strings.
@ValueFactory(bean="SimpleBean",
method="getFullName",
parameter="@Parameter(string=@StringValue("Bob"))")
public void setName(String name) {
...
}
<bean name="PropHolder" class="org.jboss.test.kernel.config.support.PropHolder">
<property name="name">
<value-factory bean="SimpleBean" method="getFullName" parameter="Bob"/>
</property>
</bean>
It is also possible to specify a default value to use in case the method returns null. This is done using the defaultValue
/default
attribute which like the parameter
attribute only accepts values created using strings.
@ValueFactory(bean="SimpleBean",
method="getFullName",
defaultValue="Mark Newton")
public void setName(String name) {
...
}
<bean name="PropHolder" class="org.jboss.test.kernel.config.support.PropHolder">
<property name="name">
<value-factory bean="SimpleBean" method="getFullName" default="Mark Newton"/>
</property>
</bean>