1. 程式人生 > >SpringBoot框架簡介及搭建

SpringBoot框架簡介及搭建

pro org 除了 運行 maven sha 方法 variable tid

Spring Boot 簡介

1. 什麽是SpringBoot

1.1 Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者。

2. SpringBoot的優點

2.1 去除了大量的xml配置文件

2.2 簡化復雜的依賴管理

2.3 配合各種starter使用,基本上可以做到自動化配置

2.4 快速啟動容器

創建獨立Spring應用程序,嵌入式Tomcat,Jetty容器,無需部署WAR包,簡化Maven及Gradle配置(spring boot項目的入口是一個main方法,運行該方法即可)(如需打包,可直接打成 jar 包,java -jar ***.jar 即可運行)
  1. 下面的應用程序啟動器是由Spring Boot在org.spring框架下提供的

技術分享圖片

Spring Boot 搭建
我們都知道,在一般的項目中是需要引入很多包和很多配置文件的,最坑的是在以前的項目中可不是僅僅引入包就可以的,還要避免版本沖突等問題,總體來說還是比較頭疼的。那麽在SpringBoot中就不需要再擔心包版本或者缺包的問題了,只需一段配置 SpringBoot就會幫我們全部搞定。我們來看一下,只引入了一個核心依賴與web支持 SpringBoot 會幫我們導入哪些包吧!(以下就是本demo引入的所有依賴了)

  1. pom.xml配置

復制代碼

org.springframework.boot
spring-boot-starter-parent 2.0.2.RELEASE
org.springframework.boot spring-boot-starter-web 1.5.9.RELEASE 復制代碼 2. 程序啟動入口(直接運行main方法即可) 復制代碼 @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 復制代碼 為了方便測試,我們在這裏添加一個Controller、Entity 復制代碼 import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.firstboot.lpx.entity.Person; @RestController public class MyController { @RequestMapping(value="/getPerson/{age}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE) public Person getPerson(@PathVariable int age){ Person p = new Person(); p.setName("小李"); p.setAge(age); return p; } } 復制代碼 復制代碼 public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 復制代碼 3. 啟動SpringBoot程序 並 訪問getPerson接口 ![](http://i2.51cto.com/images/blog/201809/18/d13678f270910667bfc0915965465454.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)

SpringBoot框架簡介及搭建