Spring Boot 快速配置
Spring Boot應用的快速構建
本文使用Maven方式快速構建Spring Boot應用
幾分鐘教程
這裡, 我藉助IDEA快速建立一個maven項目 (你也可以通過手動或者eclipse建立)
1.1 New Project1.1.1 建立一個新項目, 使用maven的方式
1.1.2 填寫一些應用相關的資訊, GroupId, ArtifactId
1.1.3 填寫項目儲存的本地路徑, 點擊Finish
1.1.4 如上操作, 建立完成一個空的Maven項目
1.2 引入Spring Boot的依賴1.2.1 編輯pom.xml檔案
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>info.chiwm</groupId> <artifactId>log4j2</artifactId> <version>1.0-SNAPSHOT</version> <!-- 映入spring boot 依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies></project>
1.2.2 編寫項目啟動的主入口代碼
在 ./src/java/main
目錄建立目錄, 例如 info/chiwm/boot
. 所有的代碼都在該目錄下編輯, 這裡定義為 代碼根目錄
建立java檔案 Application.java
作為入口檔案, 編碼如下
package info.chiwm.boot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * @author chiwm@kuyumall.com * @ClassName: Application * @Description: * @date 2018/1/3 上午11:29 */@SpringBootApplication(scanBasePackages = "info.chiwm.log4j2")public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
可以直接Application.java
中編寫controller
商務邏輯並且啟動.
1.2.3 編寫
controller
層商務邏輯
但這裡我將controller
模組的邏輯編排在相對應的目錄下, 可以使範例程式碼組織得更加明了
在 `代碼根目錄` 下建立目錄 `controller`, 在該目錄下建立樣本controller , `JsonController.java`
package info.chiwm.boot.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * @author wanminchi@gmail.com * @ClassName: JsonController * @Description: * @date 2018/1/3 下午1:11 */@RestController@RequestMapping("/json")public class JsonController { @RequestMapping(value = "/get", method = RequestMethod.GET) @ResponseBody public String getJson(HttpServletRequest req, HttpServletResponse res) { return "{\"name\":\"chi\"}"; }}
1.3 啟動項目
在IDEA中, RUN Application.java即可啟動.
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.9.RELEASE)2018-01-03 13:35:23.092 INFO 78070 --- [ main] info.chiwm.boot.Application : Starting Application on chiwanmindeMacBook-Pro.local with PID 78070 (/Users/chiwanmin/code/java/boot/target/classes started by chiwanmin in /Users/chiwanmin/code/java/boot)2018-01-03 13:35:23.096 INFO 78070 --- [ main] info.chiwm.boot.Application : No active profile set, falling back to default profiles: default2018-01-03 13:35:23.192 INFO 78070 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@74a10858: startup date [Wed Jan 03 13:35:23 CST 2018]; root of context hierarchy2018-01-03 13:35:25.083 INFO 78070 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2018-01-03 13:35:25.092 INFO 78070 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]2018-01-03 13:35:25.093 INFO 78070 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.232018-01-03 13:35:25.155 INFO 78070 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2018-01-03 13:35:25.155 INFO 78070 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1968 ms2018-01-03 13:35:25.258 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2018-01-03 13:35:25.261 INFO 78070 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2018-01-03 13:35:25.645 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@74a10858: startup date [Wed Jan 03 13:35:23 CST 2018]; root of context hierarchy2018-01-03 13:35:25.712 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/json/get],methods=[GET]}" onto public java.lang.String info.chiwm.boot.controller.JsonController.getJson(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2018-01-03 13:35:25.715 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2018-01-03 13:35:25.715 INFO 78070 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)2018-01-03 13:35:25.740 INFO 78070 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-03 13:35:25.741 INFO 78070 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-03 13:35:25.776 INFO 78070 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-01-03 13:35:25.912 INFO 78070 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-01-03 13:35:25.993 INFO 78070 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2018-01-03 13:35:25.999 INFO 78070 --- [ main] info.chiwm.boot.Application : Started Application in 3.522 seconds (JVM running for 4.575)2018-01-03 13:55:15.625 INFO 78070 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'2018-01-03 13:55:15.626 INFO 78070 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2018-01-03 13:55:15.660 INFO 78070 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 34 ms
服務端啟動, Maven方式打包, 命令啟動也可..