Getting started with the spring boot framework and quick start

Six previous articles have been written in succession, mainly introducing the spring and spring MVC frameworks. In the process of learning, my friends probably found that there are many places where we need to manually configure these two frameworks. However, my friends in Java EE development must have heard the saying "Convention is greater than configuration", that is, systems and class libraries, The framework should assume reasonable default values instead of requiring unnecessary configuration. However, if spring or spring MVC is used, there are still many such things that need to be configured, which not only increases the workload, but also is prone to problems in cross platform deployment. OK, because of these existing problems, spring boot came into being. Using spring boot allows us to quickly create a spring based project, and we only need a few configurations to make the spring project run. Spring boot mainly has the following core functions:

1. Spring project running independently

Spring boot can be run in the form of jar package. To run a spring boot project, we only need to use Java - jar XX The jar class runs. Very convenient.

2. Embedded servlet container

Spring boot can embed tomcat, so we don't need to deploy the project in the form of war package.

3. Provide starter to simplify Maven configuration

Using spring or spring MVC, we need to add a large number of dependencies, many of which are fixed. Here, spring boot can help us simplify Maven configuration through starter.

4. Automatically configure spring

5. Application monitoring of quasi production

6. No code generation and XML configuration

OK, for more detailed advantages and disadvantages of springboot, partners can also search by themselves. I won't list them here. Let's take a look at the code.

Project creation

For the first time, let's take a look at how to create a spring boot project. Here, take IntelliJ idea as an example. Other ide tool partners search the creation method by themselves:

First create a project, select spring initializr and then next, as shown in the following figure:

Fill in the project information, as shown in the figure below:

Fill in the technology used in the project. For the above spring boot version, it is recommended to select the latest stable version. Check the web below, as shown in the figure below:

Finally, fill in the project name and click Finish:

OK, the system will download the required dependencies when it is created for the first time, which takes a little longer. It will be created every time in the future.

OK, after the project is successfully created, let's see how to run this thing. First, we can see that after the project is created successfully, there will be an entry class of artifactid + application naming rule in the root directory of the project, as shown in the following figure:

This is the test19springboot2application class, which is the entry class of our whole project. This class has an @ springbootapplication annotation, which is the core annotation of the whole spring boot. Its purpose is to enable the automatic configuration of spring boot. OK, then I add another @ restcontroller annotation to this class to make it a controller, and then provide an address translation method inside, as follows:

Then click the project start button to run, which is the button in IntelliJ:

After successful startup, we can access it directly in the browser, as follows:

OK, so far, a simple spring boot project has been created and successfully accessed from the browser, but why does it finally run? I'm sure my friends have many questions. Let's analyze them.

Entry class and @ springbootapplication annotation

As mentioned above, when we create a new project system, it will help us create an entry class named artifactid + application. In this class, there is a main method, which is the entry method of a standard Java application. The @ springbootapplication here is a composite annotation. We can see its source code:

We can see that it combines @ springbootconfiguration, @ enableautoconfiguration and @ componentscan. If we don't use @ springbootapplication in the development process, we can combine these three annotations. Among the three annotations, @ springbootconfiguration is actually the @ configuration annotation mentioned in our previous blogs, indicating that this class is a configuration class, @ enableautoconfiguration means to let spring boot automatically configure the current project according to the jar package dependency in the class path. I won't repeat the role of the last @ componentscan, The only thing to note is that if we use the @ springbootapplication annotation, the system will scan the entity classes in the peer packages and child packages of the entry class. Therefore, we recommend that the entry class be located under the package name of the groupid + arctifactid combination.

Turn off specific autoconfiguration

In the above section, we saw that the @ componentscan annotation has a filter. If we only want @ springbootapplication to scan specific classes instead of all classes, we can turn off automatic configuration as follows:

Custom banner

Modify banner

When we start the spring boot project, a startup pattern will be output on the console by default, as follows:

Of course, this pattern can be modified by yourself if you need it. The modification method is very simple:

1. Create a new banner under Src / main / resources Txt document

2. Adoption http://patorjk.com/software/taag The website generates the required characters and copies the characters to the txt document created in step 1. For example, here I call Hello sang! Generate characters as follows:

GitHub address of the project

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>