[ start | index | login ]

Enums

Created by mpecher. Last edited by mpecher, 3 years and 241 days ago. Viewed 2,497 times. #1
[edit] [rdf]
labels
attachments

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>
no comments | post comment

Menu:
Java & J2EE
Development
Books

Help:
Help FAQ
Formatting


< March 2010 >
SunMonTueWedThuFriSat
123456
78910111213
14151617181920
21222324252627
28293031


Logged in Users: (1)
… and 254 Guests.



Disclaimer: Views and opinions are that of the individual author, and not that of Marand Custom Solutions. This site is an open forum for technical content, and the company accepts no liability for any content or view expressed.