Analysis of dependency injection process in SpringBoot project

1. It can be coded and analyzed according to API design drawings

  The specific codes and notes are as follows

2.1 define an interface

package com.cy.pj.common.cache;


public interface Cache {
}

2.2 leave the implementation class to spring management

package com.cy.pj.common.cache;

import org.springframework.stereotype.Component;

@Component
public class SoftCache implements Cache {

}
package com.cy.pj.common.cache;

import org.springframework.stereotype.Component;

@Component
public class WeakCache implements Cache {
}

2.3 create another test class for testing  

package com.cy.pj.commom.cache;

import com.cy.pj.common.cache.Cache;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class CacheTest {

    //  @Qualifier("softCache") spring will find the object corresponding to this type, because there are two objects that need to be specified
    //If there is only one object, multiple objects corresponding to the directly injected attribute type will be searched based on the attribute name, otherwise the injection fails
    //Or name the bean @ Component("xxxx"). If not, the first letter is lowercase
    //@Qualifier must be used with the @ Autowired annotation
    //The underlying layer uses reflection technology for assignment
    @Autowired
    @Qualifier("softCache")
    private Cache cache;//DL (dependency lookup) -- di (dependency injection)

//    @Autowired
//    private WeakCache weakCache;

    @Test
    void eat(){
        System.out.println(cache);
    }

//    @Test
//    void eat2(){
//        System.out.println(weakCache);
//        //System.out.println(softCache);
//    }




}

Summary: it is listed here in the test class code annotation

@Autowired is defined by the spring framework and is used to describe properties or related methods (such as construction methods) in a class. When the spring framework runs the project, if it finds that the Bean object managed by it has properties or methods described with the @ Autowired annotation, it can assign values (DI) to the properties according to the specified rules. The basic rule is: first, check whether there is an object matching the attribute or method parameter type in the container. If there is and only one, inject it directly. Secondly, if multiple objects are detected, it will also find out whether there are objects with matching names according to the attribute or method parameter names described by @ Autowired. If there are, it will be injected directly, and if not, an exception will be thrown. Finally, if we have specific requirements, we must inject objects whose type is the specified type and whose name is the specified name. We can also use the @ Qualifier annotation to describe their properties or parameters (this annotation must be used in conjunction with the @ Autowired annotation). This part summarizes that what I see in thinking is better than my own language

The following code assigns values by itself. Use @ Autowired to describe the attributes or related methods (such as construction methods) in the class to assign values to set and construction methods

Code design CacheServiceTest ------CacheService   --------- Cache(interface)   ---------- implements SoftCache WeakCache

The construction method is as follows:

package com.cy.pj.common.service;

import com.cy.pj.common.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class CacheService {

    private Cache cache;

    @Autowired//The construction method can be described, and one construction method can be omitted, because the construction method needs to pass arguments            
    public void  CacheService(@Qualifier("softCache") Cache cache){ //Here is still to find out whether there are objects with matching names. Because there are multiple objects that need to be specified
        this.cache=cache;

    }

set method injection

package com.cy.pj.common.service;

import com.cy.pj.common.cache.Cache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class CacheService {

    private Cache cache;

//    @Autowired / / can describe the construction method
//    Public void cacheservice (@ qualifier ("softcache") cache) {/ / here is still to find out whether there are objects with matching names. Because there are multiple objects that need to be specified
//        this.cache=cache;
//
//    }

    @Autowired
    public void setCache(@Qualifier("softCache") Cache cache){
        this.cache=cache;
    }
//There is no need to write the get method here
    public Cache getCache(){
        return cache;
    }
}

Neither set nor constructor need to be called by ourselves. In fact, set and constructor are called by reflection

summary

1 in our actual project, we will not write all businesses into one object, and each object has its own responsibility

2. In an actual project, objects will complete one by collaboration. This collaboration requires dependencies between objects

3 when objects have dependencies (this phenomenon is coupling), we should reduce the coupling. We need coupling and abstraction rather than a specific class. At the same time, we need coupling and factory, because the factory helps us create objects. The factory is BeanFactory

4 when there is a coupling relationship between objects, it is best for the object manager to complete the object injection column, such as spring

In spring, it is recommended to use automatic dependency injection columns to assign values to object type attributes. For example, spring scans the corresponding methods of attribute scanning, and its underlying technology uses reflection

6The injection strategy for assigning values to object type attributes in spring is (directly assigning values to attributes, construction methods, and set methods)

7. The reason for handing over objects to Spring management is that there is a goal for the operation of objects in the program. First, solve business problems. Second, optimize business execution efficiently and low consumption. In order to achieve this goal, we need to fully analyze the business and analyze the business into API s   The Spring framework will not help us solve business problems, but it can give us some design and help in terms of object runtime performance. For example, Spring gives our objects many more scientific features, such as delayed loading scope, life cycle method and automatic dependency injection at runtime (reducing coupling and improving program maintainability)

8The spring framework helps us create Bean objects through reflection to obtain the bytecode of the Class. When the Class type is loaded into memory, it can be obtained. Step 2: obtain the Constructor type of the Constructor object in the Class, and create an instance of the corresponding Class based on the object of this type   The Constructor is the Class in the reflection package, and the bottom layer uses the newInstance method

Tags: Java Spring Spring Boot

Posted on Tue, 14 Sep 2021 20:55:33 -0400 by Webbyturtle