zoukankan      html  css  js  c++  java
  • Spring中bean配置的继承

    In Spring, the inheritance is supported in bean configuration for a bean to share common values, properties or configurations.

    A child bean or inherited bean can inherit its parent bean configurations, properties and some attributes. In additional, the child beans are allow to override the inherited value.

    例子如下:

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

      

    public class App 
    {
        public static void main( String[] args )
        {
        	ApplicationContext context = 
    			new ClassPathXmlApplicationContext("SpringBeans.xml");
     
        	Customer cust = (Customer)context.getBean("CustomerBean");
        	System.out.println(cust);
     
        }
    }
    

      运行结果为:Customer [type=1, action=buy, Country=Malaysia]

    In above example, the ‘BaseCustomerMalaysia’ is still able to instantiate, for example,

    Customer cust = (Customer)context.getBean("BaseCustomerMalaysia");
    

      类似于java的抽象类,我们可以有:(注意abstract="true")

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

    Pure Inheritance Template

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

      



  • 相关阅读:
    C#设计模式之策略模式
    c#设计模式之单例模式
    关于分布式事务的实现梳理
    sql事务的使用及其技巧整理
    关于web系统整体优化提速总结
    .net导出excle无需任何插件,直接通过一个tablehtml实现
    ajax+ashx:实现文件的批量导出
    angularjs学习第九天笔记(指令作用域【隔离作用域】研究)
    angularjs学习第八天笔记(指令作用域研究)
    angularjs小练习(分别通过ng-repeat和ng-option动态生成select下拉框)
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2835130.html
Copyright © 2011-2022 走看看