Spring之集合属性

集合属性之List

1.新建实体类

public class Car {
    
    private String brand;
    private double price;
    private int maxSped;
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getPrice() {
        return price;
    }
    public void setPrice(double price) {
        this.price = price;
    }
    public int getMaxSped() {
        return maxSped;
    }
    public void setMaxSped(int maxSped) {
        this.maxSped = maxSped;
    }
    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + ", maxSped="
                + maxSped + "]";
    }
}
import java.util.List;

public class Person {
    
    private String name;
    private int age;
    private List<Car> 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 List<Car> getCars() {
        return cars;
    }
    public void setCars(List<Car> cars) {
        this.cars = cars;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
    }
}

2.新建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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    <bean id="car" class="com.auguigu.spring.beans2.Car">
        <property name="brand" value="Audi"></property>
        <property name="price" value="30000"></property>
        <property name="maxSped" value="200"></property>
    </bean>
    
    <bean id="car2" class="com.auguigu.spring.beans2.Car">
        <property name="brand" value="Ford"></property>
        <property name="price" value="10000"></property>
        <property name="maxSped" value="120"></property>
    </bean>
    
    <bean id="person" class="com.auguigu.spring.beans2.Person">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="21"></property>
        <property name="cars">
            <list>
                <ref bean="car"/>
                <ref bean="car2"/>
            </list>
        </property>
    </bean>
</beans>

一个土豪是可以拥有两辆车的对吧

3.测试类

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

public class Main {
    
    public static void main(String[] args) {
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        Person p = (Person) ctx.getBean("person");
        System.out.println(p);
        
    }

}

4.输出结果:

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Person [name=zhangsan, age=21, cars=[Car [brand=Audi, price=30000.0, maxSped=200], Car [brand=Ford, price=10000.0, maxSped=120]]]

集合属性之Map

1.新建实体类

import java.util.Map;

public class NewPerson {
    
    private String name;
    private int age;
    private Map<String,Car> 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<String, Car> getCars() {
        return cars;
    }
    public void setCars(Map<String, Car> cars) {
        this.cars = cars;
    }
    @Override
    public String toString() {
        return "NewPerson [name=" + name + ", age=" + age + ", cars=" + cars
                + "]";
    }
}

2,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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
    <bean id="car" class="com.auguigu.spring.beans2.Car">
        <property name="brand" value="Audi"></property>
        <property name="price" value="30000"></property>
        <property name="maxSped" value="200"></property>
    </bean>
    
    <bean id="car2" class="com.auguigu.spring.beans2.Car">
        <property name="brand" value="Ford"></property>
        <property name="price" value="10000"></property>
        <property name="maxSped" value="120"></property>
    </bean>
    
    <bean id="person" class="com.auguigu.spring.beans2.Person">
        <property name="name" value="zhangsan"></property>
        <property name="age" value="21"></property>
        <property name="cars">
            <list>
                <ref bean="car"/>
                <ref bean="car2"/>
            </list>
        </property>
    </bean>
    
    
    <bean id="newPerson" class="com.auguigu.spring.beans2.NewPerson">
        <property name="name" value="Lory"></property>
        <property name="age" value="23"></property>
        <property name="cars">
            <map>
                <entry key="car1" value-ref="car"></entry>
                <entry key="car2" value-ref="car2"></entry>
            </map>
        </property>
    </bean>
</beans>

3.测试类

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

public class Main {
    
    public static void main(String[] args) {
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//        Person p = (Person) ctx.getBean("person");
//        System.out.println(p);
        NewPerson newPerson = (NewPerson) ctx.getBean("newPerson");
        System.out.println(newPerson);
        
    }

}

4.输出结果

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
NewPerson [name=Lory, age=23, cars={car1=Car [brand=Audi, price=30000.0, maxSped=200], car2=Car [brand=Ford, price=10000.0, maxSped=120]}]
Preperties:

 

posted @ 2018-03-08 11:40  凌羽1025  阅读(171)  评论(0编辑  收藏  举报