Use of parent and abstract in Spring Configuration File

  • 2021-11-02 00:59:47
  • OfStack

Spring configuration files parent and abstract

In fact, in the project based on spring framework development, if there are multiple bean instances of one class, such as configuring multiple data sources, most of the configured attributes are one, and only a few are not one.

In this way, you can configure and inherit with object 1 in the configuration file.

For example


<bean id="testParent"  abstract="true"  class="com.bean.TestBean">
    <property name="param1" value=" Parent parameter 1"/>
    <property name="param2" value=" Parent parameter 2"/>
</bean>   
<bean id="testBeanChild1" parent="testParent"/>
<bean id="testBeanChild2" parent="testParent">
      <property name="param1" value=" Sub-parameter 1"/>
</bean>

A configuration where abstract = "true" means that this class will not generate an instance in the Spring container.

parent= "testBeanParent" means that the subclass inherits testBeanParent and will generate concrete instances. Configuration in the subclass Bean will override the corresponding attributes of the parent class.

spring uses parent attributes to reduce configuration

In the project based on spring framework development, if there are multiple bean are the strength of one class, such as configuring multiple data sources, most of the configured attributes are one sample, only a few are not one sample, often the definition of one on copy, and then modify the places where not one sample. In fact, the spring bean definition can also be inherited from object 1.

Examples are as follows:


 <bean id="testBeanParent"  abstract="true"  class="com.wanzheng90.bean.TestBean">
        <property name="param1" value=" Parent parameter 1"/>
        <property name="param2" value=" Parent parameter 2"/>
  </bean>   
  <bean id="testBeanChild1" parent="testBeanParent"/>
   <bean id="testBeanChild2" 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, similar to an abstract class. testBeanChild1 and testBeanChild2 inherit testBeanParent, and testBeanChild2 reconfigures param1 attribute, so it overrides testBeanParent

Configuration of param1 property properties.

The code is 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


Related articles: