zoukankan      html  css  js  c++  java
  • 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:
  • 相关阅读:
    NSTimer与循环引用
    Swift类实例与循环引用的解决
    Swift运算符函数与自定义运算符
    Swift延迟存储属性
    Swift枚举-相关值与递归枚举
    互斥锁、自旋锁、dispatch_once性能对比
    Swift闭包与简化
    原子属性和使用互斥锁实现的属性的性能对比
    [HDOJ]_PID_1004_Let the Balloon Rise
    [HDOJ]_PID_2087_剪花布条
  • 原文地址:https://www.cnblogs.com/sdnu-zhang/p/8527479.html
Copyright © 2011-2022 走看看