spring之为级联集合属性进行赋值

Car.java

package com.gong.spring.beans;

public class Car {
    
    public Car() {
    }

    public Car(String name) {
        this.name = name;
    }
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Car [name=" + name + "]";
    }
    
}

Student.java

package com.gong.spring.beans;

import java.util.List;

public class Student {
    
    private String name;
    private int age;
    private double score;
    private List cars;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    
    public List getCars() {
        return cars;
    }
    public void setCars(List cars) {
        this.cars = cars;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", cars=" + cars + "]";
    }
    
    
}

applicationContext.xml



    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">
    
    class="com.gong.spring.beans.Car">
        
    
    class="com.gong.spring.beans.Car">
        
    
    class="com.gong.spring.beans.Car">
        
    
    
    class="com.gong.spring.beans.Student">
        
        
        
        
            
            
                
                
                
            
        
        
    
    

那么,如何对map集合类型进行赋值呢?

修改Student.java

package com.gong.spring.beans;
import java.util.Map;

public class Student {
    
    private String name;
    private int age;
    private double score;
    private Map cars;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public double getScore() {
        return score;
    }
    public void setScore(double score) {
        this.score = score;
    }
    
    public Map getCars() {
        return cars;
    }
    public void setCars(Map cars) {
        this.cars = cars;
    }
    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + ", score=" + score + ", cars=" + cars + "]";
    }
    
    
}

主要是将car变为Map类型,然后新加getter和setter方法。

在applicationContext.xml中:



    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">
    
    class="com.gong.spring.beans.Car">
        
    
    class="com.gong.spring.beans.Car">
        
    
    class="com.gong.spring.beans.Car">
        
    
    
    class="com.gong.spring.beans.Student">
        
        
        
        
            
            
                
                
                
            
        
        
    
    

最后在Main.java中调用:

package com.gong.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //1.创建spring的IOC容器对象
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //2.从容器中获取Bean实例
        Student student = (Student) ctx.getBean("student"); 
        System.out.println(student.toString());
    }
}

输出:

你可能感兴趣的:(spring之为级联集合属性进行赋值)