Java springboot project guide class, simple understanding of program entry

We always see such a startup class when we create a springboot project
insert image description here

First of all, we need to know that our program finally establishes a spring container and all your classes are handed over to this spring container for management.

Doing springboot program will also have this spring container

In order to make it easier for everyone to see, we will change the code of the startup class to this

package com.example.threshold;

import com.example.threshold.controller.BookController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class ThresholdApplication {
    
    

    public static void main(String[] args) {
    
    
        ConfigurableApplicationContext ctx = SpringApplication.run(ThresholdApplication.class, args);
        BookController bean = ctx.getBean(BookController.class);
        System.out.println("beanmmmmmm>"+bean);
    }

}

Here we use the ConfigurableApplicationContext interface as the type to take the value of Yanlai to start the class. Anyone who has learned spring knows that this interface is to receive a container. Then
we use the assigned container to obtain the bean object.
insert image description here
Here, this class declares an object in our In the bean management of the system, we will get the BookController object in the bean object and output it through the spring obtained by starting the class. The
running results are as follows.
insert image description here
Here you can see that the class object is output

Explain that he is a spring container, which contains all the bean objects we declare

The second thing worth noting is his annotations.
insert image description here
Let’s click in and have a look.
insert image description here
You can see that there is another hole in the bottom. We believe that more insightful partners will take a fancy to this SpringBootConfiguration at first sight. Then we click in and
insert image description here
when we see this It is already optimistic when you comment. This is a configuration class.
ComponentScan will scan beans
insert image description here
, but here is a popular science. Its scan is only related to the current directory and the directory under the current directory.
If you put the bean class outside its package,
insert image description here
then the startup class can't find it
insert image description here

Simply put, the startup class is also called the boot class. It is the entry point of the program and it scans all beans.

Guess you like

Origin blog.csdn.net/weixin_45966674/article/details/130164885