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 搴旂敤
鍒涘缓涔嬪悗锛屼唬鐮佺粨鏋勫涓嬶細
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, 鏄剧ず锛�
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 鐪嬬湅鏁堟灉鍚с��