SpringBoot入門|-連載1

1.什麼是Spring Boot?

​ 隨着動態語言的流行(Ruby.Grooy.Scala.Nodejs),Java的開發顯得格外的笨重: 繁多的配置、低下的開發效率、複雜的部署流程以及第三方技術集成難度大。
​ 在上述環境下,SpringBoot應運而生。它使用「習慣優於配置」(項目中存在大量的配置,此外還內置一個習慣性的配置,讓你無須手動進行配置) 的理念讓你的項目快速運行起來。使用SpringBoot很容易建立一個獨立運行(運行jar,內嵌Servlet容器)準生產級別的基於Sring框架的項目,使用SpringBoot你能夠不用或者只須要不多的Spring配置。java

2.Spring Boot的優缺點

優勢
(1) 快速構建項目;
(2) 對主流開發框架的無配置集成;
(3) 項目可獨立運行,無須外部依賴Servlet容器;
(4) 提供運行時的應用監控;
(5) 極大地提升了開發、部署效率:
(6) 與雲計算的自然集成。web

缺點
(1) 書籍文檔較少且不夠深刻。
(2) 若是你不認同Spring框架,這也許是它的缺點,但建議你必定要使用Spring框架。spring

3.建立spring boot 工程

3.1.第一步

SpringBoot入門|-連載1

新建project ,選擇Spring Initializr,選擇jdk,點擊next瀏覽器

3.2.第二步

SpringBoot入門|-連載1

須要修改如下幾個參數tomcat

Group:組名app

artifact::項目名框架

java version:jdk版本maven

packaging:打包方式,若是是web項目打war包,若是不是打成jar包。ide

點擊nextspring-boot

3.3.第三步

SpringBoot入門|-連載1

根據須要選擇須要的依賴,咱們此時暫時不選擇,點擊next。

3.4.第四步

SpringBoot入門|-連載1

設置工程的名稱和路徑,點擊finish完成。

注意:項目新建成功以後maven配置是idea默認的,因此要檢查是否須要修改。

4.pom文件配置及啓動

4.1.設置spring boot的parent

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>1.5.10.RELEASE</version>
   <relativePath/> <!-- lookup parent from repository -->
</parent>

說明:Spring boot的項目必需要將parent設置爲spring boot的parent,該parent包含了大量默認的配置,大大簡化了咱們的開發。

4.2.導入spring boot的web支持

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

4.3.添加Spring boot的測試插件

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

4.4.添加spring boot的tomcat插件

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

4.5.新建一個StudentController類

@Controller
public class StudentController {
    @RequestMapping("hello")
    @ResponseBody
    public String hello(){
        return "Hello Spring Boot!";
    }
}

4.6.運行spring boot 的入口方法(Application)

運行效果:
SpringBoot入門|-連載1

若看到以上效果則項目啓動成功,此時tomcat默認端口8080。

4.7.測試

打開瀏覽器,輸入localhost:8080/hello,若出現如下效果則成功。

SpringBoot入門|-連載1

相關文章
相關標籤/搜索