標籤:tap demo 瀏覽器 pen 代碼 group uil conf exce
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等價於@[email protected]+其他配置
EnableAutoConfiguration:springboot啟動自動設定處理,ComponentScan掃描子包或其他包,
在springboot中,可以把controller單獨抽離到子包中,springboot自動誰掃描到,並執行.
注:剛開始時測試都沒問題,後台加了幾個測試方法後,頁面就一直報錯找不到路徑了,待研究.找到原因後更新
7,路徑映射:@RestController:該註解做自由映射路徑標籤,可以有不同的風格,可以重寫URL,使用該註解時,可以不使用@ResponseBody
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包.
springboot學習demo