Spring study notes set of injection line 1.2

Spring collections injection:

We create a test class in each of the following classes for a test run with

Demo:

public class Test_Test{
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Test test = ctx.getBean("test",Test.class);
        System.out.println(test);
    }
}

1. The injection array data:

Demo: Create a Test class

public class Test{
    private String msg[];
    private int data[];

    public String[] getMsg() {
        return msg;
    }

    public void setMsg(String[] msg) {
        this.msg = msg;
    }

    public int[] getData() {
        return data;
    }

    public void setData(int[] data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "msg=" + Arrays.toString(this.msg)+",data="+Arrays.toString(this.data);
    }
}

Two arrays may be received object information, in the use of the array data are provided corresponding to the data type. 

<bean id="test" class="com.project.Test.Test"><!—路径以自己的为准-->
    <property name="data">
        <array value-type="java.lang.Integer">
            <value>10</value>
            <value>20</value>
            <value>30</value>
        </array>
    </property>
    <property name="msg">
        <array value-type="java.lang.String">
            <value>SpringBoot</value>
            <value>Spring</value>
            <value>SpringMVC</value>
        </array>
    </property>
</bean>

  Click on operating results Test_Test test class:

2. List the set of operating an array of objects:

public class Item2 {
    private List<String> msg;
    public void setMsg(List<String> msg) {
        this.msg = msg;
    }
    public List<String> getMsg() {
        return msg;
    }
    @Override
    public String toString() {
        return "msg= "+this.msg;
    }
}

Add item2 applicationContext.xml object configuration file, the array type and value provided 

<bean id="item2" class="com.project.Demo2.Item2">
    <property name="msg">
        <array value-type="java.lang.String">
            <value>"Java工程师"</value>
            <value>"信息安全工程师"</value>
        </array>
    </property>
</bean>

Set set out implantation may duplicate values, and the operation method is the same as List

ItemTest class runs View results

public class TestItem2 {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Item2 item2 = ctx.getBean("item2",Item2.class);
        System.out.println(item2);
    }
}

3. Fill Map collection

More than a class operation, using the Map to create a property: 

public class Item2 {
    private Map<Integer,String> msg;
    public Map<Integer, String> getMsg() {
        return msg;
    }
    public void setMsg(Map<Integer, String> msg) {
        this.msg = msg;
    }    
}

Set Map Properties of objects inside the bean key:

<bean id="item2" class="com.project.Demo2.Item2">
    <property name="msg">
        <map key-type="java.lang.Integer" value-type="java.lang.String">
            <entry key="1" value="Spring"></entry>
            <entry key="2" value="MyBatis"></entry>
        </map>
    </property>
</bean>

4. the Properties injection

In the preparation process of developing a set of injection profile is the most common type Properties.

Creating Properties types of properties, initialized:

public class Item2 {
    //只能设置String类型的数据
    private Properties msg;
    public Properties getMsg() {
        return msg;
    }
    public void setMsg(Properties msg) {
        this.msg = msg;
    }
    @Override
    public String toString() {
        return "msg= "+this.msg;
    }
}

xml file settings Properties types of keys :

    <bean id="item2" class="com.project.Demo2.Item2">
        <property name="msg">
            <props>
                <prop key="java">www.java.com</prop>
                <prop key="spring">www.spring.cn</prop>
            </props>
        </property>
    </bean>

Click on operating results ItemTest test class: 

6.Spring achieve internal documents referenced definitions : 

  Creating Dept department class, achieve a reference to the Employee class internal staff attributes:


public class Detp {
    private int DetpId;
    private String DetpName;
    private List<Employee> employees;
    public List<Employee> getEmployees() {
        return employees;
    }
    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }

    public int getDetpId() {
        return DetpId;
    }
    public String getDetpName() {
        return DetpName;
    }
    public void setDetpId(int detpId) {
        DetpId = detpId;
    }
    public void setDetpName(String detpName) {
        DetpName = detpName;
    }
    @Override
    public String toString() {
        return "部门编号: "+this.DetpId+",部门名称: "+this.DetpName+",雇员信息: "+this.employees;
    }
}

Then xml described above structural relationship profile :

<bean id="dept" class="com.project.Test.Detp">
    <property name="detpName" value="开发部"/>
    <property name="detpId" value="1"/>
    <property name="employees">
	<!--指定数据类型为Employee类-->
        <list value-type="com.project.Test.Employee">	
    <!--使用反射调用员工对象--> 
            <ref bean="empA"/>		
            <ref bean="empB"/>
            <ref bean="empC"/>
        </list>
    </property>
</bean>
<!--设置3个不同的bean对象-->
<bean id="empA" class="com.project.Test.Employee">
    <property name="emp_id" value="15811"/>
    <property name="emp_name" value="EmployeeA"/>
    <!--ref引用其他Bean对象的内容-->
    <property name="detp" ref="dept"/>
</bean>
<bean id="empB" class="com.project.Test.Employee">
    <property name="emp_id" value="15812"/>
    <property name="emp_name" value="EmployeeB"/>
    <property name="detp" ref="dept"/>
</bean>
<bean id="empC" class="com.project.Test.Employee">
    <property name="emp_id" value="15813"/>
    <property name="emp_name" value="EmployeeC"/>
    <property name="detp" ref="dept"/>
</bean>

Click on Employee operating results of the test class :

 

Published 58 original articles · won praise 31 · views 40000 +

Guess you like

Origin blog.csdn.net/qq_37504771/article/details/103154234