首页新闻找找看学习计划

Spring配置中使用dependency-on依赖对象的时候,对象一直是null?

0
悬赏园豆:5 [待解决问题]

实体类:
package com.langchao.autoware;

/**

  • @ClassName: Car
  • @description: 车辆信息的Bean
  • @author: YiTianTuLong
  • @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'}}

安能倚天屠龙的主页 安能倚天屠龙 | 初学一级 | 园豆:197
提问于:2019-03-03 14:57

怎么解,急急急呀!

安能倚天屠龙 8个月前
< >
分享
所有回答(1)
0

depends-on 的意思是person3需要car,但这并不意味着car会实例化

微微_echo | 园豆:202 (菜鸟二级) | 2019-04-28 18:59

那怎么样才能使这个car在person3需要的时候实例化呢?

支持(0) 反对(0) 安能倚天屠龙 | 园豆:197 (初学一级) | 2019-07-29 11:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册