Spring Bean标签中abstract和parent属性

Spring中父子关系的如何配置:

   1)创建父类

public class Peson {

	private String pname; // 字符串类型

	public String getPname() {
		return pname;
	}

	public void setPname(String pname) {
		this.pname = pname;
	}

}

  2)创建子类

public class Student extends Peson {

	public void say(){
		System.out.println(this.getPname()+"继承关系!");
	}
}

   a)在bean中配置abstract属性为”true”,则spring容器不会为该类创建对象。

<!-- person object 
	
	abstract="true": 代表这个类是抽象类
	-->
	<bean id="peson" class="com.hlx.extend.Peson" abstract="true">
		<property name="pname" value="加菲猫" />
	</bean>
  b).在bean中配置parent属性,则可以让该bean继承父类属性的值。

	<!-- student object -->
<!-- 0)	<bean id="student" class="com.hlx.extend.Student" /> -->

	<bean id="student"  parent="peson">
    	<property name="pname" value="fatter" />
	</bean>

c)测试

ApplicationContext context = new ClassPathXmlApplicationContext(
				"com/hlx/extend/applicationContext.xml");	
		//抽象类不能实例化
		Peson per = (Peson) context.getBean("student");
		System.out.println(per.getPname());



d)效果:

  输出:fatter

 如果取消 0) test

	ApplicationContext context = new ClassPathXmlApplicationContext(
				"com/hlx/extend/applicationContext.xml");
		// 1 获得spring容器的person对象
		Student stu = (Student) context.getBean("student");
		//调用方法
		stu.say();

则 fatter继承关系!

说明:子类重写父类!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值