Spring 中继承的实现

SomeBean.java

java 代码
  1. package com.gjx.spring;   
  2. //测试Spring的继承   
  3. public class SomeBean {   
  4.   
  5.     private String name;   
  6.     private int age;   
  7.     public int getAge() {   
  8.         return age;   
  9.     }   
  10.     public void setAge(int age) {   
  11.         this.age = age;   
  12.     }   
  13.     public String getName() {   
  14.         return name;   
  15.     }   
  16.     public void setName(String name) {   
  17.         this.name = name;   
  18.     }   
  19. }   

somebean-config.xml

xml 代码
xml 代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  10. <!-- 在配制文件中设置继承,这是一个完全抽象的继承 -->  
  11.     <bean id="inheritedSomeBean" abstract="true">  
  12.         <property name="name">  
  13.             <value>冬国</value>  
  14.         </property>  
  15.         <property name="age">  
  16.             <value>22</value>  
  17.         </property>    
  18.     </bean>  
  19.     <!-- 注意这里的parent="inheritedSomeBean"与前面的对应 -->  
  20.     <bean id="some" class="com.gjx.spring.SomeBean" parent="inheritedSomeBean">  
  21.         <property name="name">  
  22.             <value>完全抽象的继承</value>  
  23.         </property>  
  24.     </bean>  
  25.        
  26.        
  27. <!-- 下面是从一个Bean的实例来继承  在这里的beanInstance也是可以被实例化的-->  
  28.     <bean id="beanInstance" class="com.gjx.spring.SomeBean">  
  29.         <property name="name">  
  30.             <value>冬国</value>  
  31.         </property>  
  32.         <property name="age">  
  33.             <value>30</value>  
  34.         </property>    
  35.     </bean>  
  36.     <bean id="instanceSome" class="com.gjx.spring.SomeBean" parent="beanInstance">  
  37.         <property name="name">  
  38.             <value>实例来继承</value>  
  39.         </property>  
  40.     </bean>  
  41.        
  42.   
  43. </beans>  

 

测试类:

java 代码
  1. package Test.com.gjx.spring;   
  2.   
  3. import junit.framework.TestCase;   
  4.   
  5. import org.springframework.beans.factory.BeanFactory;   
  6. import org.springframework.beans.factory.xml.XmlBeanFactory;   
  7. import org.springframework.context.ApplicationContext;   
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  9. import org.springframework.core.io.ClassPathResource;   
  10. import org.springframework.core.io.Resource;   
  11.   
  12. import com.gjx.spring.HelloBean;   
  13. import com.gjx.spring.SomeBean;   
  14.   
  15. public class TestHelloBean extends TestCase {   
  16.     //测试继承   
  17.     public void testSomeBean(){   
  18.         ApplicationContext context=new ClassPathXmlApplicationContext("somebean-config.xml");   
  19.         SomeBean some=(SomeBean)context.getBean("some");   
  20.         SomeBean instanceSome=(SomeBean)context.getBean("instanceSome");   
  21.            
  22.         System.out.println("Abstract>>>  name="+some.getName()+"  age="+some.getAge());   
  23.         System.out.println("Instance>>>  name="+instanceSome.getName()+"  age="+instanceSome.getAge());   
  24.     }   
  25. }   

测试结果:

冬国
Abstract>>>  name=完全抽象的继承  age=22
Instance>>>  name=实例来继承  age=30

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值