One of Spring's Cores: What does Spring's configuration file do?

The first two articles explained the concepts of IOC and DI and why we need them. This article will continue with the definition of Spring IOC container, xml configuration file, which will clarify two questions:
1. The role of Spring XML configuration files. (Inheritance with IOC thought)
2. If the dependencies between beans and beans are defined. (Only the simplest way to use this is explained in more detail, which will be covered in a later article.)

When your program integrates some tools, such as databases, Redis, Mongodb, etc., online tutorials are added to the Spring configuration file for two purposes:

1. Place these tool classes in the configuration file. The Spring framework reads the configuration file, instantiates the objects of these classes, stores them in the Spring container (in memory), and can be retrieved and used from the container at any time.

2. We specify the dependencies between classes in the configuration file, rather than writing them in the code.


Let's take a look at Spring's configuration file.

<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mongo="http://www.springframework.org/schema/data/mongo"
           xsi:schemaLocation=
                   "http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd">
     </beans>

spring configuration files are in schema format, as above. Beans tag is the root node of the xml configuration file; xmlns are namespaces for these attributes, and other tags are written under the beans tag. In order to be recognized, it is essential to import the corresponding namespace in order to be recognized.

Below is the Java code to configure dependencies (this code is best looked at in detail, very representative!!!). :

/**
 * Car Class is a calling class, which has a member variable wheel and depends on the Wheel interface.
 */
public class Car {
    private Wheel wheel;
    public void truckRun(){
        wheel.trun();
    }
}

/**********Below is the Wheel interface and its two implementation classes ***********************************************************/
public interface Wheel {
    void trun();
}

/**
 * Implementing Class of Precursor Wheel
 */
public class PrecursorWheel implements Wheel{
    @Override
    public void trun() {
        System.out.println("precursor trun!");
    }
}

/**
 * Implementing class of rear wheel
 */
public class RearWheel implements Wheel{
    @Override
    public void trun() {
        System.out.println("rear trun!");
    }
}

We can see that if the Car class wants to successfully execute the run() method, it must call a method of Wheel's implementation class. But we did not write code in the Car class to illustrate which Wheel member variable of the Car class points to which Wheel implementation class, the control of which point, we put it in a centralized configuration file - Spring configuration xml file.

That's what I said above.


We specify the dependencies between classes in the Spring configuration file, rather than writing them in code.

Within the Beans tag, we configure our instances through the Bean tag (so we often call Spring managed objects Bean objects) and assign them to the Spring container to manage:

<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mongo="http://www.springframework.org/schema/data/mongo"
           xsi:schemaLocation=
                   "http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
              http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd">

        <bean id="precursorWheel" class="com.PrecursorWheel"></bean>
        <bean id="rearWheel" class="com.RearWheel"></bean>
        <bean id="car" class="com.Car">
            <property name="wheel" ref="precursorWheel"></property>
        </bean>

     </beans>

With the above configuration, three instance objects named precursor Wheel, rearWheel, and car will be stored in the Spring container (default is singleton).

The wheel field of the car instance is assigned to the precursorWheel object through the property tag. In terms of business performance, car objects are front-drive vehicles. Obviously, we moved the dependencies of Car class and PrecursorWheel class to the configuration file. Maybe we can't see the advantage of defining dependencies in the configuration file over that in the Car class, but we should associate:

In an application, there are many dependencies between classes. For example, C and D, A and D, F and D dependencies, if we all write in the code, once the requirements change, we need to change class D to class B, then we will at least modify the information of class A, C, F, and the amount of code changes is very large.
If we put the dependencies in the configuration file, we only need to change the centralized configuration file. It will be very simple and convenient to change, and the dependencies between classes and classes are very clear in the configuration file for easy modification.

In summary, the role of Spring configuration files:
Provide a customized way to manage instances of classes to Spring's Bean container. The customized content includes: class information + class dependencies.

Tags: Spring xml Redis MongoDB

Posted on Thu, 08 Aug 2019 19:19:11 -0700 by sader