Use of parent and abstract in spring configuration file

Time:2022-6-19

Spring configuration file parent and abstract

In fact, in a project developed based on the spring framework, if multiple beans are instances of a class, for example, when configuring multiple data sources, most of the configured properties are the same, but only a few are different.

In this way, inheritance can be configured in the configuration file as in the object.

for example

<bean  abstract="true" >
    <property name= "Param1" value= "parent parameter 1" / >
    <property name= "param2" value= "parent parameter 2" / >
</bean>   
<bean parent="testParent"/>
<bean parent="testParent">
      <property name= "Param1" value= "sub parameter 1" / >
</bean>

The configuration of abstract= “true” indicates that this class will not generate instances in the spring container.

Parent= “testbeanparent” means that the subclass inherits the testbeanparent and generates a specific instance. Configuring in the subclass bean will overwrite the properties corresponding to the parent class.

Spring uses the parent attribute to reduce configuration

In a project developed based on the spring framework, if multiple beans are the strength of one class, for example, when configuring multiple data sources, most of the configured properties are the same, but only a few are different. It is often the definition of the previous copy, and then modify the different places. In fact, spring bean definitions can also be inherited like objects.

Examples are as follows:

<bean  abstract="true" >
        <property name= "Param1" value= "parent parameter 1" / >
        <property name= "param2" value= "parent parameter 2" / >
  </bean>   
  <bean parent="testBeanParent"/>
   <bean parent="testBeanParent">
          <property name= "Param1" value= "sub parameter 1" / >
    </bean>

Testbeanparent is the parent bean, where abstract= “true” means that testbeanparen will not be created, which is similar to an abstract class. Testbeanchild1 and testbeanchild2 inherit testbeanparent, and testbeanchild2 reconfigures the Param1 attribute, so testbeanparent will be overwritten

Configuration of Param1 attribute.

The codes are as follows:

TestBean


public class TestBean {    
    private String param1;
    private String param2;
    public String getParam1() {
        return param1;
    }
    public void setParam1(String param1) {
        this.param1 = param1;
    }
    public String getParam2() {
        return param2;
    }
    public void setParam2(String param2) {
        this.param2 = param2;
    }
    
}

App:


public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
        TestBean testBeanChild1 = (TestBean) context.getBean("testBeanChild1");
        System.out.println( testBeanChild1.getParam1());
        System.out.println( testBeanChild1.getParam2());
        TestBean testBeanChild2 = (TestBean) context.getBean("testBeanChild2");
        System.out.println( testBeanChild2.getParam1());
        System.out.println( testBeanChild2.getParam2());
    }
}

App main function output:

Parent parameter 1

Parent parameter 2

Sub parameter 1

Parent parameter 2

The above is my personal experience. I hope I can give you a reference, and I hope you can support developeppaer.