天天看點

【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.

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

【 Here is the UML】:

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

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

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

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:

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

    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.

繼續閱讀