Spring configuration bean-defined inheritance

Source: Internet
Author: User
Tags abstract definition constructor inheritance
3.6. Inheritance of bean definitions

The bean definition contains a large amount of configuration information, including container-related information (such as initialization methods, static factory method names, and so on), as well as constructor parameters and property values. A child bean definition is a bean definition that inherits configuration data from a parent bean definition. It can overwrite some of the values of the parent bean, or add some of the values it needs. Using the form defined by the parent/child bean can save a lot of input work. In fact, this is a form of template.

When using beanfactory programmatically, the child bean definition is represented by the Childbeandefinition class. Most users never need to use them in this way, but instead configure the bean definitions in a manner similar to those in Xmlbeanfactory. When using XML-based configuration metadata, assigning a value to the ' parent ' attribute means the declaration defined by the child bean.

<bean id= "Inheritedtestbean" abstract= "true"
    class= "Org.springframework.beans.TestBean" >
  < Property name= ' name ' value= ' parent '/>
  <property name= ' age ' value= ' 1 '/>
</bean>

<bean Id= "Inheritswithdifferentclass"
      class= "Org.springframework.beans.DerivedTestBean"
      parent= " Inheritedtestbean " init-method=" Initialize ">
    
  <property name=" name "value=" override "/>
  < !--The Age property value of 1 would be inherited from  parent--

</bean>

If the child bean definition does not specify a class attribute, it will use the class attribute defined by the parent bean and, of course, it can overwrite it. In the latter case, the child Bean's Class property value must be compatible with the half bean, meaning it must accept the parent Bean's property value.

A child bean definition can inherit the constructor parameter value, the property value, and the method that overrides the parent bean from the parent bean, and optionally add a new value. If Init-method,destroy-method and/or static factory-method are specified, they override the corresponding settings of the parent bean.

The remaining settings will always be obtained from the child Bean definition: Dependency, automatic assembly mode, dependency checking, singleton, scope, and deferred initialization.

Note In the example above, we use the abstract property to explicitly mark the parent bean definition as abstract. The following is an example of a parent bean definition that does not specify a class attribute, where the parent bean must be explicitly marked with an abstract:

<bean id= "Inheritedtestbeanwithoutclass" abstract= "true" >
    <property name= "name" value= "parent"/>
    <property name= "Age" value= "1"/>
</bean>

<bean id= "Inheritswithclass" class= " Org.springframework.beans.DerivedTestBean "
    parent=" Inheritedtestbeanwithoutclass "init-method=" Initialize " >
  <property name= "name" value= "override"/>
  <!--age would inherit the value of 1 from the parent Bean D Efinition-->
</bean>

Because such a parent bean is incomplete and is also explicitly marked abstract, it cannot get its own instance. An abstract bean defines a template that can be defined as a child bean. Attempting to use such a parent bean alone, such as referencing it as a ref attribute of another bean, or invoking the Getbean () method directly with the ID of the parent bean as a parameter, will result in an error. Similarly, the Preinstantiatesingletons () method inside the container completely ignores the abstract bean definition. Note

By default, ApplicationContext (not beanfactory) instantiates all singleton beans. So it is important to note that if you only want to use a (parent) bean definition as a template and it specifies the class attribute, you have to set the ' abstract ' property to ' true ', or the application context will (try) to instantiate the abstract bean.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.