首页 > 其他分享 >spring——Spring 注入集合——在集合中设置对象类型的值

spring——Spring 注入集合——在集合中设置对象类型的值

时间:2022-12-10 18:13:44浏览次数:53  
标签:Spring courseName spring list private import 集合 org public

在集合中设置对象类型的值

在上面的示例中,都是在集合中通过 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());

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

执行结果:

 

标签:Spring,courseName,spring,list,private,import,集合,org,public
From: https://www.cnblogs.com/xiaobaibailongma/p/16972012.html

相关文章