Spring bean配置继承
在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置。 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性。另外,子 Bean 允许覆盖继承的值。 请参见下面的完整的例子来告诉你如何配置 bean 继承在 Spring 中工作。 package com.yiibai.common; public class Customer { private int type; private String action; private String Country; //... } Bean配置文件 <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"> <bean id="BaseCustomerMalaysia" 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> 以上就是“BaseCustomerMalaysia” Bean中含有的 country 属性的值,而“CustomerBean” Bean 继承其父('BaseCustomerMalaysia')这个值。 执行它 package com.yiibai.common; import org.springframework.context.ApplicationContext; import org.springfra
package com.yiibai.common;
public class Customer {
private int type;
private String action;
private String Country;
//...
}
<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"> <bean id="BaseCustomerMalaysia" 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>
执行它
package com.yiibai.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App
{
public static void main( String[] args )
{
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
Customer cust = (Customer)context.getBean("CustomerBean");
System.out.println(cust);
}
}
输出结果
Customer [type=1, action=buy, Country=Malaysia]
继承抽象
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
<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"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="true"> <property name="country" value="Malaysia" /> </bean> <bean id="CustomerBean" parent="BaseCustomerMalaysia"> <property name="action" value="buy" /> <property name="type" value="1" /> </bean> </beans>
Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
org.springframework.beans.factory.BeanIsAbstractException: Error creating bean with name 'BaseCustomerMalaysia': Bean definition is abstract
纯继承模板
<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"> <bean id="BaseCustomerMalaysia" 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>
覆盖它
<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"> <bean id="BaseCustomerMalaysia" class="com.yiibai.common.Customer" abstract="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>
Customer [Country=Japan, action=buy, type=1]
交流与评论