【Basic computer】-----Bean Definition Inheritance



                      Bean Definition Inheritance


      What is that mean?inheritance,the bean definition can contain a lot of configuration information, including constructor arguments, property values, and container-specific information such as initialization method, static factory method name, and so on.

    A child bean definition inherits configuration data from a parent definition. The child definition can override some values, or add others, as needed.
   
 

Here is the UML】:
 


Code of bean  of the first time: So,here you can see  the repeation is bad taste: 

                   
 

We want to make it better, So, how to deal this problem?Here is the content of bean.xml file:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
        
        <bean id="abstractStudent" class="com.tgb.Student" abstract="true">
            <property name="className" value="classFive"></property>
        </bean>
        
        <bean id="zhangsan" parent="abstractStudent">
        </bean>
         <bean id="lisi" parent="abstractStudent">
        </bean>
         <bean id="wangwu" parent="abstractStudent">
        </bean>
</beans>  



Following is the content of the Student.java file:

 package com.tgb;
public class Student {
    private String className;
    public String getClassName() {
        return className;
    }
    public void setClassName(String className) {
        this.className = className;
    }
    @Override
    public String toString() {
        return "Student [className=" + className + "]";
    }
    
}


Here is the Client.java file:
  
package com.tgb;


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




public class Client {
	public static void main(String[] args){
		ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
		
		Student Daniel=(Student)ac.getBean("Daniel");
		System.out.println(Daniel);
		
		Student zhangsan=(Student)ac.getBean("zhangsan");
		System.out.println(zhangsan);
		
		Student lisi=(Student)ac.getBean("lisi");
		System.out.println(lisi);
	}
}




   Once you are done with creating source and bean configuration files, let us run the application. If everything is fine with your application, this will print the following message:

                 
 


    The parent bean cannot be instantiated on its own because it is incomplete, and it is also explicitly marked as abstract. When a definition is abstract like this, it is usable only as a pure template bean definition that serves as a parent definition for child definitions.



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值