Create a SpringBoot project from scratch

This article introduces the creation of a SpringBoot project from scratch. Before creating it, let’s talk about the startup process of springboot and some related knowledge points. For details, see the article:

1. SpringBoot related knowledge points

1. SpringBoot startup process

  • First find the run() method from main, and create a new SpringApplication object before executing the run() method
  • Enter the run() method, create the application listener SpringApplicationRunListeners and start listening
  • Then load the SpringBoot configuration environment (ConfigurableEnvironment), and then add the configuration environment (Environment) to the listening object
  • Then load the application context (ConfigurableApplicationContext) as the return object of the run method
  • Finally, create a Spring container, refreshContext (context), and realize automatic configuration of the starter and instantiation of the bean, etc.

2. Spring Boot automatic assembly

Find all corresponding configuration classes in the META-INF/spring.factories file of the class path through the @EnableAutoConfiguration annotation, and then load these automatic configuration classes into the spring container.

3. SpringBoot core annotations

The @SpringBootApplication annotation is the core annotation of SpringBoot, which includes SpringBoot's package scanning principle, automatic assembly principle and many other principles, but it is actually a combined annotation, including @Configuration, @EnableAutoConfiguration, @ComponentScan.

  • @Configuration: The marked class is equal to Spring's XML configuration file (applicationContext.xml), which assembles all bean transactions and provides Spring's context environment
  • @EnableAutoConfiguration: SpringBoot automatically configures the Spring framework according to the dependencies declared by the application
  • @ComponentScan: Component scanning, automatic scanning and assembly of beans, scanning the files under the package path where ExammanagerApplication.class in the run method of SpringApplication is located

2. Create a SpringBoot project

After knowing the above knowledge points, how about we create a usable springboot project? Please see below:

1. Open the idea and click New Project

insert image description here

2. Use Spring Initializr to build Spring Boot project, custom group, name, JDK

Select Spring Initializr, select maven and jdk versions on the right, here I use the default name, you can modify it at will. Click Next after setting.
insert image description here

3. Select the SpringBoot version

Here I choose the 2.7.10 version. A higher Springboot version requires better jdk version support (you can modify the version number in the pom file to select another version after creation). It is recommended to use a stable version. Check Spring Web and click Create.
insert image description here

4. Created successfully

The directory structure after creation is as follows.
insert image description here

5. Modify the configuration file

Only the port number is modified here, and other configurations, such as whitelists and logs, can be added according to requirements during the actual project application process.
insert image description here

6. Create a new test class

In order to see the effect more intuitively, a new test class is created, the access path is set to /hello, and the string "Hello World!" is returned.
insert image description here

7. start

Click to start the run or debug of the idea to start the project. If the console prints Started DemoApplication in seconds, the startup is successful.
insert image description here

8. Access

Access http://localhost:8088/hello with the browser, and if "Hello World!" appears on the page, it means success.
insert image description here
Since then, the simplest SpringBoot project has been successfully created. Since the blogger himself uses the mac system, some content may be different. Please correct me if there are any mistakes.

Welcome to pay attention to the pop-up official account map
and share java-related knowledge from time to time

Guess you like

Origin blog.csdn.net/sulli_F/article/details/129941555