Spring Boot 2.0 (Ⅰ) Getting Started

Spring Boot 2.0 (Ⅰ) Getting Started

  • Spring Boot 2.0 (Ⅰ) Getting Started
    • 系统和环境
    • github
    • Maven 依赖
      • parent
      • dependencyManagement
      • Spring Boot CLI
        • 安装
        • 简单使用
    • 如何运行
    • 升级

系统和环境

行内代码修饰的是本系列使用的环境

  • Windows10
  • Spring Boot 2.0.3.RELEASE
  • Java8+
  • Spring Framework 5.0.7.RELEASE+
  • Maven 3.2+(Maven 3.5)/Gradle 4+
  • Tomcat 8.5+(spring boot default)/Jetty 9.4+/Undertow 1.4+/任何一个兼容Servlet 3.1+的容器

github

spring-boot-demo

Maven 依赖

parent



<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">
    <parent>
        <artifactId>spring-boot-demoartifactId>
        <groupId>github.clyoudu.spring.bootgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>spring-boot-maven-installartifactId>

    <name>spring-boot-maven-installname>
    <url>http://www.example.comurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

project>

dependencyManagement



<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.0modelVersion>

    <artifactId>spring-boot-maven-install2artifactId>

    <name>spring-boot-maven-install2name>
    <url>http://www.example.comurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
    properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.0.3.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>
project>

Spring Boot CLI

Spring Boot Command Line Interface,只考虑在Windows上的安装、配置和使用。

安装

下载地址:spring-boot-cli-2.0.3.RELEASE-bin.zip。

解压。

添加环境变量:SPRING_PATH=/path/to/sring/boot/root/dir

编辑PATH环境变量,末未添加:%SPRING_HOME%\bin

验证:cmd -> spring --version -> Spring CLI v2.0.3.RELEASE

简单使用

对于熟悉spring boot的选手来说,这个工具略显鸡肋,但是对于初次学习的人来说,可以快速初始化一个项目,也可以很快打包、启动一个spring boot项目。

  1. 写一个正常的SpringMVC Controller

    package github.clyoudu.spring.boot;
    
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * Hello world!
     */
    @RestController
    public class App {
    
        @RequestMapping("/{name}")
        public String hello(@PathVariable(value = "name") String name){
            return "Hello " + name;
        }
    
    }
  2. 打开命令提示符窗口;
  3. 切换工作目录到Controller对应目录;
  4. 执行命令:sping run App.java,等待控制抬输出启动成功消息;
  5. 浏览器输入http://localhost:8080/clyoudu
  6. 页面显示:Hello clyoudu

很明显,就样可以快速构建一个spring boot项目。当然这并不是CLI唯一的功能,还有跟多高级的功能,比如打包、初始化项目等。

详见:新手必看,Spring Boot CLI 必会必知

如何运行

开发的时候,直接写一个包含main方法的类,加上SpringBootApplication注解,mian方法内调用SpringApplication.run(App.class,args);即可:

package github.clyoudu.spring.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 */
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

然后run这个类就能启动,但是注意这个类最好写在所有包的最外层,以便于spring boot扫描组件、配置。

当需要打包到不同的环境运行时,可以使用maven插件打包,先添加插件:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
        plugin>
    plugins>
build>

然后运行命令:mvn -DskipTests=true package,在target文件夹就可以找到your-project-name-1.0-SNAPSHOT.jar,最后使用java -jar your-project-name-1.0-SNAPSHOT.jar就可以启动,当然这里有一些参数可以配置,比如使用哪个profile,后面再记录。

升级

因为spring boot 1.xspring boot 2.x差别较大,几乎不能直接从1.x升级到2.x,因此对于升级,sring boot有一套专门的解决方案,这里不考虑升级的情况。

详见:Spring Boot 2.0 Migration Guide

你可能感兴趣的:(Java,spring,boot)