spring-boot入門,
Eclipse中安裝STS外掛程式(1)線上安裝
Help--Eclipse Marketplace
搜尋“STS”,點擊“install”安裝
後面就是一直下一步或者完成
(2)離線安裝
開啟網頁 http://spring.io/tools/sts/all
下載適合自己的eclipse版本的STS壓縮包
下載後,在eclipse操作: Help--Install New Software--Add--Archive,添加已下載的zip包
安裝有Spring IDE字樣的外掛程式即可,取消勾選自動更新,接下來就有Next就點Next,有Finish就點Finish
2. 建立Spring-boot項目
建立->其它
我這裡沒啥要改的,預設demo就行
下面其他第三方庫可以一個都不加入,可以在使用的時候再pom裡面再次添加,如果知道需要哪些也可以提前勾上
完成
3. 運行Spring-boot項目
(1)右鍵DemoApplication中的main方法,Run As -> Spring Boot App,項目就可以啟動了
1 package com.example.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class DemoApplication { 8 9 public static void main(String[] args) {10 SpringApplication.run(DemoApplication.class, args);11 }12 }
(2)如果要運行hello world,則使用@RestController註解,並且添加hello方法
得提前引入web相關的依賴,如果之前勾選了,就不用再加了
1 <dependency>2 <groupId>org.springframework.boot</groupId>3 <artifactId>spring-boot-starter-web</artifactId>4 </dependency>
1 package com.example.demo; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @RestController 9 @SpringBootApplication10 public class DemoApplication {11 12 @RequestMapping("/") 13 public String hello(){ 14 return"Hello world!"; 15 } 16 public static void main(String[] args) {17 SpringApplication.run(DemoApplication.class, args);18 }19 }
如何運行我們的Application,看到hello world的輸出呢?
第一種方式是直接運行main方法:
選中DemoApplication的main方法 -> 右鍵 -> Run as ->Java Applicacation,之後開啟瀏覽器輸入地址:http://127.0.0.1:8080/ 或者http://localhost:8080/就可以看到Hello world!了。
第二種方式:
右鍵project –> Run as –> Maven build –> 在Goals裡輸入spring-boot:run ,然後Apply,最後點擊Run
4. 打包Spring-boot項目1.命令:clean package
2.執行命令:java –jar xxxxxx.jar