首页 > 代码库 > spring List,Set,Map,Properties,array的配置文件注入方式

spring List,Set,Map,Properties,array的配置文件注入方式

虽然不多,但是有时候在实现的时候,我们还是希望某些参数或者属性通过集合()的方式注入进来,比如配置表参数列表,addresslist,亦或是三方库等等。因为这种改动不是很多,经常一时想不起来,今天做个类通用业务AOP的时候用到,顺便记录下:

    <bean id="CustomerBean" class="com.mkyong.common.Customer">
        <!-- java.util.List -->
        <property name="lists">
            <list>
                    <ref bean="PersonBean" />
                <bean class="com.mkyong.common.Person">
                    <property name="name" value="http://www.mamicode.com/mkyongList" />
                    <property name="address" value="http://www.mamicode.com/address" />
                    <property name="age" value="http://www.mamicode.com/28" />
                </bean>
            </list>
        </property>
  
        <!-- java.util.Set -->
        <property name="sets">
            <set>
                    <ref bean="PersonBean" />
                <bean class="com.mkyong.common.Person">
                    <property name="name" value="http://www.mamicode.com/mkyongSet" />
                    <property name="address" value="http://www.mamicode.com/address" />
                    <property name="age" value="http://www.mamicode.com/28" />
                </bean>
            </set>
        </property>
  
        <!-- java.util.Map -->
        <property name="maps">
            <map>
                <entry key="Key 1" value="http://www.mamicode.com/1" />
                <entry key="Key 2" value-ref="PersonBean" />
                <entry key="Key 3">
                    <bean class="com.mkyong.common.Person">
                        <property name="name" value="http://www.mamicode.com/mkyongMap" />
                        <property name="address" value="http://www.mamicode.com/address" />
                        <property name="age" value="http://www.mamicode.com/28" />
                    </bean>
                </entry>
            </map>
        </property>
  
        <!-- java.util.Properties -->
        <property name="pros">
            <props>
                <prop key="admin">admin@nospam.com</prop>
                <prop key="support">support@nospam.com</prop>
            </props>
        </property>
        <!--array 注入例子-->
        <property name="array">   
           <list>   
              <value>array1</value>   
              <value>array2</value>   
           </list>   
        </property> 
    </bean>
    <bean id="PersonBean" class="com.mkyong.common.Person">
        <property name="name" value="http://www.mamicode.com/mkyong1" />
        <property name="address" value="http://www.mamicode.com/address 1" />
        <property name="age" value="http://www.mamicode.com/28" />
    </bean>

spring List,Set,Map,Properties,array的配置文件注入方式