SpringBoot study notes-01 A preliminary study of SpringBoot project

SpringBoot is an extremely fast development framework based on Spring, it only needs a small amount of configuration. First use:

0. Create a new SpringBoot project

We don't need to add dependencies automatically at the first learning. We choose to add dependencies manually.

[Note]: 1. If sometimes the official website link is too delayed, you can also choose to download the project skeleton from Alibaba Cloud:

[Note] 2: You can also directly create an empty Maven project and copy the missing things into it.

[Note] 3: Sometimes the pom.xml file will be invalid after the SpringBoot skeleton is built for the first time. At this time, we need to add the pom to our Maven project.

1. Configure file filtering in the pom.xml file

        <!--配置资源过滤-->
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <!--记住一定要配置SpringBoot主配置文件的包含-->
                    <include>**/*.properties</include>
                    <!--要使用自定义的banner需要包含txt-->
                    <include>**/*.txt</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>

2.SpringBoot-main

1. The return value of the run() method (when the run is executed, the startup scene will be selected according to the project type)

After the project is built, a class will be generated:

package com.zzt;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

It is the starter of SpringBoot and the configuration class (@SpringBootApplication annotation) of SpringBoot, which is used to start SpringBoot. In fact, SpringApplication.run(DemoApplication.class, args) returns a container, which is the Spring IoC container;

Let's do an experiment. Create a new class under the Service package and add the @Service annotation; use the container getBean in the mian method:

package com.zzt.service;

import org.springframework.stereotype.Service;

@Service
public class StudentService {
}
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(DemoApplication.class, args);
        StudentService bean = ctx.getBean(StudentService.class);
        System.out.println(bean);
    }

As you can see, the StudentService is automatically strengthened and put into the container, which verifies that we just said that the run() method returns a Spring IoC container.

2.main method pre-configuration

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.setBannerMode(Banner.Mode.CONSOLE); //在控制台打印输出启动logo  默认开启
        ApplicationContext ctx = app.run();
        StudentService bean = ctx.getBean(StudentService.class);
        System.out.println(bean);
    }

Using the SpringApplication object, we can do a lot of pre-configuration. For example, to start the logo, we just need to create a new banner.txt under the resource path resources, and enter the logo we need:

Of course, you can configure it in the configuration file without using code:

3. Use of main method builder

    public static void main(String[] args) {
        ApplicationContext ctx = new SpringApplicationBuilder().sources(DemoApplication.class).
                bannerMode(Banner.Mode.CONSOLE)
                .run();
        StudentService bean = ctx.getBean(StudentService.class);
        System.out.println(bean);
    }

[Note]: Now we should understand that the start of the SpringBoot project is essentially the creation of the Spring IoC container.

4. CommandLineRunner interface

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {
        ApplicationContext ctx = new SpringApplicationBuilder().sources(DemoApplication.class).
                bannerMode(Banner.Mode.CONSOLE)
                .run();
        StudentService bean = ctx.getBean(StudentService.class);
        System.out.println(bean);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Spring容器构建完成......");
    }
}

The CommandLineRunner interface defines the run method, which will call back after the Spring IoC container is completed (simultaneously instantiate the object and put it into the container).

[Note]: 1. We'd better configure it separately in the configuration class or in the configuration file to avoid conflicts (although the current test seems to be based on the configuration file when there is a conflict).

          2. The configuration class containing the main method is also a bean, so it can be injected.

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    @Autowired
    private StudentService studentService;

    public static void main(String[] args) {
        ApplicationContext ctx = new SpringApplicationBuilder().sources(DemoApplication.class).
                bannerMode(Banner.Mode.CONSOLE)
                .run();
        StudentService bean = ctx.getBean(StudentService.class);
        System.out.println(bean);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Spring容器构建完成......" + studentService);
    }
}

3. Project structure

    <!--继承父项目-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

In the inherited parent project, many jar package versions are defined, so when we introduce dependencies, instead of manually adding <version>, he will add dependencies according to the version defined by the parent project.

4. Launcher

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

Use the launcher to import, he will help us automatically import all the dependencies required by the corresponding scene, here is the web environment dependency. SpringBoot turns all functional scenarios into starters.

5. Brief analysis of SpringBoot automatic configuration principle

All automatic configuration of SpringBoot scans and loads the configuration file at startup: spring.factories, which contains all the automatic configuration classes; but not all of them are loaded. The configuration classes are annotated with @ConditionalXXX to indicate that only in It will load only when the conditions are met.

@ConditionalOnClass means that it will only take effect if you own this class.

 

 

 

Guess you like

Origin blog.csdn.net/qq_39304630/article/details/113064097