• Spring中注入List,Set,Map,Properties的xml文件配置方法


    下面的例子展示了如何注入

    • List – <list/>
    • Set – <set/>
    • Map – <map/>
    • Properties – <props/>

    Spring beans

    import java.util.List;
    import java.util.Map;
    import java.util.Properties;
    import java.util.Set;
      
    public class Customer
    {
        private List<Object> lists;
        private Set<Object> sets;
        private Map<Object, Object> maps;
        private Properties pros;
      
        //...
    }

    配置文件:

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
      
        <bean id="CustomerBean" class="com.mkyong.common.Customer">
      
            <!-- java.util.List -->
            <property name="lists">
                <list>
                    <value>1</value>
                    <ref bean="PersonBean" />
                    <bean class="com.mkyong.common.Person">
                        <property name="name" value="mkyongList" />
                        <property name="address" value="address" />
                        <property name="age" value="28" />
                    </bean>
                </list>
            </property>
      
            <!-- java.util.Set -->
            <property name="sets">
                <set>
                    <value>1</value>
                    <ref bean="PersonBean" />
                    <bean class="com.mkyong.common.Person">
                        <property name="name" value="mkyongSet" />
                        <property name="address" value="address" />
                        <property name="age" value="28" />
                    </bean>
                </set>
            </property>
      
            <!-- java.util.Map -->
            <property name="maps">
                <map>
                    <entry key="Key 1" value="1" />
                    <entry key="Key 2" value-ref="PersonBean" />
                    <entry key="Key 3">
                        <bean class="com.mkyong.common.Person">
                            <property name="name" value="mkyongMap" />
                            <property name="address" value="address" />
                            <property name="age" value="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>
      
        </bean>
      
        <bean id="PersonBean" class="com.mkyong.common.Person">
            <property name="name" value="mkyong1" />
            <property name="address" value="address 1" />
            <property name="age" value="28" />
        </bean>
      
    </beans>

    运行:

    public class App
    {
        public static void main( String[] args )
        {
            ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml");
      
            Customer cust = (Customer)context.getBean("CustomerBean");
            System.out.println(cust);
      
        }
    }
  • 相关阅读:
    RabbitMQ一:消息队列的认识
    RabbitMQ二:AMQP协议
    SVN中如何去除版本控制器
    Asp.net:MVC认识
    时间连接查询展示
    C#string类型总结
    JavaScript01天学习笔记分享
    UML中的类图及类图之间的关系
    23 种设计模式的分类和功能
    WCF入门
  • 原文地址:https://www.cnblogs.com/franson-2016/p/5546299.html
Copyright © 2020-2023  润新知