Springboot learning summary 01

Time:2021-6-12

The startup process of springboot

1) The loading class based on configuration loads the class, and the bottom layer loads the class from disk to cache through io
2) Find the annotation defined in spring to describe the bean, and create a map at the bottom. The name of the bean is used as the key, and the bytecode of the class is XXX. Class uses the configuration information obtained by reflection as value, and passes it into map < string, beandefinition > class
3) The map is passed into beanfactory, and the object is created by beanfactory. The map is used to save again. map<String,Object>
4) When using, we can get the object we need through the getBean () method. However, we should note that value is saved by using the object type, so we should remember to convert it to the type we need when using it.

The startup class of springboot

@SpringBootApplication
The class annotated by this annotation is the startup class of springboot.
@SpringBootApplication
public class Application{
public static void main(String[] args){
SpeingApplication.run(Application.class, args);
}

}

Some annotation usage in spring

package com.cy.pj.common.pool;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
//Build object pool type
@Component
@Lazy
@Scope ("prototype") // multiple instances, do not write, default to singleton mode
The name of public class objectpool {// bean is lowercase by default
 public ObjectPool(){
        System.out.println("ObjectPool()");
    }
    @Postconstruct // life cycle initialization method, which is executed after the construction method is executed
 public void init(){
        System.out.println("init()");
    }
    @Predestroy // life cycle destruction method, which is executed before the spring container is removed
 public void destory(){
        System.out.println("destory()");
    }
}`

@Component

The following comments are quoted in
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
This annotation is a general annotation describing spring management, that is to say, the class modified by this annotation will give the created object to spring to manage and implement.

@Lazy

Delayed loading
@Lazy (on) means turn on delay loading, and off means turn off delay loading
But in general, it is not suitable for delayed loading, so this comment is not needed, so off is generally not used
Delay loading, will not start the program in the call to create an instance of the class without parameters, earwax in the need to create an instance of the class and use.
We can observe this change in two ways.
1) In the nonparametric construction of the delayed loading class, a statement called “south” is used to observe when the nonparametric construction of the class is called, and an instance of the class is created
2) The JVM parameter code is: – XX: + traceclassloading

@Scope

This annotation is used to specify the actual scope of the class.
When the @ score annotation is not used, the default mode is singleton
Singleton: after the program starts, spring will create an instance of the class and place it in beanpool for easy calling. The instance is unique and will not be created for the second time. It will be put into the pool again after use. Depending on the situation, it can be used with @ lazy. When we create a single instance that is too large and will not use the blade at the beginning, we don’t want to load the instance at the beginning of the program to occupy memory, but then it will be used frequently. We don’t want to create it many times to waste time, we can use @ scope (singleton) and @ lazy to combine the two annotations.
Multiple cases :@Scope (“prototype”) itself has a certain feature of lazy loading. When the program is running, it will not load an instance of the class into beanpool. However, note that in the multi instance mode, a new instance will be created every time it is called (the address value of the instance will be different if it is called twice in succession), and this instance will not be put into pool, After use, it will be recycled by the garbage collection mechanism (while the single instance will disappear only when the pool is destroyed). If the instance is rarely used, you don’t want it to occupy memory all the time in the pool. Then we can choose the multi instance mode.

Recommended Today

The use of springboot Ajax

Ajax overview What is Ajax? data Ajax application scenarios? project Commodity system. Evaluation system. Map system. ….. Ajax can only send and retrieve the necessary data to the server, and use JavaScript to process the response from the server on the client side. data But Ajax technology also has disadvantages, the biggest disadvantage is that […]