spring boot--用于簡便快捷的搭建基于spring的獨立可運行的應用
創建一個Maven工程,在pom.xml文件中加入依賴:
<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>springboot</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>springboot</name> <description /> <!-- 父依賴 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.2.RELEASE</version> </parent> <dependencies> <dependency> <!-- 導入jar包 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
新建一個Controller來接受處理請求:
package com.i.springboot.controller; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class TestController { @RequestMapping(value="/test") String home(){ return "hello world"; } public static void main(String[] args) throws Exception { SpringApplication.run(TestController.class, args); } }
[代碼]這個Controller有一個main方法,直接運行這個main方法
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.0.2.RELEASE) 2014-04-26 22:54:40.985 INFO 7236 --- [ main] c.r.spring.boot.SimpleController : Starting SimpleController on rollen with PID 7236 (D:\workspace\GitHub\SpringDemo\spring-boot\target\classes started by wenchao.ren in D:\workspace\GitHub\SpringDemo\spring-boot) 2014-04-26 22:54:41.008 INFO 7236 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@50de0926: startup date [Sat Apr 26 22:54:41 CST 2014]; root of context hierarchy 2014-04-26 22:54:41.583 INFO 7236 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080 2014-04-26 22:54:41.706 INFO 7236 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2014-04-26 22:54:41.706 INFO 7236 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.52 2014-04-26 22:54:41.785 INFO 7236 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2014-04-26 22:54:41.785 INFO 7236 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 779 ms 2014-04-26 22:54:42.055 INFO 7236 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2014-04-26 22:54:42.057 INFO 7236 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2014-04-26 22:54:42.289 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-04-26 22:54:42.368 INFO 7236 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.rollenholt.spring.boot.SimpleController.hello() 2014-04-26 22:54:42.376 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-04-26 22:54:42.377 INFO 7236 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2014-04-26 22:54:42.447 INFO 7236 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2014-04-26 22:54:42.459 INFO 7236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http 2014-04-26 22:54:42.460 INFO 7236 --- [ main] c.r.spring.boot.SimpleController : Started SimpleController in 1.675 seconds (JVM running for 1.944) 2014-04-26 22:54:54.963 INFO 7236 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2014-04-26 22:54:54.963 INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2014-04-26 22:54:54.971 INFO 7236 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 8 ms
[代碼]直接訪問http://localhost:8080/test
瀏覽器輸出 hello world,是不是簡單方便
本文由用戶 hollistertop 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!