Spring strategy learning notes (1.13) -- Inherit bean Configuration

Source: Internet
Author: User

I. knowledge points

When you configure beans in the Spring IoC container, you may have more than one bean that shares some common configurations, such as attributes and attributes in the <bean> element. You often need to repeat these configurations for multiple beans.

Spring allows you to extract common bean configurations to form a parent bean. The bean inherited from the parent bean is called a child bean. Child beans inherit bean configurations from the parent bean, Including Bean attributes and attributes in the <bean> element to avoid repeated configuration. Child beans can also overwrite the inherited configurations when necessary.

The parent bean can be used as a configuration template or an instance of the bean. If you want the parent bean to be retrieved only as a template, you must set abstract to true and require spring not to instantiate the bean.

Note that not all attributes defined in the parent <bean> element will be inherited. For more information, see the spring document on bean inheritance.

Ii. Sample Code:

Bean configuration file:

<bean id="sequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >   <property name="initial" value="100000" />   <property name="suffix" value="A" />   <property name="prefixGenerator" ref="datePrefixGenerator" /></bean><bean id="sequenceGenerator1" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >   <property name="initial" value="100000" />   <property name="suffix" value="A" />   <property name="prefixGenerator" ref="datePrefixGenerator" /></bean><bean id="datePrefixGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.DatePrefixGenerator">   <property name="pattern" value="yyyyMMdd" /></bean> 

To avoid repeated attributes, you can use these attributes to declare a base sequence generator. Then, the two sequence generators can inherit from the base sequence generator, so that they automatically own the attribute sets. If the class attributes of the Child bean and the parent bean are the same, you do not need to specify them.

<bean id="baseSequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >    <property name="initial" value="100000" />    <property name="suffix" value="A" />    <property name="prefixGenerator" ref="datePrefixGenerator" /></bean><bean id="sequenceGenerator" parent="baseSequenceGenerator" /><bean id="sequenceGenerator1" parent="baseSequenceGenerator" />

The inherited attributes can be overwritten by sub-Beans. For example, you can add subsequence generators with different initial values.

<bean id="baseSequenceGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >   <property name="initial" value="100000" />   <property name="suffix" value="A" />   <property name="prefixGenerator" ref="datePrefixGenerator" /></bean><bean id="sequenceGenerator2" parent="baseSequenceGenerator" >   <property name="initial" value="200000"></property></bean>

Now, the base sequence generator bean can be used by bean instances. If you want to use the bean as a template only and set the abstract attribute to true, spring will not instantiate the bean; otherwise, an exception will be thrown.

<bean id="baseSequenceGenerator" abstract="true" class="com.jackie.codeproject.springrecipesnote.springioc.SequenceGenerator" >   <property name="initial" value="100000" />   <property name="suffix" value="A" />   <property name="prefixGenerator" ref="datePrefixGenerator" /></bean>

You tooYou can ignore the parent Bean class so that the child bean can specify its own class, especially when the parent bean and the Child bean are not in the same class hierarchy but share the same name attribute. In this case, the abstract attribute of the parent Bean must be set to true because the parent bean cannot be instantiated.For example, add another reversegenerator class with the initial attribute.

public class ReverseGenerator {    private int initial;    public void setInitial(int initial) {        this.initial = initial;    }}

Currently, sequencegenerator and reversegenerator do not extend the same base class, that is, they are not in the same class hierarchy, but have the same name attribute: initial. To extract the common initial attributes, a parent bean -------- basegenerator without defining the class attributes is required.

<bean id="baseGenerator" abstract="true">   <property name="initial" value="100000" /></bean><bean id="baseSequenceGenerator" abstract="true" parent="baseGenerator" >   <property name="suffix" value="A" />   <property name="prefixGenerator" ref="datePrefixGenerator" /></bean><bean id="reverseGenerator" parent="baseGenerator" class="com.jackie.codeproject.springrecipesnote.springioc.ReverseGenerator" /><bean id="sequenceGenerator" parent="baseSequenceGenerator" /><bean id="sequenceGenerator1" parent="baseSequenceGenerator" /><bean id="sequenceGenerator2" parent="baseSequenceGenerator" />

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.