spring 集合属性赋值

1.person类

package com.hxzy.model;


import java.util.List;


public class Person {


private int age;
private String name;

private List<Car> cars;

//此处若没有无参构造方法,bean配置文件中会报错“No constructor with 0 arguments defined in class 'com.hxzy.model.Person'”
public Person() {
super();
}


public void setCars(List<Car> cars) {
this.cars = cars;
}

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

public void setAge(int age) {
this.age = age;
}


public Person(int age, String name, List<Car> cars) {
super();
this.age = age;
this.name = name;
this.cars = cars;
}


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

}


2.car类

package com.hxzy.model;


public class Car {


private String brand;
private String address;
private int speed;

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

public void setAddress(String address) {
this.address = address;
}

public void setSpeed(int speed) {
this.speed = speed;
}

public Car(String brand, String address, int speed) {
super();
this.brand = brand;
this.address = address;
this.speed = speed;
}

@Override
public String toString() {

return "brand : " + brand + " address : " + address + "  speed : " + speed;
}

}


3.bean配置文件

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="com.hxzy.model.Car">
        <constructor-arg type="java.lang.String">
            <value>宝马</value>
        </constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>天津</value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>200</value>
        </constructor-arg>
    </bean>
    
     <bean id="car2" class="com.hxzy.model.Car">
        <constructor-arg type="java.lang.String">
            <value>奔驰</value>
        </constructor-arg>
        <constructor-arg type="java.lang.String">
            <value>北京</value>
        </constructor-arg>
        <constructor-arg type="int">
            <value>220</value>
        </constructor-arg>
    </bean>
    
    <bean id="person2" class="com.hxzy.model.Person">
        <property name="age" value="58"> </property>
        <property name="name" value="老刘"></property>
        <property name="cars" >
            <list>
                <ref bean="car"/>
                <ref bean="car2"/>
            </list>
            
        </property>

    </bean>

</beans>

4.测试文件

AbstractApplicationContext  ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

Person man = (Person) ctx.getBean("person2");
System.out.println(man);

ctx.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值