xiaoke_code 2015-12-10 07:14 采纳率: 0%
浏览 7210
已采纳

spring 依赖注入的困惑 关于子类和父类

有一个Son类
package cn.extend;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

public class Son extends Father {

}

还有一个父类Father
package cn.extend;

public class Father {
private String name;
protected int age;
public Long maney;
private GrandFather grandFather;//这里是private私有成员
public GrandFather getGrandFather() {
return grandFather;
}
public final void setGrandFather(GrandFather grandFather) {
//这里声明final 子类不可以重写这个方法
this.grandFather = grandFather;
System.out.println("father final set Method---->"+this.grandFather.showFather());
}
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 Long getManey() {
return maney;
}
public void setManey(Long maney) {
this.maney = maney;
}
}

一个爷爷类GrandFather
package cn.extend;

public class GrandFather {
public String showFather() {
return "i am your father";
}
}

那么我们在spring的applicationContext.xml配置文件中
<?xml version="1.0" encoding="UTF-8"?>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">










问题来了 我在Son的<bean 配置里面写上了父类所有的成员属性
而且我的setGrandFather()方法还是final的

测试类

public class TestExtends {
@Test
public void testExtend(){
ApplicationContext context=new ClassPathXmlApplicationContext("cn/extend/applicationContext.xml");
Father father=(Father)context.getBean("father");
Son son=(Son)context.getBean("son");
System.out.println(father.getAge()+father.getName());
System.out.println("father--->"+father.getGrandFather());
System.out.println(son.getAge()+son.getName());
System.out.println("son--->"+son.getGrandFather().showFather());
}
}

输出的结果
father final set Method---->i am your father(这句话说明父类的这个属性已经被赋值了)
0null(父类的属性也都是为空,但是神奇的是子类可以获得到)
father--->null(问题的是,为什么从容器中取出来的father调用getGrandFather()方法就是为null的呢)
22kenan
son--->i am your father(而且子类是可以获得父类的GrandParent()还可以拿出来,但是father类就是null,

  • 写回答

3条回答

  • xiaoke_code 2015-12-10 07:15
    关注

    补充一下我的applicationContext.xml配置文件是这样的额
    <?xml version="1.0" encoding="UTF-8"?>
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">










    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