论坛首页 Java企业应用论坛

Spring的继承

浏览 2760 次
锁定老帖子 主题:Spring的继承
精华帖 (0) :: 良好帖 (0) :: 新手帖 (2) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-03-11  
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是属性的继承。大家可以定义属性,然后采用重定义覆写.
   发表时间:2011-03-23  
呵呵,没看懂啊
0 请登录后投票
   发表时间:2011-04-02  
Technoboy 写道
呵呵,没看懂啊

执行一下代码就明白了
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics