Preface
When learning a new technology, don’t worry about its principles, processes, how to organize code, etc. Just copy a small demo from the official website or blog, run it yourself, and then read the bunch of explanations that make people scalp tingling, and you will be relatively easy to understand.
There are currently many introductory articles about Spring Boot on the Internet, all of which are very helpful. I have been studying Spring Cloud in depth recently. When I was building the first Hello World program, I feel that for novices, it is not an exaggeration to introduce the article in detail. Because there are many pitfalls, I will post the steps of the first practice here to make progress with everyone.
1. What is Maven? What help can it bring?
When we use Eclipse to develop projects, we will definitely introduce jar packages that support specific functions. For example, from the figure below, we can see that this project needs to introduce jar packages that support mysql.
From the above picture we can see that the jar package that supports mysql is placed in the local path, so if it runs locally, it will be fine. But if we want to publish this project to the server, there will be a problem, because in the .classpath file of this project, a certain path of the mysql jar package on the local D disk has been specified, as shown in the figure below.
Once published on the server, the project will still look for this path under the d disk according to the configuration of .classpath. In fact, it is impossible for such a path and jar package on the server.
We can also solve this problem by specifying relative paths in .classpath. In the following code, we can specify that this project will introduce the jar package in the "Project Path/WebRoot/lib" directory.
<classpathentry kind="lib" path="WebRoot/lib/jar package name.jar"/>
When doing this, when publishing to the server, all files in the entire project path will be uploaded, so there will be no errors. But this will still cause us inconvenience. For example, we deployed 5 projects on this server, and they all use the mysql support package, so we have to upload this jar package 5 times and expand it. If 20 same jar packages are used in the 5 projects, then we really have to copy it multiple times. If we want to upgrade one of the jar packages, we really have to do a lot of duplicate copy and paste actions.
The expected working mode should be that there should be a "repository" to place all jar packages at the same time. When developing a project, you can introduce necessary packages through configuration files instead of copying the packages into this project. This is what Maven does.
In simple terms, Maven is a plug-in with Eclipse. Its core value is to be able to straighten out the dependencies between projects. Specifically, it can uniformly manage the jar packages to be used in this project through the pom.xml configuration file. After introducing the Maven plug-in in the project, developers do not have to manually add the jar package, which can also avoid a series of problems caused by this.
2. Develop Spring Boot's HelloWorld program through Maven
The first step is to create a Maven project. This book uses MyEclipse as the development environment, and the Maven plug-in has been introduced, so we can directly create the Maven project through the "File"->"New" menu as shown in the figure below.
In the above picture, after clicking the "Next" button, you will see the interface shown in the figure below, where we can set properties such as Group Id.
Among them, Group Id represents the company name, which is set to "com.springBoot", while Artifact Id is the project name, and Version and Packag use default values. After completing the setup, you can see the newly created project MyFirstSpringBoot
The second step is to rewrite pom.xml. After we create the Maven project, we can see the pom.xml file. In Maven projects, the basic information of this project and the jar packages that need to be introduced are generally specified through pom.xml. The key code here is as follows.
<groupId>com.springboot</groupId> <artifactId>MyFirstSpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>MyFirstSpringBoot</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.5.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies>
Among them, the codes on lines 1 to 4 are automatically generated and are used to specify the basic information of this project, which is consistent with the information we filled in when we created the Maven project before.
From the dependencies attributes of lines 7 to 19, we can specify the jar package used in this project. Here, we use two dependencies to specify the introduction of two types of jar packages in lines 8 and 13 respectively. From lines 8 to 12, a collection of jars called spring-boot-starter-web that need to be introduced to develop Spring Boot projects is specified, and in lines 13 to 18, a junit package for unit testing is specified.
From the above code, we can see the general way to manage project dependency files through Maven. For example, in the code snippet below, it is through lines 2 to 4, indicating that it is necessary to introduce a set of jar packages called spring-boot-starter-web, which is published by the company organization org.springframework.boot (the organization that publishes Spring Boot jar packages) that support Spring Boot, and the version number of the imported package is 1.5.4.RELEASE, is specified through line 4.
In this way, in this project, we do not need to manually add jar packages locally. These packages are actually in the remote repository. Our project specifies that these packages need to be introduced through the pom.xml configuration.
The third step is to rewrite App.java.
When creating a Maven project, the package we specify is com.springboot.MyFirstSpringBoot, and there will be an App.java in it. We rewrite this file into the following style.
package com.springboot.MyFirstSpringBoot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class App { @RequestMapping("/HelloWorld") public String sayHello() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(App.class, args); } }Since it is the first time to use Maven, we will emphasize again here that although we did not manually introduce jars in the project, since the dependency package to be introduced is specified in the pom.xml, specifically, we need to rely on the spring-boot-starter-web provided by org.springframework.boot organization. Therefore, in lines 2 to 5 of the code, we can use the spring-boot-starter-web (that is, Spring Boot) class library through the import statement.
In line 8, we introduced the @SpringBootApplication annotation to declare that the class is a Spring Boot application. In lines 10 to 13, we specify the sayHello method used to handle /HelloWorld requests through @RequestMapping. In the main function on line 14, we start the web service through the code on line 15.
So far we have completed the code writing work. Start App.java in the MyFirstSpringBoot project and enter http://localhost:8080/HelloWorld in the browser. Since the /HelloWorld request can be corresponding to the @RequestMapping of the saysHello method on lines 11 to 13, the content of Hello World! will be output through the sayHello method, as shown in the figure below.
From this program, we can understand the difference between developing Spring Boot and traditional Spring programs.
First, in the previous Spring MVC framework, we had to define a Spring listener in web.xml, and in order to use the @Controller controller class, we had to add a lot of configurations, but in Spring Boot, we only need to add a @SpringBootApplication annotation.
Second, we often need to publish traditional Spring MVC projects to web servers such as Tomcat. After starting the web server, we can enter a request in the browser to see the running effect. Here we only need to start the App.java class to achieve similar effects, which can save the step of deploying to the web server.
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.