We always see such a startup class when we create a springboot project
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.
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.
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.
Let’s click in and have a look.
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
when we see this It is already optimistic when you comment. This is a configuration class.
ComponentScan will scan beans
, 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,
then the startup class can't find it
Simply put, the startup class is also called the boot class. It is the entry point of the program and it scans all beans.