Spring Boot (一): 快速構(gòu)建 WebMvc 應(yīng)用

1. 引言

作為多年的 Java 成猿,在 Spring 掃蕩 Java 界的時(shí)代,不可避免的被長(zhǎng)長(zhǎng)的 ...ApplicationContext.xml 刷屏。更可悲的是不管被刷過多少屏,被虐過多少次,依然沒法記住那長(zhǎng)長(zhǎng)的配置項(xiàng)。

終于,在配置深坑苦苦煎熬之后,有人垂下了一根繩索,帶來了 Spring Boot。

Spring Boot充分利用了 JavaConfig 配置模式以及“約定優(yōu)于配置”的理念,能夠極大的簡(jiǎn)化基于 Spring 的應(yīng)用開發(fā)。為了簡(jiǎn)化依賴圖,Boot 的功能是模塊化的,通過導(dǎo)入 Boot 所謂的“starter” 模塊,可以將許多的依賴添加到工程之中。

下面,我們就使用 IDEA 和 Gradle 構(gòu)建一個(gè)最簡(jiǎn)單的 SpringMVC 應(yīng)用。

2. Hello MVC

2.1. 創(chuàng)建一個(gè)基于 Gradle 的 Java Web 應(yīng)用

idea gradle web

創(chuàng)建之后,代碼結(jié)構(gòu)如下:

structure

2.2. 添加 Spring Boot Web 依賴

build.gradle 內(nèi)容如下:

group 'li.fyun'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8
targetCompatibility = 1.8

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

jar {
    baseName = 'springboot'
    version = '0.1.0'
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
}

2.3. 打個(gè)招呼吧

Application.java

package li.fyunli.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

/**
 * Created by fyunli on 16/4/1.
 */
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        application.listeners();
        return application.sources(applicationClass);
    }

    private static Class<Application> applicationClass = Application.class;

}

HelloController.java

package li.fyunli.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by fyunli on 16/4/1.
 */
@Controller
public class HelloController {

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello Boot!";
    }

}

2.4. 展示成果

運(yùn)行 Application.java, 控制臺(tái)很友好地告知這是基于 Spring Boot 的應(yīng)用:

...
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.3.3.RELEASE)

......

瀏覽器訪問 http://localhost:8080/hello, 顯示:

hello boot

OK, 第一個(gè) Spring Boot MVC 程序完成。

源代碼可以在 github 找到。

3. 修改配置

Spring Boot 可以方便地通過 application.properties 或者 application.yml 調(diào)整默認(rèn)的配置。

如需要修改應(yīng)用發(fā)布端口和目錄,在 src/main/resources 中創(chuàng)建 application.properties, 內(nèi)容如下:

server.port=8081
server.contextPath=/springboot

啟動(dòng)后訪問 http://localhost:8081/springboot/hello 看看效果吧。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容