給自己做個筆記...
有時會用到配置文件中配置一下映射關系,方便以後擴展.此時可采用集合類型的bean配置方式配置.程序中直接注入即可.
map類型的:
<!-- 舊版方式,無需util包 --> <bean id="emails" class="org.springframework.beans.factory.config.MapFactoryBean"> <property name="sourceMap"> <map> <entry key="pechorin" value="[email protected]"/> <entry key="raskolnikov" value="[email protected]"/> <entry key="stavrogin" value="[email protected]"/> <entry key="porfiry" value="[email protected]"/> </map> </property> </bean> <!-- 新版方式,需util包,灰字部分可省略 --> <util:map id="emails" map-class="java.util.TreeMap"> <entry key="pechorin" value="[email protected]"/> <entry key="raskolnikov" value="[email protected]"/> <entry key="stavrogin" value="[email protected]"/> <entry key="porfiry" value="[email protected]"/> </util:map>
List類型的:
<!-- 舊版方式 --> <bean id="emails" class="org.springframework.beans.factory.config.ListFactoryBean"> <property name="sourceList"> <list> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </list> </property> </bean> <!-- 新版方式,灰字可省 --> <util:list id="emails" list-class="java.util.LinkedList"> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </util:list>
Set類型的:
<!-- 舊版方式 --> <bean id="emails" class="org.springframework.beans.factory.config.SetFactoryBean"> <property name="sourceSet"> <set> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </set> </property> </bean> <!-- 新版方式,灰字可省 --> <util:set id="emails" set-class="java.util.TreeSet"> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> <value>[email protected]</value> </util:set>
基本類型的(int舉例,其它同理):
<bean id="countNum" class="java.lang.Integer"> <constructor-arg value="5"/> </bean>
<bean id="countMessage" class="java.lang.String"> <constructor-arg value="5"/> </bean>