項目構建
我們采用maven構建SpringBoot工程,首先創建一個maven工程,對應的pom文件如下:
<properties> <java.version>1.8</java.version> </properties> <!--集成springboot的父依賴--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--打可執行jar包--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
創建Application.java
package com.ysl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application.class,args); } }
運行main方法,運行結果如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.7.RELEASE) 2018-03-03 10:28:24.028 INFO 6358 --- [ main] com.ysl.Application : Starting Application on master with PID 6358 (/home/workspace/springboottest/target/classes started by ysl in /home/workspace/springboottest) 2018-03-03 10:28:24.038 INFO 6358 --- [ main] com.ysl.Application : No active profile set, falling back to default profiles: default 2018-03-03 10:28:24.218 INFO 6358 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy 2018-03-03 10:28:27.001 INFO 6358 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2018-03-03 10:28:27.025 INFO 6358 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-03-03 10:28:27.026 INFO 6358 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.20 2018-03-03 10:28:27.141 INFO 6358 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-03-03 10:28:27.142 INFO 6358 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2936 ms 2018-03-03 10:28:27.294 INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2018-03-03 10:28:27.299 INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-03-03 10:28:27.300 INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-03-03 10:28:27.300 INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-03-03 10:28:27.300 INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-03-03 10:28:27.669 INFO 6358 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy 2018-03-03 10:28:27.755 INFO 6358 --- [ 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-03-03 10:28:27.756 INFO 6358 --- [ 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-03-03 10:28:27.795 INFO 6358 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-03 10:28:27.796 INFO 6358 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-03 10:28:27.835 INFO 6358 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-03-03 10:28:28.143 INFO 6358 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-03-03 10:28:28.252 INFO 6358 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2018-03-03 10:28:28.261 INFO 6358 --- [ main] com.ysl.Application : Started Application in 5.148 seconds (JVM running for 5.974)
spring boot已經啟動,內嵌tomcat
容器,監聽為8080
端口,一個springboot程序就這么簡單的被創建了。
@SpringBootApplication 注解
SpringBootApplication注解的源碼如下:
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication { @AliasFor( annotation = EnableAutoConfiguration.class, attribute = "exclude" ) Class<?>[] exclude() default {}; @AliasFor( annotation = EnableAutoConfiguration.class, attribute = "excludeName" ) String[] excludeName() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackages" ) String[] scanBasePackages() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackageClasses" ) Class<?>[] scanBasePackageClasses() default {}; }
@Configuration
: 表示Application
作為sprig配置文件存在 @EnableAutoConfiguration
: 啟動spring boot內置的自動配置 @ComponentScan
: 掃描bean,路徑為Application
類所在package
以及package
下的子路徑,這里為 com.ysl
,在spring boot
中bean都放置在該路徑已經子路徑下。
構建REST工程
上面的操作連個HelloWorld都沒有出來,遠遠滿不足我們的需求。在com.ysl下創建子包controller,在該包下創建TestController.java,內容如下:
package com.ysl.controller; import org.springframework.web.bind.annotation.Mapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping("/") public String home(){ return "hello,springboot"; } }
重新啟動程序,訪問http:localhost:8080,得到結果:hello,springboot