在 Spring 中使用 parent bean 定义.doc_第1页
在 Spring 中使用 parent bean 定义.doc_第2页
在 Spring 中使用 parent bean 定义.doc_第3页
在 Spring 中使用 parent bean 定义.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

淄博怡源网络科技有限公司如何去映射壹个 parent bean 作为 Spring 的 Bean 定义的壹部分。 我们壹起来看看以下三个类,看它们如何使用 Spring 来映射。view sourceprint?01public abstract class SuperBean 02private int id;03private String name;0405public int getId() 06return id;070809public void setId(int id) 10this.id = id;111213public String getName() 14return name;151617public void setName(String name) 18 = name;19 202122public class SubBean extends SuperBean 23private String newProperty;2425public String getNewProperty() 26return newProperty;272829public void setNewProperty(String newProperty) 30this.newProperty = newProperty;31 323334public class DifferentBean 35private int id;36private String name;37private String myProperty;3839public int getId() 40return id;414243public void setId(int id) 44this.id = id;454647public String getName() 48return name;495051public void setName(String name) 52 = name;535455public String getMyProperty() 56return myProperty;575859public void setMyProperty(String myProperty) 60this.myProperty = myProperty;6162我们开始在 Spring 中来映射它们:view sourceprint?010206070910111213151617181921222324并且现在我们写壹个简单的应用来展示使用这些映射到底会发生什么情况:view sourceprint?01import org.springframework.context.ApplicationContext;02import org.springframework.context.support.ClassPathXmlApplicationContext;0304public class TestClass 05public static void main(String args) 06ApplicationContext context = new ClassPathXmlApplicationContext(new String applicationContext.xml); 07SubBean subBean = (SubBean)context.getBean(subBean);08System.out.printf(ID: %d, Name: %s, Property: %sn,09subBean.getId(), subBean.getName(), subBean.getNewProperty(); 10DifferentBean differentBean = (DifferentBean)context.getBean(differentBean);11System.out.printf(ID: %d, Name: %s, Property: %sn,12differentBean.getId(), differentBean.getName(), differentBean.getMyProperty();1314如果我们运行这个类,会获取如下结果:view sourceprint?1ID: 2, Name: superBean, Property: subBean2ID: 3, Name: superBean, Property: differentBean我们从这个例子中了解到,如果你的 bean 类没有继承别的类,可以使用 parent 属性来映射 parent bean 的定义。壹个必要条件是子类必须与父类保持兼容,也就是说,子类必须具有父类中已经定义过的所有属性。事实上, Spring 官方文档

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论