实体类:
package com.langchao.autoware;
/**
@Date: 2019\2\24 10:26
**/
public class Car
{
private String brand;
private String corp;
private double prize;
private int speed;
public String getBrand()
{
return brand;
}
public void setBrand(String brand)
{
this.brand = brand;
}
public String getCorp()
{
return corp;
}
public void setCorp(String corp)
{
this.corp = corp;
}
public double getPrize()
{
return prize;
}
public void setPrize(double prize)
{
this.prize = prize;
}
public int getSpeed()
{
return speed;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public Car(String brand, String corp, double prize)
{
this.brand = brand;
this.corp = corp;
this.prize = prize;
}
public Car(String brand, String corp, int speed)
{
this.brand = brand;
this.corp = corp;
this.speed = speed;
}
public Car(String brand, String corp, double prize, int speed)
{
this.brand = brand;
this.corp = corp;
this.prize = prize;
this.speed = speed;
}
public Car()
{
}
@Override
public String toString()
{
final StringBuilder sb = new StringBuilder("Car{");
sb.append("brand='").append(brand).append(''');
sb.append(", corp='").append(corp).append(''');
sb.append(", prize=").append(prize);
sb.append(", speed=").append(speed);
sb.append('}');
return sb.toString();
}
}
Spring中的配置:
<bean id="car" class="com.langchao.autoware.Car" p:brand="BiyaDi" p:corp="zhongguo" p:prize="323233" p:speed="212"></bean>
<bean id="person3" class="com.langchao.autoware.Person" p:name="Tom" p:age="33" p:sex="男" p:address-ref="address"
depends-on="car"></bean>
打印结果:
person{name='Tom', age=33, sex=男, car=null, address=Address{city='Hangzhou', street='WenSanLu'}}
depends-on 的意思是person3需要car,但这并不意味着car会实例化
那怎么样才能使这个car在person3需要的时候实例化呢?
怎么解,急急急呀!
– 安能倚天屠龙 5年前