Use Maven to get detailed explanation of the first Spring Boot program, mavenspring

Source: Internet
Author: User

Use Maven to get detailed explanation of the first Spring Boot program, mavenspring

Preface

To learn a new technology, don't worry about its principles, procedures, how to organize code or something. Copy a demo directly from the official website or blog, and run it on your own. Then, you can easily understand the pile of explanations.

At present, there are a lot of articles on Spring Boot on the Internet, which are very helpful. I recently studied Spring Cloud in depth. when setting up the first Hello World Program, I feel that for new users, this article is not detailed, because there are many pitfalls, so here we will post the step of the first practice, in order to make progress with everyone.

1. What is Maven? What help can it bring?

When we use Eclipse to develop a project, we will certainly introduce jar packages that support specific functions. For example, we can see that this project needs to introduce jar packages that support mysql.

 

We can see that the jar packages that support mysql are stored in the local path, so that if you run it locally, it will be okay. But if we want to release this project to the server, there will be problems, because in this project. classpath file, which specifies a path of the mysql jar package on the local D Drive, as shown in.

Once published to the server, the project will still find the path under the D Drive Based on the. classpath configuration. In fact, this path and jar package cannot be found on the server.

You can also. in classpath, specify the relative path to solve this problem. In the following code, we can specify that the jar package in the "Project path/WebRoot/lib" Directory will be introduced in this project.

<Classpathentry kind = "lib" path = "WebRoot/lib/jar package name. jar"/>

In this way, the files in the entire project path will be uploaded to the server, so there will be no errors. However, this will still cause inconvenience to us. For example, if we have deployed five projects on this server, they will all use this mysql support package, so we have to upload this jar package five times and then expand it, if 20 identical jar packages are used in five projects, we have to copy them multiple times. If we want to upgrade one of the jar packages, we have to do a lot of repeated copy and paste operations.

The expected working mode should be that there should be a "warehouse" where all jar packages are placed at the same time. When developing a project, you can introduce necessary packages through the configuration file, instead of copying the package to this project. This is the practice of Maven.

In general, Maven is an Eclipse plug-in. Its core value is to streamline dependencies between projects. Specifically, it can use the pom. the xml configuration file is used to centrally manage the jar packages used in this project. After the Maven plug-in is introduced in the project, developers do not have to manually add the jar package, this can also avoid a series of problems.

2. Develop the HelloWorld program of Spring Boot through Maven

Step 1: Create a Maven project. This book uses MyEclipse as the development environment and has introduced the Maven plug-in, so we can directly create a Maven project through the "File"-> "New" menu, as shown in.

In, click "Next" and you will see the interface shown in. In this interface, you can set attributes such as Group Id.

The Group Id indicates the company name. Here it is set to "com. springBoot", while the Artifact Id indicates the project name. The default values are used for Version and Packag. After completing the settings, you can see the newly created Project MyFirstSpringBoot.

Step 2: rewrite pom. xml. After creating a Maven project, you can see the pom. xml file. In a Maven project, the basic information of the project and the jar package to be introduced are usually 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>

Lines 1st to 4th are automatically generated to specify the basic information of the project, which is consistent with the information we filled in when creating the Maven project.

From the dependencies attribute of lines 7th to 19th, we can specify the jar package used in this project. Here, in lines 8th and 13th, two types of jar packages are specified through two dependency. Lines 8th to 12 specify the set of jar files used to develop the Spring Boot project named spring-boot-starter-web, and lines 13th to 18, specify the junit package to be introduced for unit testing.

From the above code, we can see the general method of managing project dependent files through Maven. For example, in the following code snippet, the code is from 2nd to 4th lines, which indicates that org. springframework. A jar package named Spring-boot-starter-web that supports spring Boot is released by the company organization "boot" (the organization that publishes the Spring Boot jar package, the version number of the introduced package is 1.5.4.RELEASE.

In this way, in this project, we do not need to manually add jar packages locally. These packages are actually in the remote repository, and our project uses pom. xml configuration to specify the packages to be introduced.

Step 3: rewrite App. java.

When creating a Maven project, the package we specified is com. springboot. MyFirstSpringBoot. There will be an App. java in it. We will rewrite this file to 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 Maven is used for the first time, we emphasize again here that although we did not manually introduce jar in the project. the dependency package to be introduced is specified in xml, which depends on org. springframework. the spring-boot-starter-web provided by the boot organization. Therefore, in lines 2nd to 5th of the code, we can use the import Statement, use the spring-boot-starter-web (Spring Boot) class library.

In row 8th, we introduced the @ SpringBootApplication annotation to declare that this class is a Spring Boot application. In rows 10th to 13th, we use @ RequestMapping to specify the sayHello method used to process/HelloWorld requests. In the main function of row 14th, we started the Web Service through the Code in line 2.

So far, we have completed coding. Start App. java in the MyFirstSpringBoot project and enter http: // localhost: 8080/HelloWorld in the browser. The/HelloWorld request can be mapped to the @ RequestMapping of the sayHello method of rows 11th to 13, so Hello World will be output through the sayHello method! As shown in.

From this program, we can understand the differences between Spring Boot and traditional Spring programs.

First, in the previous Spring MVC Framework, we had. the xml definition uses Spring listeners. To use the @ Controller class, we have to add a lot of configuration, 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 requests in the browser to view the running effect, here we only need to start the App. java can achieve similar results, saving the steps for deploying to the Web server.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.