1,父項目,在pom中加入
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
2,創建子maven項目,
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.5.RELEASE</version>
</plugin>
</plugins>
</build>
3,如果報錯 Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE:run (default-cli) on project springboot-base: An exception occurred while running. null: InvocationTargetException: Connector configured to listen on port 8080 failed to start -> [Help 1]
請在子maven項目的pom里加上紅色標記的plugin
4,啟動后再瀏覽器輸入http://localhost:8080/
5,使用springboot做單元測試
6,springboot 使用的注解:@SpringBootApplication等價於@EnableAutoConfiguration+@ComponentScan+其他配置
EnableAutoConfiguration:springboot啟動自動配置處理,ComponentScan掃描子包或其他包,
在springboot中,可以把controller單獨抽離到子包中,springboot自動誰掃描到,並執行.
7,路徑映射:@RestController:該注解做自由映射路徑標簽,可以有不同的風格,可以重寫URL
8,程序員都會喜歡的倆個依賴:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
這倆個依賴成對使用,主要是在改動某些代碼時,可以不用再重新手動去重新啟動.
注:只是方法和返回的改動時,springboot自動加載,方法參數和注解改動無效.
9,測試幾個方法都沒問題后,可以打包發布了
發布插件:
打包后可以在cmd中使用Java -jar xxxx.jar,自動執行,無需啟動容器,訪問URL,頁面顯示成功,Linux下也是一樣,不需要容器,只需要一個jar包.