Spring Boot Practice Tutorial (i): hello,world!

Source: Internet
Author: User
Tags spring initializr

?? This article will start our Spring boot journey by completing a common HelloWorld example

Preparatory work

15 minutes

Development environment

Project creation

?? When the project is created, we can choose to create it with a custom configuration maven file, or with spring INITIALIZR tools, which are officially provided by INITIALIZR, a tool that simplifies the construction of the Spring Boot project. There are two ways to use spring INITIALIZR:

    • Official web page tool, address

    • How the development tools are integrated

?? The steps to operate in both ways are the same, except that the Web page tool takes a step further into the process of importing the resulting results into the development tools, and here we take the second: Development tools Integrated Spring INITIALIZR.

?? In idea, new Project > select Spring Initializ The following dialog box appears:

?? Fill in the corresponding group, Artifact, select type for MAVEN project, and fill in other relevant information (as shown) after clicking Next, in this step can select the corresponding function, select Spring boot will add corresponding dependencies for the project, etc. Here we only add a basic function of the web. Completing the project path further completes the creation of a Web project.

?? Look at the structure after the project is created:

spring-boot-1-helloworld    │  pom.xml        ├─src    │  ├─main    │  │  ├─java    │  │  │  └─com    │  │  │      └─practice    │  │  │          └─springboot    │  │  │              └─hello    │  │  │                  │  SpringBoot1HelloworldApplication.java    │  │  │                              │  │  └─resources    │  │      │  application.properties    │  │      ├─static    │  │      └─templates    │  └─test    │      └─java    │          └─com    │              └─practice    │                  └─springboot    │                      └─hello    │                              SpringBoot1HelloworldApplicationTests.java
Project Run

?? We can start the project by running the main method, we first create a controller to look at the effect after launch, and create the Hellocontroller in the Web package (which needs to be created manually):

package com.practice.springboot.hello.web;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestControllerpublicclass HelloController {    @RequestMapping("/")    publichi(){        return"Congratulations! It works!";    }}

?? Running the main method in Springboot1helloapplication.java, we'll see a spring flag and a description of the spring boot version in the console. After the boot is complete, it is accessed in the browser http://localhost:8080/ and will be seen. It took us a few minutes to create a Web project and run it. In this process we did not add any jar dependencies, did not configure Web. XML, did not configure the spring and SPRINGMVC environments, and even did not deploy the project only Tomcat, because all of this spring boot was done for us.

Project analysis

?? First, let's take a look at the Pom.xml file

<?xmlVersion= "1.0" encoding= "UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0</modelVersion>    <groupId>Com.practice.springboot</groupId>    <artifactId>Hello</artifactId>    <version>0.0.1-snapshot</version>    <packaging>Jar</packaging>    <name>Spring-boot-1-helloworld</name>    <description>Spring-boot-1-helloworld</description>    <parent>        <groupId>Org.springframework.boot</groupId>        <artifactId>Spring-boot-starter-parent</artifactId>        <version>1.5.8.RELEASE</version>        <relativePath/> <!--lookup parent from repository --    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>Org.springframework.boot</groupId>            <artifactId>Spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>Org.springframework.boot</groupId>            <artifactId>Spring-boot-starter-test</artifactId>            <scope>Test</scope>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>Org.springframework.boot</groupId>                <artifactId>Spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

?? You can see the following information in the Pom file:

    1. The entire project is inherited from Spring-boot-starter-parent
    2. Project has two dependency packages spring-boot-starter-web,spring-boot-starter-test

?? With the above two configurations, Spring boot provides us with an environment for the underlying Web project, including dependency packages and an embedded Tomcat environment. In the following article we will analyze the starter here.

Project Packaging

?? Run the MAVEN command under Project path MVN package, and after running it will get a jar file in the target directory of the project, which is the project bundle. Unlike the previous war packages, the project package format is a jar package.

?? You can run the jar file by Java-jar the file name. jar and then access it in the browser to http://localhost:8080/ see that the results are the same as in the previous development environment.

Summarize

?? Through the above steps, completed a preliminary basic Spring boot project creation and operation, follow-up I will be a practical way to analyze the spring boot of some of the common features and features, and according to my own understanding of how spring boot operating principle.

?? Reference:
?? Spring Boot Reference Guide

Welcome to my public number:

Spring Boot Practice Tutorial (i): hello,world!

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.