1. Define array, list, Map, set type attributes

Create class, define array, list, map, set type attribute, generate the corresponding set method.

package com.pingguo.spring5.collectiontype; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set; Public class Student {// 1. Private String[] courses; // 2. Private list <String> list; // 3. Private map <String, String> maps; Private set <String> set; // 4. public void setCourses(String[] courses) { this.courses = courses; } public void setList(List<String> list) { this.list = list; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public void setSets(Set<String> sets) { this.sets = sets; } public void test() { System.out.println(Arrays.toString(courses)); System.out.println(list); System.out.println(maps); System.out.println(sets); }}Copy the code

2. Perform corresponding configurations in the configuration file

<? The XML version = "1.0" encoding = "utf-8"? > <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.xsd"> <! - collection types properties of injection - > < bean id = "student" class = "com. Pingguo. Spring5. Collectiontype. Student" > <! </value> </value> </value> </value> </value> </value> </property> <! </value> </value> </value> </value> </value> </property> <! </entry> </entry> </entry> </entry> </entry> </entry> </entry> </map> </property> <! <value> <value> </value> </value> </value> </value> </set> </bean> </beans>Copy the code

Write a test class to facilitate testing.

package com.pingguo.spring5.testdemo; import com.pingguo.spring5.collectiontype.Student; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpring5Demo2 { @Test public void testCollection() { ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); Student student = context.getBean("student", Student.class); student.test(); }}Copy the code

Run test results

Process finished with exit code 0Copy the code

Inject object collection type

All the contents in the above collection are in String. If it is now an object, how do I inject it?

Ok, now add a new class, Course:

package com.pingguo.spring5.collectiontype; public class Course { private String course_name; public void setCourse_name(String course_name) { this.course_name = course_name; } @Override public String toString() { return "Course{" + "course_name='" + course_name + '\'' + '}'; }}Copy the code

Next, in the Student class, add the Course object property and generate the corresponding set method:

. . public class Student { ... . // 5. 大 学 的 private List<Course> courseList; public void setCourseList(List<Course> courseList) { this.courseList = courseList; }... . public void test() { System.out.println(Arrays.toString(courses)); System.out.println(list); System.out.println(maps); System.out.println(sets); System.out.println(courseList); }}Copy the code

Operation configuration file.

You can add multiple Course objects to the configuration file.

<! - create multiple Course object - > < bean id = "course1" class = "com. Pingguo. Spring5. Collectiontype. Course" > < property name = "course_name" Value = "hu's pastry class >" < / property > < / bean > < bean id = "course2" class = "com. Pingguo. Spring5. Collectiontype. Course" > < property Name ="course_name" value=" course_name" ></property> </bean>Copy the code

Then use the REF tag during injection.

<! -- Inject the list type, Value is object --> <property name="courseList"> <list> <ref bean="course1"></ref> <ref bean="course2"></ref> </list> </property>Copy the code

OK, now run the test class and see the result:

[course_name=' course_name ', Course{course_name=' course_name ', Course{course_name=' course_name ', Course{course_name=' course_name '}]Copy the code

Injection succeeded.

Extract the part of the injection set

Where the above collection is injected, it feels cumbersome when there are multiple values. It would be better if we could extract this part.

    <property name="courseList">
        <list>
            <ref bean="course1"></ref>
            <ref bean="course2"></ref>
        </list>
    </property>
Copy the code

Now create a new Book class to demonstrate:

package com.pingguo.spring5.collectiontype; import java.util.List; public class Book { private List<String> list; public void setList(List<String> list) { this.list = list; } public void test() { System.out.println(list); }}Copy the code

1. Introduce namespace util

For convenience, create a new bean2.xml with the namespace util in it.

<? The XML version = "1.0" encoding = "utf-8"? > <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> </beans>Copy the code

Places to add:

2. Use the util tag to complete the extraction of collection injection

Take extracting the list collection as an example.

<! --> <util:list id="bookList"> <value> How mysql runs </value> <value> <value> <value>  </util:list>Copy the code

Then, using the extracted collection, use the REF attribute.

    <bean id="book" class="com.pingguo.spring5.collectiontype.Book">
        <property name="list" ref="bookList"></property>
    </bean>
Copy the code

To test this, create a new test method, testCollection2().

    @Test
    public void testCollection2() {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean2.xml");
        Book book = context.getBean("book", Book.class);
        book.test();
    }
Copy the code

Run the test method:

Process Finished with Exit Code 0Copy the code

Injection succeeded.