1. 程式人生 > >複雜型別注入(陣列,集合,properties)

複雜型別注入(陣列,集合,properties)

複雜型別注入

  1. 陣列注入
  2. list集合注入,注入值和物件
  3. set集合注入,注入值和物件
  4. map集合注入,注入值和物件
  5. properties注入,注入值 springConfig.xml程式碼為:
	<!--被注入的物件-->
	<bean id="u1" class="com.fdk.been.Users" p:name="hah"></bean>
	
    <!--複雜型別注入-->
    <bean id="c1" class="com.fdk.been.counts">
        <!--陣列注入-->
        <property name="objects">
            <list>
                <value>1hao</value>
                <value>2hao</value>
                <value>3hao</value>
                <value>4hao</value>

            </list>
        </property>

        <!--list集合注入,注入值和物件-->
        <property name="list">
            <list>
                <value>張三</value>
                <ref bean="u1"></ref>   <!--list集合注入物件-->
            </list>
        </property>
        
        <!--set集合注入,注入值和物件-->
        <property name="set">
            <set>
                <value>set集合</value>
                <ref bean="u1"></ref>
            </set>
        </property>
        
        <!--map集合注入,注入值和物件-->
        <property name="map">
            <map>
                <entry key="age" value="20"></entry>
                <entry key="u" value-ref="u1"></entry>
            </map>
        </property>
        
        <!--properties注入,注入值-->
        <property name="properties">
            <props>
                <prop key="driver">com.mysql...</prop>
                <prop key="username">root</prop>
            </props>
        </property>
    </bean>

測試程式碼為

陣列測試

//陣列型別注入值
    @Test
    public  void test10(){
        ApplicationContext app=new ClassPathXmlApplicationContext("springConfig3.xml");
        counts c=(counts)app.getBean("c1");
        Object[] objects=c.getObjects();
        for (Object o:objects) {
            System.out.println(o);
        }
    }

list集合注入測試

 //List集註入值和物件
    @Test
    public  void test11(){
        ApplicationContext app=new ClassPathXmlApplicationContext("springConfig3.xml");
        counts c=(counts)app.getBean("c1");
        List list=c.getList();
        for (Object o:list) {
            System.out.println(o);
        }
    }

set集合注入測試

 //set集註入值和物件
    @Test
    public  void test12(){
        ApplicationContext app=new ClassPathXmlApplicationContext("springConfig3.xml");
        counts c=(counts)app.getBean("c1");
        Set set=c.getSet();
        for (Object o:set) {
            System.out.println(o);
        }
    }

map注入測試

//map集合注入值和物件
    @Test
    public  void test13(){
        ApplicationContext app=new ClassPathXmlApplicationContext("springConfig3.xml");
        counts c=(counts)app.getBean("c1");
        Map<String,Object> map=c.getMap();
        Set<String> keys=map.keySet();
        for (String s: keys) {
            System.out.println(s+"   "+map.get(s));
        }
    }

properties注入測試

@Test
    public  void test14(){
        ApplicationContext app=new ClassPathXmlApplicationContext("springConfig3.xml");
        counts c=(counts)app.getBean("c1");
        Properties p=c.getProperties();
        System.out.println(p.get("driver"));
        System.out.println(p.get("username"));
    }