Writing in the front

In view of the message that someone wants to learn SpringBoot related knowledge, I am going to write a related blog about SpringBoot series, the goal is to let students read this series of blog posts, can have a glimpse of SpringBoot. This preliminary set down a series of blog posts including SpringBoot introduction, introduction, configuration, log, web development, data access, combined with related docker, caching, message queue, retrieve, tasks, security, distributed and so on a series of blog posts, a lot of work, is a long process, every step I have detailed as far as possible, deserve to go up screenshot shows, I hope it’s really useful for those of you who are watching. I just want to share technical blog posts, but also want to say that if you think it is useful, please click on the following, give a like, also a relief to me, after all, I have to lose a lot of hair, hey hey hey

Serial article transport bar

Detailed SpringBoot tutorial introduction (1) detailed SpringBoot tutorial introduction (2) detailed SpringBoot tutorial configuration file (1) detailed SpringBoot tutorial configuration file (2) detailed SpringBoot tutorial log framework Detailed SpringBoot tutorial Web development (1) detailed SpringBoot tutorial Web development (2) detailed SpringBoot tutorial Web development (3) detailed SpringBoot tutorial data access detailed SpringBoot tutorial boot configuration principle Detailed SpringBoot tutorial for cache development

Project package

Following up on the last post, we created a simple SpringBoot application, ran it, packaged it, and experienced the benefits of SpringBoot. So, to start with, I’d like to talk about the packaging at the end of the last post.

I didn’t introduce any dependency on Tomcat at the beginning, and I didn’t adjust or configure the environment where the JAR package is. Why can I still execute the JAR package without any error?

This is a simple question. If we think about it, the application cannot run without any dependency support for nothing, then the JAR we are running must already have all the configuration dependencies needed to run. Yes, that is the answer. So how do we go to see, no hurry, here I will tell you the secret of snooping jar package. First we can use the compression tool to open the JAR package we put on the desktop, as shown below



We’re going to see these three directories, and if we go into boot-INF, the first directory, we’ll see that there are two directories, classes, where we put the compiled classes of the code we wrote, and liBS, where we put all the dependencies of our project, at the bottom, did you see that? Yeah, This is the Tomcat dependency package.



When the SpringBoot project was created, it helped us introduce Tomcat’s dependencies, so we could start running the program, as shown below, and as we’ll see later, the first couple of dependencies we introduced in poM have specific names for SpringBoot, “Starters,” We are introducing the Web application starter, so it will come with the basic configuration dependencies that we need for Web development.

pom.xml

Now let’s look at the eye-catching POM.xml file from the project and see what’s inside. We will find that there is a parent project in the project, click on the parent project, as follows (only part of the project can be captured, you can open it in Idea by yourself)



If you’re careful, you’ll notice that when we used Maven to create projects like SpringMVC, we had to import a version of the dependency, like the following

<! -- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> < artifactId > hibernate - core < / artifactId > < version > 5.4.12. The Final < / version > < / dependency >Copy the code

However, in our SpringBoot pom. XML, we do not need to import dependencies. This is because parent has all SpringBoot dependencies. If management dependencies are not in dependencles, then we naturally need to write versions.

Starter starter

As mentioned in the last blog post, all we need to do is import the Spring-boot-starter-Web dependencies, and SpringBoot will automatically import the basic dependencies needed for Web development, including Tomcat dependencies. Spring-boot-starter-web is what we call a SpringBoot launcher. There are many such initiators, we can go to the official documentationArticle transfer



Click on the POM on the right to view the dependencies that the launcher contains.

An initiator such as spring-boot-starter-web is formally called a Springboot scenario initiator, which helps us import dependencies needed for normal operation in relevant development scenarios (for example, spring-boot-starter-web is a dependency in a Web scenario). We can rely on different import scenarios, such as web development import spring-boot-starter-web, database JPA access import spring-boot-starter-data-jPA, Want to import Spring-boot-starter-AMQP and so on using advanced message queues.

Springboot extracts all the functional scenarios and makes them into a starter (starter). As long as all the dependencies of these starter related scenarios are imported into the project, it is convenient to pour into any scenario starter to use any function.

Main program class (main entry class)

Let’s now examine the relevant basic annotations in the main program class.

@SpringBootApplication

@springBootApplication is a Springboot application labeled on a class to indicate that this class is the entry class to Springboot, and Springboot should run in the main method of this class to start the Springboot application. We hold down the Ctrl key to click in, we mainly look at the annotations inside

@SpringBootApplication

In @SpringBootApplication, a key annotation is @SpringBootApplication. @SpringBootConfiguration is the configuration class of Springboot, and the annotation is on a class, This class is a Configuration for Springboot. Again, if you hold down Ctrl and click on it, you’ll see @Configuration in @SpringBootConfiguration, and this annotation is to annotate various Configuration classes, Configuration files. If you’re familiar with AOP, We know that the Configuration class is also a Component in the container, so in @Configuration we annotate it with @Component.

@EnableAutoConfiguration

Going back to @springbootapplication, I can see another configuration class annotation @enableautoconfiguration. This annotation says to EnableAutoConfiguration, so that we don’t have to do the configuration stuff that we did in Spring projects. Springboot will automatically configure it for us, @enableAutoConfiguration tells SoringBoot to enable auto-configuration so that the boot configuration will work, and what’s in this annotation, we’re going to talk about it a little bit, because then we’ll know, Here’s how SpringBoot automatically loads dependencies and places them there.



@ AutoConfigurationPackage automatic configuration package, Spring’s underlying annotations @ import, to the container import a component, the imported components by AutoConfigurationPackage. The Registrar. The class to register, The AutoConfigurationPackage is a way to scan the main configurationPackage (@SpringBootApplication annotated class) and all of its subpackages into the Spring container. If you’ve ever written a SpringMVC project or something like that, When we write some Controller or Service, we need to configure the bean to scan the Controller package name in the configuration file, which we do not need in SpringBoot

AutoConfigurationImportSelector

Under the @AutoConfigurationPackage, we can see that we imported a class through import. We go into the class and debug the class in the position shown below



Will find, in fact AutoConfigurationImportSelector is to give the container import automatic configuration class very much, also is to give the container import this scenario requires all the components, and configured these components, a automatic configuration class, removes our manual writing configuration into functional components, etc.

When Springboot starts, it obtains the values specified by EnableAutoConfiguration from the meta-INF /Spring.factories under the classpar. The diagram below.

Quickly create a SpringBoot project

If you want to create a Maven project and import Springboot dependencies, you can create a Maven project using IDEA to automatically create Springboot dependencies







By doing this, we have created a SPringboot project that has already imported the dependencies. Of course, we can still import the dependencies in the POM if there is anything else missing, so there is no difference. I personally like to create a Maven project directly and import the dependencies on the website. This allows me to simplify the project and know what packages the project uses. We can see all the dependencies for our project in the project sidebar, Lib.



In particular, three things are automatically created under resources in the project created using IDEA. I will explain them here.

  • Static: Stores all static resources. What js, CSS, etc. are under this
  • Templates: Holds all the template pages. The default Jar package for Springboot uses embedded Tomcat. JSP pages are not supported by default, so for templates you can use a template engine such as Freemaker, Thymeleaf, etc
  • Application.properties, which is the SpringBoot configuration file. We know that SpringBoot has a lot of default Settings, so we can change the default Settings in this folder, such as ports and so on.

The next article

Here we have a brief introduction to A simple HelloWord, and at this point we have also said a word of HelloWord to the vast technical world of SpringBoot, so in the next post I will talk about SpringBoot configuration