Spring Boot provides a number of “Starters” that let you add jars to your classpath. Our sample application has already used spring-boot-starter-parent in the parent section of the POM. The spring-boot-starter-parent is a special starter that provides useful Maven defaults. It also provides a dependency-management section so that you can omit version tags for “blessed” dependencies.
Spring Boot 提供了一些“Starters”用于您增加一些jars包到您的类路径下。我们的示例应用已经使用了spring-boot-starter-parent在上一节的POM文件中。spring-boot-starter-parent是一个提供了非常有用的Maven默认的特殊的starter。它同样提供了一个dependency-management部分,以便于您可以为一些“幸福”依赖忽略版本标签。
1 2 3 4 5
Other “Starters” provide dependencies that you are likely to need when developing a specific type of application. Since we are developing a web application, we add a spring-boot-starter-web dependency. Before that, we can look at what we currently have by running the following command:
The mvn dependency:tree command prints a tree representation of your project dependencies. You can see that spring-boot-starter-parent provides no dependencies by itself. To add the necessary dependencies, edit your pom.xml and add the spring-boot-starter-web dependency immediately below the parent section:
If you run mvn dependency:tree again, you see that there are now a number of additional dependencies, including the Tomcat web server and Spring Boot itself.
We need to start by creating a Maven pom.xml file. The pom.xml is the recipe that is used to build your project. Open your favorite text editor and add the following:
<!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
1 2 3
The preceding listing should give you a working build. You can test it by running mvn package (for now, you can ignore the “jar will be empty - no content was marked for inclusion!” warning).
上面的列表应该已经给了您一个可工作的构建。您可以使用mvn package来运行测试它(现在,你可以忽略“jar will be empty - no content was marked for inclusion!“的警告)
1 2 3
At this point, you could import the project into an IDE (most modern Java IDEs include built-in support for Maven). For simplicity, we continue to use a plain text editor for this example.
10.2 Installing the Spring Boot CLI(安装Spring Boot命令行界面)
1 2 3 4 5 6 7
The Spring Boot CLI (Command Line Interface) is a command line tool that you can use to quickly prototype with Spring. It lets you run Groovy scripts, which means that you have a familiar Java-like syntax without so much boilerplate code. You do not need to use the CLI to work with Spring Boot, but it is definitely the quickest way to get a Spring application off the ground.
Spring Boot CLI(命令行界面)是一个命令行工具,您可以使用它来快速使用Spring进行原型开发 它允许您运行Groovy脚本,这意味着您不需要太多样板代码就能以类似于java的语法进行开发。 尽管您不需要使用CLI去运行Spring Boot ,但这绝对是获得Spring应用程序的最快捷方式
10.2.1 Manual Installation(手动安装)
1 2 3 4 5 6 7 8 9 10
You can download the Spring CLI distribution from the Spring software repository: • spring-boot-cli-2.1.0.BUILD-SNAPSHOT-bin.zip • spring-boot-cli-2.1.0.BUILD-SNAPSHOT-bin.tar.gz Cutting edge snapshot distributions are also available. Once downloaded, follow the INSTALL.txt instructions from the unpacked archive. In summary, there is a spring script (spring.bat for Windows) in a bin/directory in the .zip file. Alternatively, you can use java -jar with the .jar file (the script helps you to be sure that the classpath is set correctly).
SDKMAN! (The Software Development Kit Manager) can be used for managing multiple versions of various binary SDKs, including Groovy and the Spring Boot CLI. Get SDKMAN! from sdkman.io and install Spring Boot by using the following commands:
$ sdk install springboot $ spring --version Spring Boot v2.1.0.BUILD-SNAPSHOT
1 2
If you develop features for the CLI and want easy access to the version you built, use the following commands:
如果您为CLI开发功能并希望轻松访问您创建的版本,请使用以下命令:
1 2 3 4 5
$ sdk install springboot dev /path/to/spring-boot/spring-boot-cli/ target/spring-boot-cli-2.1.0.BUILD-SNAPSHOT-bin/spring-2.1.0.BUILD-SNAPSHOT/ $ sdk default springboot dev $ spring --version Spring CLI v2.1.0.BUILD-SNAPSHOT
1 2
The preceding instructions install a local instance of spring called the dev instance. It points at your target build location, so every time you rebuild Spring Boot,spring is up-to-date. You can see it by running the following command:
================================================================================ Available Springboot Versions ================================================================================ > + dev * 2.1.0.BUILD-SNAPSHOT
================================================================================ + - local version * - installed > - currently in use ================================================================================
If you are on a Mac and use MacPorts, you can install the Spring Boot CLI by using the following command:
如果您使用Mac,并且使用MacPorts,您可以通过以下命令安装Spring Boot CLI
1
$ sudo port install spring-boot-cli
10.2.5 Command-line Completion(命令行完成)
1 2 3 4 5 6 7 8 9
The Spring Boot CLI includes scripts that provide command completion for the BASH and zsh shells. You can source the script (also named spring) in any shell or put it in your personal or system-wide bash completion initialization. On a Debian system, the system-wide scripts are in /shell-completion/bash and all scripts in that directory are executed when a new shell starts. For example, to run the script manually if you have installed by using SDKMAN!, use the following commands:
Spring Boot CLI为BASH和zsh shells提供了命令行完成脚本。您可以在任何shell或者您个人的或者系统范围内的bash中初始化source该脚本。在Debian系统,全系统脚本在/shell-completion/bash,并且所有新建的可执行脚本都在该路径下。比如说,在您已经安装使用SDKMAN的情况下,去手动运行该脚本,使用如下命令:
1 2 3
$ . ~/.sdkman/candidates/springboot/current/shell-completion/bash/spring $ spring <HIT TAB HERE> grab help jar run test version
1 2 3
If you install the Spring Boot CLI by using Homebrew or MacPorts, the command-line completion scripts are automatically registered with your shell.
The first run of your application is slow, as dependencies are downloaded. Subsequent runs are much quicker.
首次运行您的应用会比较慢,因为依赖库需要被下载,以后每次运行会比较快
1 2
Open localhost:8080 in your favorite web browser. You should see the following output:
在您喜爱的浏览器中打开 localhost:8080 ,您将会看到如下的输出:
1
Hello World!
10.3 Upgrading from an Earlier Version of Spring Boot(从一个旧版本升级Spring Boot)
1 2 3 4
If you are upgrading from an earlier release of Spring Boot, check the “migration guide” on the project wiki that provides detailed upgrade instructions. Check also the“release notes” for a list of “new and noteworthy” features for each release.
To upgrade an existing CLI installation, use the appropriate package manager command (for example, brew upgrade) or, if you manually installed the CLI, follow thestandard instructions, remembering to update your PATH environment variable to remove any older references.
11. Developing Your First Spring Boot Application(部署你的第一个Spring Boot应用)
1 2 3
This section describes how to develop a simple “Hello World!” web application that highlights some of Spring Boot’s key features. We use Maven to build this project, since most IDEs support it.
The spring.io web site contains many “Getting Started” guides that use Spring Boot. If you need to solve a specific problem, check there first.You can shortcut the steps below by going to start.spring.io and choosing the "Web" starter from the dependencies searcher. Doing so generates a new project structure so that you can start coding right away. Check the Spring Initializr documentation for more details.
This sample needs to be created in its own folder. Subsequent
instructions assume that you have created a suitable folder and
that it is your current directory.
Spring Boot can be used with “classic” Java development tools or installed as a command line tool. Either way, you need Java SDK v1.8 or higher. Before you begin, you should check your current Java installation by using the following command: $ java -version If you are new to Java development or if you want to experiment with Spring Boot, you might want to try the Spring Boot CLI (Command Line Interface) first. Otherwise, read on for “classic” installation instructions.
Spring Boot 可以被传统的java部署工具部署,也可以被安装为命令行工具。同样的,您需要java1.8或更高的版本支持。在您开始之前,您可以使用如下命令检查一下您的JDK版本
10.1 Installation Instructions for the Java Developer(针对java开发者的安装介绍)
1 2 3 4 5 6 7 8 9 10
You can use Spring Boot in the same way as any standard Java library. To do so, include the appropriate spring-boot-*.jar files on your classpath. Spring Boot does not require any special tools integration, so you can use any IDE or text editor. Also, there is nothing special about a Spring Boot application, so you can run and debug a Spring Boot application as you would any other Java program. Although you could copy Spring Boot jars, we generally recommend that you use a build tool that supports dependency management (such as Maven or Gradle).
Spring Boot is compatible with Apache Maven 3.2 or above. If you do not already have Maven installed, you can follow the instructions at maven.apache.org.
Spring Boot与Apache Maven 3.2或更高版本兼容。 如果你 尚未安装Maven,您可以按照maven.apache.org上的说明进行操作。
1 2 3 4 5
On many operating systems, Maven can be installed with a package manager. If you use OSX Homebrew, try brew install maven. Ubuntu users can run sudo apt-get install maven. Windows users with Chocolatey can run choco install maven from an elevated (administrator) prompt.
Spring Boot dependencies use the org.springframework.boot groupId. Typically, your Maven POM file inherits from the spring-boot-starter- parentproject and declares dependencies to one or more “Starters”. Spring Boot also provides an optional Maven plugin to create executable jars. The following listing shows a typical pom.xml file:
Spring Boot 依赖使用 org.springframework.boot 作为groupId。通常,您的Maven POM文件继承于spring-boot-starter-parentproject 并且声明依赖于一个或者多个“Straters”。Sring Boot同样提供了可选的Maven插件去创建可运行jars包。以下列表展示了一个通常使用的pom.xml文件
<!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.BUILD-SNAPSHOT</version> </parent>
<!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
<!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
<!-- Add Spring repositories --> <!-- (you don't need this if you are using a .RELEASE version) --> <repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories> </project>
10.1.2 Gradle Installation(Gradle下的安装)
1 2 3 4 5 6 7
Spring Boot is compatible with Gradle 4. If you do not already have Gradle installed, you can follow the instructions at gradle.org. Spring Boot dependencies can be declared by using the org.springframework.boot group. Typically, your project declares dependencies to one or more“Starters”. Spring Boot provides a useful Gradle plugin that can be used to simplify dependency declarations and to create executable jars.
Spring Boot 兼容Gradle 4. 如果您还没有安装Gradle,您可以从 gradle.org中获取介绍文档。 Spring Boot依赖可以被声明为org.springframework.boot group。通常,您的项目被声明依赖于一个或者多个“Strarters”。Spring Boot 提供可用的Gradle 插件用于简化依赖声明和创建可执行jars包。
1 2 3 4 5
Gradle Wrapper The Gradle Wrapper provides a nice way of “obtaining” Gradle when you need to build a project. It is a small script and library that you commit alongside your code to bootstrap the build process. See docs.gradle.org/4.2.1/userguide/gradle_wrapper.html for details.
Spring Boot 2.1.0.BUILD-SNAPSHOT requires Java 8 or 9 and Spring Framework 5.0.5.RELEASE or above. Explicit build support is provided for Maven 3.2+ and Gradle 4.
Spring Boot 2.1。0.BUILD-SNAPSHOT 需要java8或者java9版本,以及Spring Framework 5.0.5.RELEASE 或更高版本。为Maven 3.2+和Gradle 4提供了明确的支持。
9.1 Servlet Containers(Servlet 容器)
1
Spring Boot supports the following embedded servlet containers:
Spring Boot支持如下内置容器
Name
Servlet Version
Tomcat 8.5
3.1
Jetty 9.4
3.1
Undertow 1.4
3.1
1
You can also deploy Spring Boot applications to any Servlet 3.1+ compatible container.
If you are getting started with Spring Boot,or “Spring” in general, start by reading this section. It answers the basic “what?”, “how?” and “why?” questions.It includes an introduction to Spring Boot, along with installation instructions.We then walk you through building your first Spring Boot application, discussing some core principles as we go.
Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can run. We take an opinionated view of the Spring platform and third-party libraries, so that you can get started with minimum fuss. Most Spring Boot applications need very little Spring configuration.
Spring Boot使您能够非常简单的创建一个可运行的基于Spring的,产品级别的,独立部署应用。我们用我们自己认为的观点来处理Spring平台和第三方库,以便于您能够以最小的代价开始您的应用。所有的Spring Boot应用都只需要很少的Spring配置
1 2 3
You can use Spring Boot to create Java applications that can be started by using java -jar or more traditional war deployments. We also provide a command line tool that runs “spring scripts”.
Our primary goals are: * Provide a radically faster and widely accessible getting-started experience for all Spring development. * Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults. * Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration). * Absolutely no code generation and no requirement for XML configuration.
String s = new String("stringette"); //这种写法会制造不必要的String实例,"stringette"本身就是一个实例,再使用new会再创建一个实例 String ss = "stringette"; //这种写法会复用一个实例,即便在多次调用的情况下,该实例被保存在字符串常量池中
/** * Created by mike on 2017/12/4. */ publicclassLongTest{
publicstaticvoidmain(String[] args){ long start = System.currentTimeMillis(); //获取开始时间 Long sum = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum += i; } long middle = System.currentTimeMillis(); //获取中间时间 System.out.println(middle-start);
long sum2 = 0L; for (long i = 0; i < Integer.MAX_VALUE; i++) { sum2 += i; } long end = System.currentTimeMillis(); //获取结束时间 System.out.println(end-middle); }