Spring Boot (涓�): 蹇�熸瀯寤� WebMvc 搴旂敤

1. 寮曡█

浣滀负澶氬勾鐨� Java 鎴愮尶锛屽湪 Spring 鎵崱 Java 鐣岀殑鏃朵唬锛屼笉鍙伩鍏嶇殑琚暱闀跨殑 ...ApplicationContext.xml 鍒峰睆銆傛洿鍙偛鐨勬槸涓嶇琚埛杩囧灏戝睆锛岃铏愯繃澶氬皯娆★紝渚濈劧娌℃硶璁颁綇閭i暱闀跨殑閰嶇疆椤广��

缁堜簬锛屽湪閰嶇疆娣卞潙鑻﹁嫤鐓庣啲涔嬪悗锛屾湁浜哄瀭涓嬩簡涓�鏍圭怀绱紝甯︽潵浜� Spring Boot銆�

Spring Boot鍏呭垎鍒╃敤浜� JavaConfig 閰嶇疆妯″紡浠ュ強鈥滅害瀹氫紭浜庨厤缃�濈殑鐞嗗康锛岃兘澶熸瀬澶х殑绠�鍖栧熀浜� Spring 鐨勫簲鐢ㄥ紑鍙戙�備负浜嗙畝鍖栦緷璧栧浘锛孊oot 鐨勫姛鑳芥槸妯″潡鍖栫殑锛岄�氳繃瀵煎叆 Boot 鎵�璋撶殑鈥渟tarter鈥� 妯″潡锛屽彲浠ュ皢璁稿鐨勪緷璧栨坊鍔犲埌宸ョ▼涔嬩腑銆�

涓嬮潰锛屾垜浠氨浣跨敤 IDEA 鍜� Gradle 鏋勫缓涓�涓渶绠�鍗曠殑 SpringMVC 搴旂敤銆�

2. Hello MVC

2.1. 鍒涘缓涓�涓熀浜� Gradle 鐨� Java Web 搴旂敤

Spring Boot (涓�): 蹇�熸瀯寤� WebMvc 搴旂敤_第1张图片
idea gradle web

鍒涘缓涔嬪悗锛屼唬鐮佺粨鏋勫涓嬶細

Spring Boot (涓�): 蹇�熸瀯寤� WebMvc 搴旂敤_第2张图片
structure

2.2. 娣诲姞 Spring Boot Web 渚濊禆

build.gradle 鍐呭濡備笅锛�

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. 鎵撲釜鎷涘懠鍚�

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 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. 灞曠ず鎴愭灉

杩愯 Application.java, 鎺у埗鍙板緢鍙嬪ソ鍦板憡鐭ヨ繖鏄熀浜� Spring Boot 鐨勫簲鐢細

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

......

娴忚鍣ㄨ闂� http://localhost:8080/hello, 鏄剧ず锛�

Spring Boot (涓�): 蹇�熸瀯寤� WebMvc 搴旂敤_第3张图片
hello boot

OK锛� 绗竴涓� Spring Boot MVC 绋嬪簭瀹屾垚銆�

婧愪唬鐮佸彲浠ュ湪 github 鎵惧埌銆�

3. 淇敼閰嶇疆

Spring Boot 鍙互鏂逛究鍦伴�氳繃 application.properties 鎴栬�� application.yml 璋冩暣榛樿鐨勯厤缃��

濡傞渶瑕佷慨鏀瑰簲鐢ㄥ彂甯冪鍙e拰鐩綍锛屽湪 src/main/resources 涓垱寤� application.properties, 鍐呭濡備笅锛�

server.port=8081
server.contextPath=/springboot

鍚姩鍚庤闂� http://localhost:8081/springboot/hello 鐪嬬湅鏁堟灉鍚с��

你可能感兴趣的:(Spring Boot (涓�): 蹇�熸瀯寤� WebMvc 搴旂敤)