Spring Map 注入属性 方法

Car 类

package com.lishimin.collection;

public class Car {
    private String brand;
    private int price;


    public Car(String brand, int price) {
        this.brand = brand;
        this.price = price;
    }
    public Car(){}
    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}

 NewPerson 类

package com.lishimin.collection;

import java.util.Map;

public class NewPerson {
    private String name;
    private int age;
    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 Map getCars() {
        return cars;
    }

    public void setCars(Map cars) {
        this.cars = cars;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", cars=" + cars +
                '}';
    }

    public NewPerson(String name, int age, Map cars) {
        this.name = name;
        this.age = age;
        this.cars = cars;
    }
    public NewPerson(){}

}

配置文件



        
    
        
        
    
    



        
        
        

        
        

    
    
    
        
        
    
    
        
        
    
    

        
        

        
            
                
                
                
                    
                    
                
            
        
    
    
        
        
        
               // Map 属性注入
                
                
            
        
    

main方法

import com.lishimin.Person;
import com.lishimin.collection.NewPerson;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {

        //1,创建spring的ioc容器对象
        // ClassPathXmlApplicationContext 是 applicationContext 的实现类
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");
        //2,从IOC容器对象中获取Bean

        Person person = (Person) applicationContext.getBean("person");
        System.out.println(person);
//        com.lishimin.collection.Person person3 = applicationContext.getBean("person3", com.lishimin.collection.Person.class);
        com.lishimin.collection.Person person3 = (com.lishimin.collection.Person) applicationContext.getBean("person3");
        System.out.println(person3);
        NewPerson newPerson = (NewPerson) applicationContext.getBean("person4"); //获取带有Map的bean 
        System.out.println(newPerson);

    }
}

你可能感兴趣的:(spring)