springboot2.x Reference Guide (Chinese version) get started with the second part

Source: Internet
Author: User
Tags bind manual require versions xmlns tomcat tomcat server jcenter

Reprint Please specify source: http://blog.csdn.net/hitliuxiaodong/article/details/53259797
I have limited level, translation errors please advise
If you're using Springboot for the first time or just touching spring, this is part of your plan. Here we will answer: what is. Why. What to do. Three basic questions. With the installation of Springboot, you will have a basic impression of springboot. We'll start building our first Springboot project, and we'll discuss the core guidelines.
 
Introduction of 8.SpringBoot
 
Springboot makes it easy to build standalone, published spring projects that can be run directly. We've integrated the spring library and other third-party libraries so you can start building projects without a lot of confusion. Most springboot projects require a small spring configuration to suffice.
You can use Springboot to build a Java project that runs through Java-jar, or you can build a relatively traditional war project. We also provide a command-line tool to run the spring script
Our primary goal is to provide a fundamentally fast and concise spring development experience that provides many non-essential features that are common in large projects (e.g., consolidated servers, security, etc.)

No need to generate code and XML configuration

9. System Requirements
By default, Springboot2.0.0.build-snapshot requires Java7 and Springframework5.0.0build-snapshot or later. You can also use JAVA6, but some additional configuration is required. See section 80.11. The build tool requires Maven (3.2+) or Gradle2 (2.9+). GRADLE3 does not provide support.

Although you can use JAVA6 or JAVA7, we recommend that you use the JAVA8 as much as possible.

9.1 servlet Container
The following containers are springboot internally integrated

Name servletversion Java version
Tomcat8 3.1 java7+
Tomcat7 3.0 java6+
Jetty9.3 3.1 java8+
Jetty9.2 3.1 java7+
Jetty8 3.1 java6+
Undertow 1.3 3.1 java7+

You can also deploy Springboot projects in any container that supports servlet 3.0+

10. Installing Springboot
Springboot can use traditional Java development tools or command-line tools. But anyway you need to use java1.6 or later. You can use the following command to check the Java version
$java-version
If you are a novice Java developer, or if you just want to experience springboot, you can try using the SPRINGBOOTCLI tool or read the installation guide.

Although Springboot can run well on java1.6, use the latest version of Java if possible

10.SpringBoot Installation Guide

You can use Springboot as you would with a traditional Java library, which is to put Sping-boot-*.jar in your classpath. Springboot does not require additional plug-in integration, so you can use any IDE or text editor. There is no difference between a springboot project and a normal Java project, so you can run debug Springboot projects just as you would run a normal Java project.

Although you can only copy the jar package to the build path, we strongly recommend that you use the build tool (such as Maven or Gradle)

10.1.1 maven Configuration
Springboot can work properly with ApacheMaven3.2 and above. If you do not have Maven installed, you can refer to the tutorial

On most operating systems, MAVEN can be installed through the package management tool. If you are an OSX system, you can use the Brew install MAVEN Ubuntu system to use sudo apt-get install maven

