What is the IOC

IOC is short for ‘Inversion of Control,’ which translates as’ Inversion of Control. ‘

Let’s not delve into what this means in Spring, but take it literally. For example: Until you get married, your salary is completely at your disposal. Spend it however you want. After marriage, things change. Your money has to go to your daughter-in-law, and you have to ask for it when you want to spend it. At this point, your control over your salary changes from you to your wife. This is called “inversion of control,” where what you control is taken over by someone else, and you only get it when you need it.

Spring bucket address: The latest collection of Spring bucket information

Inversion of control in Spring is the process of creating objects that Spring manages instead of having to manually create new objects itself.

This is just like having a container in Spring where we put our beans, let the container create instances for us, and fetch them directly from the container when we need them. The container’s implementation philosophy is IOC.

Why use IOC

The biggest benefit of using IOC is that it reduces the coupling degree of the code and the maintenance cost of the program. Probably many people know this truth, but do not quite understand how it is lowered, don’t panic, let me explain to you.

Suppose we have a dish: Kung Pao chicken.

/ / pseudo code
public class KungPaoChicken {
    
    public static KungPaoChicken getKungPaoChicken(A variety of ingredients) {
        // The final result of processing various ingredients is a delicious Kung pao chicken.
        returnKungPaoChicken; }}Copy the code

The traditional way

If we do not use IOC now and want to eat Kung Pao chicken, we need to do as follows.

/ / pseudo code
public class Person(a){
    // Buy all kinds of ingredients
    // prepare all the ingredients and get a portion of KungPaoChicken at KungPaoChicken.KungPaoChicken KungPaoChicken = KungPaoChicken. GetKungPaoChicken (various ingredients); }Copy the code

Coupling diagram between codes:

It doesn’t seem too hard or too much trouble, right?

Don’t jump to conclusions. It’s only one person who wants Kung pao chicken. What if 10 people want kung pao chicken? Are there ten copies of the same code? All 10 are coupled to KungPaoChicken. For example, if the ingredients needed are changed, do these 10 people need to adjust the code? Doesn’t that seem like a nice way to implement it.

Use the IOC approach

Now, instead of making it ourselves, we’re going to tell the restaurant how to make it.

/ / pseudo code
public class Restaurant {
    
    public static KungPaoChicken getKungPaoChicken() {
        // Process the ingredients and return to kung pao chickenretur KungPaoChicken; }}Copy the code

Coupling diagram after transformation:

Through this process, these problems can be solved to a large extent.

1. KungPaoChicken is managed by the Restaurant and created by the Restaurant. 2. Now whether it’s 1 person or 10 people, we just need to get it from Restaurant. This changes the coupling, and the Person only needs to be coupled to the Restaurant. 3. When KungPaoChicken changes, it doesn’t need everyone to change, it just needs the Restaurant to change.

Spring’s IOC container acts as the Restaurant in the example above. We simply tell Spring which beans Spring needs to manage, and then get them from the IOC container in a specified way.

The IOC container provided by Spring

Spring provides an interface, BeanFactory. This interface is the top level of Spring’s implementation of the IOC container and is used internally by Spring, not specifically for consumers of the framework.

We typically use the ApplicationContext subinterface of the BeanFactory, which provides much more and much more power.

There are three commonly used implementation classes in the ApplicationContext interface: AnnotationConfigApplicationContext, FileSystemXmlApplicationContext, ClassPathXmlApplicationContext.

The creation of the container requires reading configuration files or configuration classes that tell Spring which beans Spring needs to manage.

Note: When reading configuration files, prefix file: is required for reading absolute paths, and classpath: is required for reading relative paths.

AnnotationConfigApplicationContext

Purpose: Used to read configuration information about a configuration class during full annotation development. Note: Annotate the current class as Spring’s Configuration class with the @Configuration annotation

The sample code

ApplicationContext context = newAnnotationConfigApplicationContext (custom configuration classes. The class);Copy the code

ClassPathXmlApplicationContext

The default load is in the classPath configuration file where the code was compiled in the classes folder. Note: when using the ClassPathXmlApplicationContext read relative path into the participation “the classpath:” can be omitted. When reading the absolute path, prefix file: with the input parameter.

The sample code

// Relative path
ApplicationContext context = new ClassPathXmlApplicationContext("Classpath: configuration file name.xml");
ApplicationContext context = new ClassPathXmlApplicationContext("Configuration file name.xml");

// Absolute path
ApplicationContext context = new ClassPathXmlApplicationContext("File: configuration file path under absolute path");Copy the code

FileSystemXmlApplicationContext

What it does: Loads the configuration file in the project path by default. Note: while reading the absolute path for FileSystemXmlApplicationContext into parameter prefix “file:” can be omitted, but read the relative path into the participation “the classpath:” is a must.

The sample code

// Relative path
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:beans.xml");

// Absolute path
ApplicationContext context = new FileSystemXmlApplicationContext("File: configuration file path under absolute path");
ApplicationContext context = new FileSystemXmlApplicationContext("Configuration file path under absolute path");
// Directly from the project path
ApplicationContext context = new FileSystemXmlApplicationContext("SRC \main\resources\ config file name");
Copy the code

Spring’s IOC implementation principles

Spring implements the IOC container through: factory + reflection.

To illustrate the implementation principle of SpirngIOC (based on XML configuration files)

In a fully annotated form, you simply change the step of reading the configuration file to reading the configuration class, and then get the Bean that needs to be created through the configuration class, and create it through reflection. The overall implementation idea is the same as using XML configuration files.