Spring教程 - Spring Bean繼承

2018-01-09 19:06 更新

Spring教程 - Spring Bean繼承


Spring支持bean配置繼承。我們可以定義一個(gè)bean,然后進(jìn)一步配置它來(lái)創(chuàng)建新的bean。

通過(guò)使用bean繼承,我們可以共享公共值,屬性或配置。

子bean繼承其父bean配置,屬性和屬性。

此外,子bean可以覆蓋繼承的值。

Java Bean

package com.www.o2fo.common;
public class Customer {
    private int type;
    private String action;
    private String Country;
    public int getType() {
      return type;
    }//from   w ww.ja  v a  2  s.co  m
    public void setType(int type) {
      this.type = type;
    }
    public String getAction() {
      return action;
    }
    public void setAction(String action) {
      this.action = action;
    }
    public String getCountry() {
      return Country;
    }
    public void setCountry(String country) {
      Country = country;
    }
  @Override
  public String toString() {
    return "Customer [type=" + type + ", action=" + action + ", Country="
        + Country + "]";
  }
}


Bean配置文件

此外,子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="generalCustomer" class="com.www.o2fo.common.Customer">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer">
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

在上面“generalCustomer"bean的配置文件中包含國(guó)家財(cái)產(chǎn)的“美國(guó)"值。

在上面“generalCustomer"bean的配置文件中包含國(guó)家財(cái)產(chǎn)的“美國(guó)"值。...

這里是運(yùn)行此應(yīng)用程序的代碼。

package com.www.o2fo.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("SpringBeans.xml");
      Customer cust = (Customer)context.getBean("specialCustomer");
      System.out.println(cust);
    }
}

上面的代碼生成以下結(jié)果。

客戶[type = 1,操作=備份,國(guó)家=美國(guó)]


Download Java2s_Spring_Bean_Inheritance.zip


抽象bean

在上面的例子中,我們?nèi)匀豢梢詫?shí)例化“generalCustomer"bean如下。

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

要使此基本bean作為模板,并且不允許Java代碼實(shí)例化它,在< bean>中添加“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="generalCustomer" class="com.www.o2fo.common.Customer" abstract="true">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer">
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

在bean定義中將“generalCustomer"bean標(biāo)記為抽象之后。它成為bean繼承的純模板。

如果我們嘗試實(shí)例化它,我們將遇到以下錯(cuò)誤消息。

Customer cust = (Customer)context.getBean("generalCustomer");
org.springframework.beans.factory.BeanIsAbstractException: 
  Error creating bean with name "generalCustomer": 
  Bean definition is abstract

豆模板

父bean不需要類屬性,它只需要共享的共享屬性。

這里的是一個(gè)例子

<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="generalCustomer" abstract="true">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer" 
      class="com.www.o2fo.common.Customer">
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

在上面的xml代碼中, generalCustomer bean是一個(gè)模板托管其他bean的公共屬性以繼承。

覆蓋

我們可以通過(guò)在子bean中指定新值來(lái)覆蓋繼承的值。

<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="generalCustomer" class="com.www.o2fo.common.Customer" abstract="true">
    <property name="country" value="USA" />
  </bean>
  <bean id="specialCustomer" parent="generalCustomer">
      <property name="country" value="Japan" />
    <property name="action" value="backup" />
    <property name="type" value="1" />
  </bean>
</beans>

“specialCustomer"bean覆蓋父“generalCustomer"國(guó)家財(cái)產(chǎn),從“美國(guó)"到“日本"。

上面的代碼生成以下結(jié)果。


Download Java2s_Spring_Override.zip
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)