spring 级联属性

1.People类

package com.hxzy;


public class People {


private String name;
private int age;
private Car car;

//必须有无参构造方法
public People() {

}

public People(String name, int age, Car car) {
super();
this.name = name;
this.age = age;
this.car = car;
}


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

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


public void setCar(Car car) {
this.car = car;
}

//此处如果getCar方法缺少,会导致如下错误:

/*

Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'car.speed' of bean class [com.hxzy.People]: Nested property in path 'car.speed' does not exist; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'car' of bean class [com.hxzy.People]: Bean property 'car' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?


*/
public Car getCar() {
return car;
}

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

}


2.Car类

package com.hxzy;


public class Car {


private String brand;
private String corp;
private double price;
private int speed;




public Car(String brand, String corp, double price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
}

//此处如果不写,运行时报错
public void setSpeed(int speed) {
this.speed = speed;
}


@Override
public String toString() {
return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
+ ", speed=" + speed + "]";
}


}


3.配置文件

<?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="people" class="com.hxzy.People">
        <property name="name" value="王强强"></property>
        <property name="age" value="15"></property>
    </bean>
    -->
    
    <bean id="car" class="com.hxzy.Car">
        <constructor-arg value="audi" ></constructor-arg>
        <constructor-arg value="shanghai" ></constructor-arg>
        <constructor-arg value="300000" ></constructor-arg>
    </bean>
    
    <bean id="people2" class="com.hxzy.People">
        <constructor-arg value="私人"></constructor-arg>
        <constructor-arg value="25"></constructor-arg>
<constructor-arg ref="car"></constructor-arg>    
<property name="car.speed" value="300"></property>    
     </bean>

</beans>


4.


package com.hxzy;


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


public class Test {


public static void main(String[] args) {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("config.xml");
// People man = new People();

People man = (People) ctx.getBean("people2");
System.out.println(man);

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值