Spring学习笔记(04-数组、集合类型的注入)

1.实体类准备

创建一个实体类MyBean,封装数组、集合,用于注入测试

package com.xiao.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
 * @Author 笑笑
 * @Date 19:02 2018/06/04
 */
public class MyBean {

    //数组类型
    private Object[] arr;
    //list集合
    private List list;
    //map集合
    private Map map;
    //properties集合
    private Properties prop;

    public Object[] getArr() {
        return arr;
    }

    public void setArr(Object[] arr) {
        this.arr = arr;
    }

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Properties getProp() {
        return prop;
    }

    public void setProp(Properties prop) {
        this.prop = prop;
    }

    @Override
    public String toString() {
        return "MyBean{" +
                "arr=" + Arrays.toString(arr) +
                ", list=" + list +
                ", map=" + map +
                ", prop=" + prop +
                '}';
    }
}

工程目录如下


2.数组类型的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns="http://www.springframework.org/schema/beans"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">


	 
       <bean name="user" class="com.xiao.pojo.User">
		 <property name="id" 	   value="1"></property>
		 <property name="username" value="xiaoxiao1"></property>
		 <property name="password" value="123456"></property>
	 </bean>

	   <!--
		数组类型的注入,可以直接注入值,也可以注入其他对象
	   -->
	   <bean name="MyBean" class="com.xiao.pojo.MyBean">
		<property name="arr">
		    <array>
		        <value>abc</value>
		        <value>dfg</value>
		        <ref bean="user"></ref>
		    </array>
		</property>
 	   </bean>

</beans>

测试类

import com.xiao.pojo.MyBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
 * @Author 笑笑
 * @Date 21:09 2018/05/31
 */
public class SpringTest_01 {

    @Test
    public void  test(){
        //创建容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从容器中获取MyBean对象
        MyBean myBean = (MyBean) ac.getBean("MyBean");
        //输出myBean对象
        System.out.println(myBean);
    }

}

运行测试类,输入结果如下


3.List集合类型注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns="http://www.springframework.org/schema/beans"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">


	
        <bean name="user" class="com.xiao.pojo.User">
		 <property name="id" 	   value="1"></property>
		 <property name="username" value="xiaoxiao1"></property>
		 <property name="password" value="123456"></property>
	 </bean>

	   <!--
		list集合类型的注入,可以直接注入值,也可以注入其他对象
	   -->
	   <bean name="MyBean" class="com.xiao.pojo.MyBean">
		<property name="list">
	            <list>
		        <value>abc</value>
		        <value>dfg</value>
		        <ref bean="user"></ref>
		    </list>
		</property>
 	   </bean>

</beans>

运行测试类,输入结果如下


4.Map集合类型的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns="http://www.springframework.org/schema/beans"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">


	 
       <bean name="user" class="com.xiao.pojo.User">
		 <property name="id" 	   value="1"></property>
		 <property name="username" value="xiaoxiao1"></property>
		 <property name="password" value="123456"></property>
	 </bean>

	   <!--
		map集合类型的注入,如果要将其他对象作为value,使用value-ref属性
	   -->
	   <bean name="MyBean" class="com.xiao.pojo.MyBean">
	        <property name="map">
		    <map>
		        <entry key="addr"  value="china"></entry>
		        <entry key="phone" value="123"></entry>
		        <entry key="user"  value-ref="user"></entry>
		    </map>
	        </property>
 	   </bean>

</beans>

运行测试类,输入结果如下


5.Properties集合类型的注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns="http://www.springframework.org/schema/beans"
	   xmlns:p="http://www.springframework.org/schema/p"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">


	
        <bean name="user" class="com.xiao.pojo.User">
		 <property name="id" 	   value="1"></property>
		 <property name="username" value="xiaoxiao1"></property>
		 <property name="password" value="123456"></property>
	 </bean>

	   <!--
		properties集合类型的注入
	   -->
	   <bean name="MyBean" class="com.xiao.pojo.MyBean">
	        <property name="prop">
		    <props>
			<prop key="addr">china</prop>
			<prop key="phone">123</prop>
			<prop key="gender">male</prop>
		    </props>
		</property>
 	   </bean>

</beans>

运行测试类,输出结果如下




猜你喜欢

转载自blog.csdn.net/u012430402/article/details/80571672
今日推荐