zoukankan      html  css  js  c++  java
  • spring-boot 速成(1) helloworld

    一、mac上安装

    $ brew tap pivotal/tap
    $ brew install springboot

    安装成功后,可在终端查看命令行

    ➜  ~ spring --version
    Spring CLI v1.5.2.RELEASE

    二、极速体验hello world

    随便开个vim啥的,敲几行代码:

    @RestController
    class ThisWillActuallyRun {
        @RequestMapping("/")
        String home() {
            "Hello World!"
        }
    }
    

    保存成app.groovy,然后在终端下就可以运行了:

    spring run app.groovy
    

    不要退出,然后在浏览器里浏览http://localhost:8080 ,没错,一个自带webserver容器的web应用就这样跑起来了。

    三、gradle 项目

    3.1 build.gradle

    buildscript {
    	ext {
    		springBootVersion = '1.5.2.RELEASE'
    	}
    	repositories {
    		mavenCentral()
    	}
    	dependencies {
    		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    	}
    }
    
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    
    jar {
    	baseName = 'spring-boot-web-demo'
    	version = '0.0.1-SNAPSHOT'
    }
    
    sourceCompatibility = 1.8
    
    repositories {
    	mavenCentral()
    }
    
    
    dependencies {
    	compile('org.springframework.boot:spring-boot-starter-web')
    	compileOnly('org.projectlombok:lombok')
    	testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    

    3.2 项目结构

    点击查看原图

    3.3 配置文件application.yml

    1 server:
    2   port: 9090 #服务器端口
    3   context-path: "/jimmy" #context-path
    4 spring:
    5   main:
    6     banner-mode: "off" #启动时是否在控制台/日志里输出Spring字样Banner

    spring-boot推荐配置使用新的yaml格式,更多默认的配置项请见参考文档2

    3.4 运行及打包

    spring-boot插件为gradle新增了2个task:bootRun、bootRepackage

    分别用于运行及打包

    gradle bootRun 、gradle bootRepackage 大家试下即可。打包成功后,/build/libs 下将生成可执行的jar包,复制到服务器上,java -jar spring-boot-web-demo-0.0.1-SNAPSHOT.jar 完事

    参考文档:

    1、http://docs.spring.io/spring-boot/docs/current/reference/html/getting-started-installing-spring-boot.html

    2、http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties

  • 相关阅读:
    SDUT 猴子分桃
    SDUT 母牛的故事
    SDUT 小鑫の日常系列故事(六)——奇遇记 递推算法
    SDUT 爬楼梯
    SDUT 王小二切饼
    linux 排序命令sort
    shell 判断语句
    shell统计指定范围内的所有质数以及它们的和
    shell 1到指定数累加
    shell九九乘法表
  • 原文地址:https://www.cnblogs.com/yjmyzz/p/spring-boot-helloworld.html
Copyright © 2011-2022 走看看