In Spring, inheritance is used to set up a bean for a support bean to share a common value, property, or configuration. A child bean or inherited bean can inherit the configuration, properties, and some properties of its parent bean. In addition, the child Bean allows overriding the inherited value. See the complete example below to show you how to configure Bean inheritance to work in Spring.
Package Com.yiibai.common; Public class Customer { privateint type; Private String action; Private String country; // ... }
Bean configuration file
<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-2.5.xsd "> class=" Com.yiibai.common.Customer "> <property name=" Country "value=" Malaysia "/> </bean> <bean id= "Customerbean" parent= "Basecustomermalaysia" > <property name= "action" value= "buy"/> <property name= "type" value= "1"/> </bean> </beans>
The above is the value of the country attribute contained in the "Basecustomermalaysia" Bean, and the "Customerbean" Bean inherits the value of its parent (' Basecustomermalaysia ').
Execute it
PackageCom.yiibai.common;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classApp { Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Customer Cust= (Customer) context.getbean ("Customerbean"); SYSTEM.OUT.PRINTLN (Cust); }}
Output results
Customer [Type=1, Action=buy, Country=malaysia]
The Customerbean Bean inherits the country attribute only from its parent ("Basecustomermalaysia"). Inheritance abstraction in the above example, ' Basecustomermalaysia ' can still be instantiated, for example,
Customer cust = (customer) Context.getbean ("Basecustomermalaysia");
If you want to have this bean as a base template and not allow others to instantiate it, you can add an "abstract" attribute to a <bean> element. For example
<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-2.5.xsd "> classabstract = "true" > <property name= "Country" value= "Malaysia"/> </bean> <bean id= " Customerbean "parent=" Basecustomermalaysia "> <property name=" action "value=" buy "/> < Property name= "Type" value= "1"/> </bean> </beans>
Now, the "Basecustomermalaysia" Bean is a purely template, because the bean can only inherit it, and if you try to instantiate it, you will encounter the following error message.
Customer cust = (customer) Context.getbean ("Basecustomermalaysia"); o Rg.springframework.beans.factory.BeanIsAbstractException: ' Basecustomermalaysia ': Abstract
Pure Inheritance TemplateIn fact, the parent bean is not required to define the properties of the class, many times you may only need a common property share. Here is an example of
<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-2.5.xsd "> abstract= "true" > <property name= "Country" value= "Malaysia"/> </bean> <bean ID = "Customerbean" parent= "Basecustomermalaysia" class= "Com.yiibai.common.Customer" > <property name= "Action" value= "buy"/> <property name= "type" value= "1"/> </bean> </beans> /c2>
In this case, the "Basecustomermalaysia" Bean is a purely template that only shares its "country" attribute. Overwrite it however, you can still specify a new value for the child bean to overwrite the inherited value. Let's take a look at this example
<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-2.5.xsd "> classabstract = "true" > <property name= "Country" value= "Malaysia"/> </bean> <bean id= " Customerbean "parent=" Basecustomermalaysia "> <property name=" Country "value=" Japan "/> < Property name= "Action" value= "buy"/> <property name= "type" value= "1"/> </bean> </ Beans>
The "Customerbean" Bean simply overrides the parent ("Basecustomermalaysia") country attribute, which is modified from ' Malaysia ' to ' Japan '.
Customer [Country=japan, Action=buy, type=1]
It is useful to summarize the spring bean configuration inheritance in order to prevent multiple beans from having duplicate values or configurations. Download code –HTTP://PAN.BAIDU.COM/S/1BLOGAQ
Spring Learning (ix)-----Spring Bean Configuration Inheritance