Springboot relies on the use of Org.springframework.boot groupId. A typical springboot project Pom needs to inherit spring-boot-starter-parent depending on one or more "starters". Springboot also provides Maven plugin to create executable jar packages.
The following is a typical pom.xml document

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "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.exam

    Ple</groupid> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <!--Inherit defaults from Spring Boot--<parent> &LT;GROUPID&GT;ORG.SPRINGFRAMEWORK.BOOT&L T;/groupid> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.build- Snapshot</version> </parent> <!--Add Typical dependencies for a Web application--<de
            Pendencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-web</artifaCtid> </dependency> </dependencies> <!--package as a executable jar--and <bu ild> <plugins> <plugin> <groupid>org.springframework.boot</gro
        Upid> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!--Add Spring repositories-to-<!--(you don't need this if you a Re using a. RELEASE version)-<repositories> <repository> <id>spring-snapshots</id&
            Gt <url>http://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled><
            /snapshots> </repository> <repository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </repository> </repositories>
            <pluginRepositories> <pluginRepository> <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository>
        <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>

The best way to use Springboot is to inherit spring-boot-starter-parent, but sometimes it's not appropriate. Sometimes you need to use inheritance from another parent pom, or you don't want to use the default settings. Take a look at the following tutorials

10.1.2 Gradle Configuration

Springboot compatible with Gradle2 (2.9+). Recommended gradle2.14.1,gradle3 is not supported. If you do not have Gradle installed, please refer to the Gradle official installation tutorial www.gradle.org

The dependence of springboot needs to be org.springframework.boot groupId. Your project may need to declare one or more "starters" dependencies. Springboot also provides a plug-in Gradle plugin for generating executable jar packages

Gradle Wrapper
Gradlewrapper provides a very concise way to build a project. It contains some scripts and some libraries for starting the build thread. Please see https://docs.gradle.org/2.14.1/userguide/gradle_wrapper.html for details.

The following is a typical Build.gradle script

Buildscript {
    repositories {
        jcenter ()
        maven {URL "http://repo.spring.io/snapshot"}
        Maven {URL "http ://repo.spring.io/milestone "}
    }
    dependencies {
        classpath (" Org.springframework.boot: Spring-boot-gradle-plugin:2.0.0.build-snapshot ")
    }
}

Apply plugin: ' java '
apply plugin: ' Org.springframework.boot '

jar {
    baseName = ' MyProject '
    version =  ' 0.0.1-snapshot '
}

repositories {
    jcenter ()
    maven {URL "http://repo.spring.io/snapshot"}
    maven {URL "/HTTP/ Repo.spring.io/milestone "}
}

dependencies {
    compile (" Org.springframework.boot: Spring-boot-starter-web ")
    testcompile (" Org.springframework.boot:spring-boot-starter-test ")
}

10.2 Installing the SPRINGBOOTCLI tool
SPRINGBOOTCLI is a command-line tool that lets you quickly experience the prototype of spring, which allows you to run Groovy scripts, meaning you can use Java-like syntax

You don't need to use SPRINGBOOTCLI when you work, but it defines a new way to quickly build a spring project

10.2.1 Installation Manual

You can download sping-boot-cli from the following two branches Spring-boot-cli-2.0.0.build-snapshot-bin.zip Spring-boot-cli-2.0.0.build-snapshot-bin.tar.gz

Once the download is complete, you can follow the steps in the INSTALL.txt in the package to install it.

10.2.2 Installation sdkman!
sdkman! (The software Development Kit Manager) can be used to manage several different SDKs, including Groovy and springbootcli. You can download from Sdkman.io and then execute the following command to install

$ SDK Install Springboot
$ spring--version
Spring Boot v2.0.0.build-snapshot

If you want to enhance the functionality of the CLI and want to use your compiled version faster, you can use the following method:

$ SDK Install Springboot dev/path/to/spring-boot/spring-boot-cli/target/spring-boot-cli-2.0.0.build-snapshot-bin/ spring-2.0.0.build-snapshot/
$ SDK default Springboot dev
$ spring--version
Spring CLI V2.0.0.build-snapshot

This will install an instance called Dev, which points to the location you compiled, so that every time you recompile springboot,spring it will be up to date.
You can use the following command to view

$ SDK ls springboot

================================================================================
Available springboot Versions
============================================================================== = =
> + Dev
* 2.0.0.build-snapshot

================================================================ ================
+-local version
*-Installed
>-currently in use
============================== ==================================================

10.2.3 OSX Homebrew Installation

If you are using a MAC and are using Homebrew, you can install SPRINGBOOTCLI with the following command:

$ brew Tap Pivotal/tap
$ brew Install Springboot

Homebrew will install spring into the usr/local/bin.

If you don't see a hint like this, your brew might not be new enough to perform a brew update and try again.

10.2.4 MacPorts Installation
If you're using a Mac, and you're using macports, you just need to do:

$ sudo port install Spring-boot-cli

10.2.5 command line Auto-completion

The SPRINGBOOTCLI provides auto-complete functionality for BASH and zsh shells. You can use it in any shell, or put it in a personal or system auto-complete dictionary. In the Debian system, system-wide scripts (which I don't quite know what that means) in/shell-completion/bash, where the script executes when the new Shelli starts. If you want to do this manually, you can use the sdkman!

$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring
$ spring 

If you are using homebrew or macports, then SPRINGBOOTCLI will automatically bind to the shell.

10.2.61 Examples of SPRINGCLI

Here is a very simple Web project that you can use to test whether the installation was successful. Create a file named App.groovy:

@RestController
class Thiswillactuallyrun {

    @RequestMapping ("/")
    String Home () {
        "Hello world!"
    }

}

Then execute through the shell:

$ Spring Run App.groovy

The first execution may be slower because you need to download the dependent library, and the next time.

Enter localhost:8080 in the browser and you can see:

Hello world!

10.3 Upgrading from an earlier version

If you want to upgrade from an earlier version, see "Release Notes" in the Project Wiki. where you'll see the relevant upgrade guide

If you want to upgrade an earlier CLI tool, you can use the package management tools above (e.g. brew upgrade). If manual installation, please refer to the Guide to upgrade, remember to change the environment variables.

11. Development of the first Springboot project

Let's look at some of the moving features of Springboot through a simple "Hello World" project. We'll use Maven to build, because most Ides support maven.
Before you start, please open a terminal to make sure we have the correct Java and MAVEN versions

$ java-version
java Version "1.7.0_51"
Java (tm) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot (tm ) 64-bit Server VM (build 24.51-b03, Mixed mode)
$ mvn-v
Apache maven 3.2.3 (33F8C3E1027C3DDDE99D3CDEBAD2656A31E8FDF4; 2014-08-11t13:58:10-07:00)
maven Home: /users/user/tools/apache-maven-3.1.1
Java version:1.7.0_51, vendor:oracle Corporation

This example needs to be in a separate folder. The following process by default you have created a suitable folder and is your current directory.

11.1 Creating a Pom.xml file
We need to create a pom.xml file. This file is a list of items. Open your text editor and type in the text:

<?xml version= "1.0" encoding= "UTF-8"?> <project xmlns= "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.exam

    Ple</groupid> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot- starter-parent</artifactid> <version>2.0.0.BUILD-SNAPSHOT</version> </parent> &lt ;! --Additional lines to being added here ...-<!--(you don't need this if you're using a. RELEASE version)-<repositories> <repository> <id>spring-snapshots</id&
            Gt
 <url>http://repo.spring.io/snapshot</url>           <snapshots><enabled>true</enabled></snapshots> </repository> < Repository> <id>spring-milestones</id> &LT;URL&GT;HTTP://REPO.SPRING.IO/MILESTONE&L t;/url> </repository> </repositories> <pluginRepositories> <pluginreposito 
        Ry> <id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url>
            </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>http://repo.spring.io/milestone</url> </pluginRepository> </pluginrepositor Ies> </project>

Then run the MVN package and you can ignore "jar will is empty-no content is marked for inclusion!" The Tips

At this point you can import it into your common IDE. For simplicity, we also use a text editor

11.2 Adding classpath dependencies
Springboot provides a lot of "starters" to make building simple. Our example has been used in the parent of the POM
Spring-boot-starter-parent. This spring-boot-starter-parent is a special starter with MAVEN's default configuration. It also provides a dependency-management module to omit the version label

Other "starters" also provide some dependencies for you to develop special programs that are used. Because we are developing a Web project, all we need to add on spring-boot-starter-web dependencies. But before that, let's look at the dependencies we have now

$ mvn dependency:tree

[INFO] Com.example:myproject:jar:0.0.1-snapshot

The MVN dependency:tree command prints out the current project dependencies. You can see that spring-boot-starter-parent does not provide other dependencies. We open the Pom.xml and add the Spring-boot-starter-web dependency under the parent block:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

If you run MVN dependency:tree again, you can see a lot more dependencies on the project, including the Tomcat server and the springboot itself.

11.3 Write code
In order to complete our project, we also need to create a Java file. MAVEN will automatically compile the source code under Src/main/java, and we'll create a file with the following src/main/java/example.java:

Import org.springframework.boot.*;
Import org.springframework.boot.autoconfigure.*;
Import org.springframework.stereotype.*;
Import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration Public
class Example {

    @RequestMapping ("/")
    String Home () {
        return "Hello world!";
    }

    public static void Main (string[] args) throws Exception {
        springapplication.run (example.class, args);
    }

}

Although there's not much code here, it's enough. Let's take a look at the next important part.

11.3.1 @RestController and @RequestMapping notes
The first comment in our example is @RestController. This comment has a special meaning in spring, meaning it is a controller, so spring uses it to process Web requests.
@RequestMapping This comment provides routing information. It tells Spring that all HTTP requests with the request path "/" should be handled by the home method. This comment also tells the spring to directly render the result as a string to the caller.

@RestController and @requestmapping are comments in the SPRINGMVC. You can learn more through the MVC section.

11.3.2 @EnableAutoConfiguration Notes
A second class-level comment is @EnableAutoConfiguration. This note tells Springboot to guess how you want to configure spring, mainly based on the jar package you add. Because Spring-boot-starter-web adds Tomcat and SPRINGMVC, Springboot will think you are developing a Web project and configuring it.

Starters and Auto-configuration
Auto-configuration are designed to work better with "starters". But these two concepts are not directly connected. You are free to choose the jar dependencies, and Spring-boot will still be well-equipped to automatically configure your project.

11.3.3 "Main" method
The final part is the main method of the application. This is the standard Java program entry method. Our main method delegates to Springboot's Springapplication class's Run method. Springapplication will launch our program to start the automatic configuration of Tomcat. We need to pass the Example.class as a parameter to springapplication.

11.4 Running Instances

At this point our program is ready to work. Because we have spring-boot-starter-parent POM configured, we can run our program. To start the application by entering MVN Spring-boot:run in the console

$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ))
  '  |____|. __|_| |_|_| |_\__, |////
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot::  (v2.0.0.build-snapshot)
..... ....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)

At this point we can enter localhost:8080 in the browser to see the relevant output:

Hello world!

Graceful exit program can be achieved by CTRL + C

11.5 Generating executable jar files
Finally, let's end the example by generating an executable jar package. The executable jar package contains all the classes and dependencies required for execution.

The executable jar package and Java
Java itself do not provide a standard way to generate executable jars. This can cause problems when we distribute the app.

to solve this problem, many developers use "Uber" jars. "Uber" jars just simply hits the generated class into a compressed package. The problem is that you can't see which classes are applied in the program. Another problem is that multiple files of the same file name (different content) are used

springboot different ways to allow you to package directly

In order to create an executable jar package, we need to add spring-boot-maven-plugin to the Pom file.

<build>
    <plugins>
        <plugin>
            <groupid>org.springframework.boot</groupid >
            <

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.