Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.
As mentioned last time, we can set up the Spring Boot project and use the initialization website provided by Spring, which may not be convenient. Today, we will talk about how to use IDEA to initialize our Spring Boot project and analyze the demo.
The IDEA that can initialize Spring Boot has a version requirement. The minimum version is 14.1.
The following is the operation steps, all pictures, please do alert.
1. Select file-new-project-Spring Initializr
2. You can modify the package name, packaging method, Java version and other information.
3, Choose Web, the rest is not yet understood.
4. Configure the project path
After finish, you should see that the new project has the pom.xml file and an empty application.properties file in the main entry. We’ll talk about it one by one. Let’s get the project up and running.
Before running, let’s add a class to test the effect. Add an Example file as shown below.
5 Starting the Project
The first way is to use the Maven command to package and then execute the JAR package.
Run the java-jar command in the Terminal to access the target Jar package. Of course, you can also execute this Jar package in DOS.
The second way is to execute it directly using the plug-in command. We can also type MVN Spring Boot :run directly into Terminal (you can see the referenced plug-in in pom.xml).
Third, right-click Run.
Well, each of these approaches is possible, and you can see Hello World by visiting localhost:8080!
OK, let’s analyze a wave of the parts that make up the demo.
Pom. The XML file
We’ve said before that one of the advantages of Spring Boot is that dependency management is much easier, and look at what it offers.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> The < version > 2.1.3. RELEASE < / version > < relativePath / > <! -- lookup parent from repository --> </parent>Copy the code
The poM file provides the concept of a parent dependency. This dependency is the identity of the Spring Boot project. Spring-boot-starter-parent is a special starter that provides Maven default dependencies. We also don’t need to add version information for different dependencies.
Of course we can override a dependent version, but this is not recommended, as it may not be compatible with other JAR packages. If we maintain a set of dependencies in our own company, we can either do without the parent dependencies or use only a portion of them, with the help of the keyword import. In practice, however, the default should be more than the default.
Pom also defines many “starter”. Each starter is like a package, which contains a module dependency. For example, the spring-boot-starter-Web startup dependency internally depends on other dependencies. In this way, our POM files will be much simpler and easier.
The POM file also contains plugins. There is only one Maven plugin in the POM file, but some plugins are already in the parent dependency.
Java, which is the entry point to the program. We could have just executed this class, but we would have stopped there because there is no mapped path information. We could have put the method in Example in this class and added the corresponding annotation.
conclusion
Finally, there are several comments in the demo, one by one.
@SpringBootApplication does three things
1. Enable the automatic configuration mechanism of Spring Boot, which is equivalent to @enableAutoConfiguration.
2, scan package and its sub-package components, the role is the same as @ComponentScan.
3. Allow registering additional beans or importing additional Configuration classes in context, equivalent to @Configuration.
So, @SpringBootApplication is one against three, and it’s very important, and finally, there’s an annotation @RestController, which is a combination of @Controller and @responseBody.
OK, to this our Spring Boot entry demo even if explained to complete. Do you feel there are too many annotations? In fact, there are not many new annotations created by Spring Boot. Many of them are taken from Spring MVC.