How spring boot works

Time:2020-10-28

Overview of spring boot

Spring boot is a scaffold:
           Other external frameworks can be integrated quickly and easily
    Automatic configuration function is provided
    It can be used out of the box

Automatic configuration principle of springboot:
How spring boot works

The annotation of startup class (@ springbootapplication) has the annotation of “enable autoconfiguration”, which implements the function of automatic configuration
How spring boot works
Enable autoconfiguration with @ import annotation
Introduction of autoconfigu rationImportSelector.class
This class, which contains the configuration of springboot integration external framework

Automatic scanning:
The annotation (@ springbootapplication) of the startup class has @ componentscan
Automatically scan and load qualified beans into the container, and the package where the Default scan startup class is located starts scanning

Common notes of spring boot:

@Springbootapplication startup class annotation
    @ Component,@Controller ,@Service : give class to 
    Implementation class of beanfactory factory interface in spring framework
    object
    @Springboottest: the test class is handed over to spring management.
    @Autowired: the description tells the spring framework to inject a value for this property

Introduction to spring

Create a defaultcache class and give it to spring management.

@Component
public class DefaultCache {}

//Step two
@SpringBootTest
public class DefaultCacheTests {
 @Autowired
 private DefaultCache defaultCache;

 @Test
 public void testCache() {
 System.out.println(defaultCache);
 }
}

Program execution process analysis:

1. The program will first find the startup class (@ springbootapplication)
2. There are two annotations on the startup class:@ [email protected]
@Enable autoconfiguration will load some configuration required by program running, @ componentscan will scan the whole package structure from the package where the startup class is located, and scan the classes with specific annotations into the spring container,
3. After scanning, the defaultcachetests object exists in the spring container
4. The annotated class of springboottest indicates that it is a test class. When the program goes to @ Autowired, it will go to the defaultcache object or its subclass object in the spring container

Spring object scope + lazy load + life cycle method

There are five scopes in spring beansingleton,prototype,request,session,globalsession

Case study:

package com.cy.pj.common.cache;
@Component
@Scope("singleton")
@Lazy
public class DefaultCache {
 public DefaultCache() {
 System.out.println("cache()");
 }

 @PostConstruct
 public void init() {
 System.out.println("init()");
 }
 @PreDestroy
 public void destory() {
 System.out.println("destory");
 }
}

1. @ component will be scanned into the spring container
2. @ scope (“Singleton”) specifies that the storage domain is singleton
Singleton stores only one single instance
3. @ lazy tells the spring framework that this class supports delayed loading. In the program
The defaultcache is scanned to the spring container with @ lazy. At this time, the bean factory object will not create its instance immediately. When will it be used will it load its instance
4. The @ postconstruct annotation is used to describe initialization methods in bean object lifecycle methods
5. The @ predestroy annotation is used to describe destruction methods in bean object lifecycle methods
When the program is loaded, first go back to load the static method = > construction method = > initialization method = > Destroy method
6. If the scope prototype is not a singleton (a new instance will be created every time it is obtained), and it will not be submitted to spring for management. Spring can initialize it. Because spring does not care about it, it has to be destroyed manually

Spring training

package com.cy.pj.common.cache;
public interface Cache {}

//Interface subclass 1
package com.cy.pj.common.cache;
import org.springframework.stereotype.Component;
@Component
public class WeakCache implements Cache{
}
//Interface subclass 2
package com.cy.pj.common.cache;
@Component
public class DefaultCache implements Cache{
}

//Test class
@SpringBootTest
public class CacheTests {
 @Autowired
 @Qualifier("defaultCache")
 private Cache cache;
 @Test
 public void testCache() {
 System.out.println(cache);
 }
}
  1. By scanning, subclass 1 + subclass 2 are scanned into the spring container
  2. @Autowired is the interface of the parent class when it is injected into the container. However, if there are two subclasses in the spring container, the injection cannot be completed at this time
    How spring boot works
    At this point, you need to add @ qualifier (“defaultcache”) to specify an object for injection, and the specified object is subclass 2. At this time, the retrieved object is the object of subclass 2

Summary

1. What is spring boot? (scaffold rather than framework) can easily integrate external framework

2. Core advantages of spring boot? (start dependency, auto configuration – out of the box, health check – system monitoring)
In the process of integration, we only need to add relative dependency, and do not need to write tedious configuration, so it can be used directly

3. What is the bean initialization process when the spring boot project is started? (find, load, read and store configuration, create and store instance objects)
Find: the annotation specified on the class is loaded into the spring container

4. What is the dependency injection process of bean objects when the spring boot project is started? (tag, find, inject)