Spring教程 - Spring Bean继承

2018-01-09 19:06 更新

Spring教程 - Spring Bean继承


Spring支持bean配置继承。我们可以定义一个bean,然后进一步配置它来创建新的bean。

通过使用bean继承,我们可以共享公共值,属性或配置。

子bean继承其父bean配置,属性和属性。

此外,子bean可以覆盖继承的值。

Java Bean

package com.www.w3cschool.cnmon;
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.w3cschool.cnmon.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的配置文件中包含国家财产的“美国"值。

在上面“generalCustomer"bean的配置文件中包含国家财产的“美国"值。...

这里是运行此应用程序的代码。

package com.www.w3cschool.cnmon;
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);
    }
}

上面的代码生成以下结果。

客户[type = 1,操作=备份,国家=美国]


Download Java2s_Spring_Bean_Inheritance.zip


抽象bean

在上面的例子中,我们仍然可以实例化“generalCustomer"bean如下。

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

要使此基本bean作为模板,并且不允许Java代码实例化它,在< 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.w3cschool.cnmon.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标记为抽象之后。它成为bean继承的纯模板。

如果我们尝试实例化它,我们将遇到以下错误消息。

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

豆模板

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

在上面的xml代码中, generalCustomer bean是一个模板托管其他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.w3cschool.cnmon.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"国家财产,从“美国"到“日本"。

上面的代码生成以下结果。


Download Java2s_Spring_Override.zip
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号