1. 程式人生 > 其它 >spring——Spring 注入集合——在集合中設定物件型別的值

spring——Spring 注入集合——在集合中設定物件型別的值

在集合中設定物件型別的值

在上面的示例中,都是在集合中通過 value 屬性設定的普通型別的值,我們還可以通過 ref 屬性在注入到 Bean 的集合中設定物件型別的值。

1. 在 my-spring-demo4 專案的 net.biancheng.c 包中,建立一個名為 Course 的類,程式碼如下。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package net.biancheng.c;   import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;   public class Course {     private static final Log LOGGER = LogFactory.getLog(Course.class);     //課程編號     private Integer courseId;     //課程名稱     private String courseName;       public void setCourseId(Integer courseId) {         this
.courseId = courseId;     }       public void setCourseName(String courseName) {         this.courseName = courseName;     }       @Override     public String toString() {         return "Course{" +                 "courseId=" + courseId +                 ", courseName='" + courseName + '\
'' +                 '}';     } }

  

2. 將 JavaCollection 中的程式碼修改成以下形式。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 package net.biancheng.c;   import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Set;   public class JavaCollection {     //1 陣列型別屬性     private Course[] courses;     //2 list 集合型別屬性     private List<String> list;     //3 map 集合型別屬性     private Map<String, String> maps;     //4 set 集合型別屬性     private Set<String> sets;       public void setCourses(Course[] 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;     }       @Override     public String toString() {         return "JavaCollection{" +                 "courses=" + Arrays.toString(courses) +                 ", list=" + list +                 ", maps=" + maps +                 ", sets=" + sets +                 '}';     } }

  

3. 將 Beans.xml 中配置修改成以下內容。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 <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-3.0.xsd">       <bean id="course" class="net.biancheng.c.Course">         <property name="courseId" value="1"></property>         <property name="courseName" value="Java課程"></property>     </bean>     <bean id="course2" class="net.biancheng.c.Course">         <property name="courseId" value="2"></property>         <property name="courseName" value="PHP課程"></property>     </bean>       <bean id="course3" class="net.biancheng.c.Course">         <property name="courseId" value="3"></property>         <property name="courseName" value="C語言課程"></property>     </bean>       <bean id="javaCollection" class="net.biancheng.c.JavaCollection">         <!--陣列型別-->         <property name="courses">             <array>                 <ref bean="course"></ref>                 <ref bean="course2"></ref>                 <ref bean="course3"></ref>             </array>         </property>         <!--List 型別-->         <property name="list">             <list>                 <value>張三</value>                 <value>李四</value>                 <value>王五</value>                 <value>趙六</value>             </list>         </property>         <!--Map 型別-->         <property name="maps">             <map>                 <entry key="JAVA" value="java"></entry>                 <entry key="PHP" value="php"></entry>             </map>         </property>         <!--Set 型別-->         <property name="sets">             <set>                 <value>MySQL</value>                 <value>Redis</value>             </set>         </property>     </bean> </beans>

  

========================================================================================

專案依賴:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>ssw</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.6</version>
        </dependency>
    </dependencies>

</project>

javacollection集合:

package org.example;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JavaCollection {
    //1 陣列型別屬性
    private Course[] courses;
    //2 list 集合型別屬性
    private List<String> list;
    //3 map 集合型別屬性
    private Map<String, String> maps;
    //4 set 集合型別屬性
    private Set<String> sets;

    public void setCourses(Course[] 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;
    }

    @Override
    public String toString() {
        return "JavaCollection{" +
                "courses=" + Arrays.toString(courses) +
                ", list=" + list +
                ", maps=" + maps +
                ", sets=" + sets +
                '}';
    }
}

course集合:

package org.example;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Course {
    private static final Log LOGGER = LogFactory.getLog(Course.class);
    //課程編號
    private Integer courseId;
    //課程名稱
    private String courseName;

    public void setCourseId(Integer courseId) {
        this.courseId = courseId;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    @Override
    public String toString() {
        return "Course{" +
                "courseId=" + courseId +
                ", courseName='" + courseName + '\'' +
                '}';
    }
}

bean.xml:

<?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
       https://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="course" class="org.example.Course">
        <property name="courseId" value="1"></property>
        <property name="courseName" value="Java課程"></property>
    </bean>
    <bean id="course2" class="org.example.Course">
        <property name="courseId" value="2"></property>
        <property name="courseName" value="PHP課程"></property>
    </bean>

    <bean id="course3" class="org.example.Course">
        <property name="courseId" value="3"></property>
        <property name="courseName" value="C語言課程"></property>
    </bean>

    <bean id="javaCollection" class="org.example.JavaCollection">
        <!--陣列型別-->
        <property name="courses">
            <array>
                <ref bean="course"></ref>
                <ref bean="course2"></ref>
                <ref bean="course3"></ref>
            </array>
        </property>
        <!--List 型別-->
        <property name="list">
            <list>
                <value>張三</value>
                <value>李四</value>
                <value>王五</value>
                <value>趙六</value>
            </list>
        </property>
        <!--Map 型別-->
        <property name="maps">
            <map>
                <entry key="JAVA" value="java"></entry>
                <entry key="PHP" value="php"></entry>
            </map>
        </property>
        <!--Set 型別-->
        <property name="sets">
            <set>
                <value>MySQL</value>
                <value>Redis</value>
            </set>
        </property>
    </bean>



</beans>

執行:

package org.example;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main
{
    private static final Log LOGGER = LogFactory.getLog(Main.class);

    public static void main(String[] args)
    {
        //獲取 ApplicationContext 容器
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");

        JavaCollection javaCollection = context.getBean("javaCollection", JavaCollection.class);

        LOGGER.info(javaCollection.toString());

        System.out.println(javaCollection.toString());

    }

}

執行結果: