[SpringBoot] One article to understand the creation and use of SpringBoot

Xiaoyu came into contact with the framework excitedly, but after three days of using it, she forgot all about it. She quickly wrote a blog to summarize it to avoid making mistakes in the future…

Table of Contents

1. What is Spring Boot? Why learn Spring Boot?

2.Spring Boot Advantages

3.Spring Boot item creation

3.1Create using IDEA

3.2 Using springboot:

*Configuration file:application.properties

*Customized configuration information and acquisition

3.3SpringBoot hot deployment

3.3.1 Add spring-dev-tool dependency:

3.3.2 Turn on automatic compilation in the two settings of IDEA;

3.3.3 Turn on auto-make in settings:


1. What is Spring Boot? Why learn Spring Boot?

Spring was born to simplify the development of Java programs, and Spring Boot was born to simplify the development of Spring programs. So novices like Xiaoyu don’t be afraid. The framework is a tool that is convenient for programmers. It serves you and only simplifies some unnecessary processes for you…

2.Spring Boot Advantages

  • Quickly integrate frameworks. Spring Boot provides the function of starting to add dependencies, integrating various frameworks in seconds.
  • Built-in running container, no need to configure Web containers such as Tomcat to run and deploy programs directly.
  • Quickly deploy projects without the need for external containers to get them up and running.
  • You can completely abandon the cumbersome XML and use the annotation and configuration methods for development.
  • By maintaining more monitoring indicators, you can better understand the operation of the project.

3.Spring Boot project creation

3.1 Create using IDEA

Please refer to the next picture for downloading the plug-in:

Type in the name of the plug-in you want to download and click install. Then you will see an icon similar to my lombok angry pepper in installed. Continue below…

Coders who don’t have lombok, please download the plug-in “lombok”. After downloading, there are two plugs – one that looks like a plug, and one that looks like a red pepper (bundled download).

Congratulations, you have completed more than half of it. Next, we will start to familiarize ourselves with the layout inside…

First let’s delete a few useless files: four in total

Then select the package name, right-click and select Add….

Check maven and click OK

Then you will see the maven visual interface on the right side.

At this time, we see that pom.xml is becoming popular, and the displayed version is incorrect. When we created it, didn’t we choose the 2.x version? There is an extra release here, and we need to delete it manually…

Then select the folded maven tool on the right side of the screen and select the reload button

When this appears at the bottom of the screen, it means it has been configured….

At this point we need to pay attention to one thing, that is
Domestic source problem: Check maven in settings to see the configuration inside:

To configure the mirror in settings.xml, check the last two and uncheck the following options. The content of the mirror to be configured in settings.xml is written in my Gitee (in the code snippet). If it is not open source, you can chat with me privately. I Send you…

All configurations in IDEA, whether character set or file configuration, must be configured twice: if the two settings are exactly the same, they must be configured twice (once One is the current project and one is the new project)

Let’s take a look at what the file directories are and what they are used for:

3.2 Use springboot:

Classes can only be created under the com.example.demo package, or under a sub-package of this package. Only here can you create a Java Class. I created a TestController class, which will write the helloworld of my first springboot implementation:

@RestController
public class TestController {

    @RequestMapping("/sayhi")
    public String SayHi(){
        return "hello world!";
    }
}

There are several annotations that need explanation:

  • @RestController: It is the combined annotation of @ResponseBody + @Controller, which is equivalent to including five major categories of annotations (@Controller, @Service, @Repository, @Component, @Configuration)
    One of the @Controller and @ResponseBody indicating that the return is not an HTML page but static data.
  • RequestMapping means that this is a routing annotation. The URL filled in will match the URL in the request when the request is sent. If the URLs are the same, you can execute the startup method, otherwise 404 will be returned. (Note that the URL here must use ” ” and add /xxxx route name inside).

Now click the run button:

We can see in the log how long it took to successfully start, and the port number 8080 above, which means that the project has been started. At this time, we enter the URL in the browser (Edge and Chrome are recommended):
127.0.0.1:8080/sayhi or:localhost:8080/sayhi is enough. (The latter sayhi is my route name in @RequestMapping brackets).

Okay, is your first hello world a success? Post it in the comment area and let me praise you~~

*Configuration file:application.properties

The configuration files in springboot include application.properties and application.yml (or application.yaml)
The two have the same function and both belong to configuration files that can be recognized by springboot. We can choose one. If there are both, application.properties will be the main one.
That is to say, the .properties configuration file has the highest priority, but after loading the .properties file, the configuration information of the .yml file will also be loaded.

It is located under resources. There is nothing when you open it, but you can configure some information in it, such as port number and so on…

The format of key-value pairs is used to set configuration items——>key=value
In yml, it is composed of ——>key: value. Note that key and value are composed of English and spaces. The spaces cannot be omitted, so the yml format is not friendly to novices.
Here is an example of configuring the port number:

server.port=8081

Start the program again:


You can see that the port number in the log during startup is 8081. At this time, when typing in the URL, you need to change the port number to 8081, otherwise the connection will be rejected:

*Customized configuration information and acquisition

So the configuration information is provided by the system, can programmers customize it? Can it be obtained after customization? The answer is yes

server.port=8081

myapplication.path = zhangsan

We need to use another annotation in the startup class: @Value()
You need to write your custom key value in the brackets to tell this annotation which custom configuration information I want to get, but when writing, pay attention to the format requirements: you must add “${xxxx}” to get this Customize the configuration items, and then we define a variable to accept printing and verification:

Type the URL again: note that the port number is 8081!

3.3SpringBoot hot deployment

The hot deployment is enabled after writing the code. There is no need to start it manually. The automatic deployment will take effect. The hot deployment of the paid version of IDEA will take effect very quickly. The hot deployment of the community version will take effect slower. After typing in the URL, it may be 404 for a few seconds. See the page… The steps to enable hot deployment are as follows:

3.3.1 Add spring-dev-tool dependency:

Add the tag to the tag in pom.xml, or manually check Spring Boot DevTools when creating a springboot project to automatically add dependencies (see P4 of 3.1 ), this is also the benefit of the framework, there is no need to go to the maven repository to find the copy coordinates.

3.3.2 Turn on automatic compilation in the two settings of IDEA;

3.3.3 Turn on auto-make in settings:

Just restart the project.

Okay, that’s it for today’s introduction. There is also springboot related content in the sequel. Supervise each other with Xiaoyu and operate it carefully~~~~~~~

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138038 people are learning the system