Injecting enums in Spring
To inject java.commons.lang.Enum, we need to use the static method and pass in the String. ie given:
import org.apache.commons.lang.enum.Enum;public final class SortEnum extends Enum { /** ascending sort order. */
public static final SortEnum ASC = new SortEnum("ASC"); /** descending sort. */
public static final SortEnum DESC = new SortEnum("DESC"); /** no sort order specified. */
public static final SortEnum NO_SORT = new SortEnum("NO_SORT"); /**
* callers should use convience methods.
* @param dir to sort
*/
private SortEnum(String dir) {
super(dir);
} /**
* SortEnum to use.
* @param dir requied
* @return enum represention for the dir
*/
public static SortEnum getEnum(String dir) {
return (SortEnum) getEnum(SortEnum.class, dir);
}
}
<beans>
<bean id="ASC"
class="com.marandcustomsolutions.SortOrder"
factory-method="getEnum" singleton="true">
<constructor-arg><value>ASC</value> </constructor-arg>
</bean>
</beans>
Injecting list, set, map, and props
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setPeople(java.util.Properties) call -->
<property name="people">
<props>
<prop key="HarryPotter">The magic property</prop>
<prop key="JerrySeinfeld">The funny property</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource"/>
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry>
<key><value>yup an entry</value></key>
<value>just some string</value>
</entry>
<entry>
<key><value>yup a ref</value></key>
<ref bean="myDataSource"/>
</entry>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource"/>
</set>
</property>