Spring的继承

parent-init.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-autowire="byName">

<bean id="initBean" class="org.frame.auth.init.InitBean" >
<property name="name">
<value>parent</value>
</property>
</bean>
</beans>

spring-init.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans default-autowire="byName">

<bean id="initBean" class="org.frame.auth.init.InitBean" >
<property name="name">
<value>sub</value>
</property>
</bean>
</beans>

TestParent.xml

package org.frame.auth.init;


import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class TestParent {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//文档果然犀利啊.
Resource resource = new ClassPathResource("parent-init.xml");
BeanFactory xmf = new XmlBeanFactory(resource);
Resource subResource = new ClassPathResource("spring-init.xml");
BeanFactory subXmf = new XmlBeanFactory(subResource,xmf);
InitBean initBean = (InitBean) subXmf.getBean("initBean");
System.out.println(initBean.getName());//得到的是父.

ApplicationContext ctx = new ClassPathXmlApplicationContext("parent-init.xml");

ApplicationContext subctx = new ClassPathXmlApplicationContext(new String[]{"spring-init.xml"},ctx);
InitBean initBeanb = (InitBean) subctx.getBean("initBean");
System.out.println(initBeanb.getName());//得到的是子.
}

}


这里展示了Spring 的BeanFactory的加载,有趣的是XmlBeanFactory加载是父容器类覆盖子容器类,而ApplicationContext加载是是子容器类覆盖父容器类,这个大家要注意的。
下面展示Spring Bean的parent属性。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" " http://www.springframework.org/dtd/spring-beans.dtd ">
<beans default-autowire="byName">

<bean id="parent" class="org.frame.base.parent.Message" abstract="true" >
<property name="name">
<value>ycl</value>
</property>
<property name="message">
<value>yes</value>
</property>
</bean>

<bean id="person" class="org.frame.base.parent.Person" parent="parent">
<property name="name">
<value>天才</value>
</property>
</bean>


</beans>

Person

package org.frame.base.parent;

public class Person {

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

public String toString(){
return name+":"+message;
}

}

Message

package org.frame.base.parent;

public abstract class Message {

private String name;
private String message;
public void setName(String name) {
this.name = name;
}
public void setMessage(String message) {
this.message = message;
}
public String toString(){
return name+":"+message;
}

}


测试

package org.frame.base.parent;

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

public class TestParent {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("parent.xml");
Person pp =(Person)ctx.getBean("person");
System.out.println(pp);
}

}


这里的Message和Person完全没有关系,但是我在Spring Bean中指定了其父Bean,可见Spring的这个parent是属性的继承。大家可以定义属性,然后采用重定义覆写.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值