咱們之前使用Spring框架的時候,須要首先在pom文件中增長對相關的的依賴,而後新建Spring相關的xml文件,並且每每那些xml文件還不會少。而後繼續使用tomcat或者jetty做爲容器來運行這個工程。基本上每次建立一個新的項目都是這麼一個流程,而咱們有時候僅僅想快速的建立一個Spring web工程來測試一些東西,或者是但願能節省時間。html
如今咱們使用Spring Boot就能夠快速的作到這些了。java
1. 咱們建立一個Maven工程,工程名字爲spring-boot-helloworld,而後修改pom.xml文件:web
<?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>net.mingyang</groupId> <artifactId>spring-boot-helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-helloworld</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. 新建一個Controller來接受處理咱們的請求:spring
package net.mingyang.spring_boot_helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @SpringBootApplication @Controller public class SimpleController { @RequestMapping(value ="/hello", method = RequestMethod.GET) @ResponseBody public String hello(){ return "hello world"; } public static void main(String[] args) { SpringApplication.run(SimpleController.class, args); } }
3. 執行SimleController中的main()方法:apache
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.4.1.RELEASE) 2016-10-18 08:43:46.946 INFO 4640 --- [ main] n.m.s.SimpleController : Starting SimpleController on D60601653 with PID 4640 (X:\dev\spring-test-suite\spring-boot-helloworld\target\classes started by lbin in X:\dev\spring-test-suite\spring-boot-helloworld) 2016-10-18 08:43:46.950 INFO 4640 --- [ main] n.m.s.SimpleController : No active profile set, falling back to default profiles: default 2016-10-18 08:43:47.238 INFO 4640 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69cc1627: startup date [Tue Oct 18 08:43:47 CST 2016]; root of context hierarchy 2016-10-18 08:43:49.969 INFO 4640 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2016-10-18 08:43:49.983 INFO 4640 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat 2016-10-18 08:43:49.984 INFO 4640 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.5 2016-10-18 08:43:50.108 INFO 4640 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2016-10-18 08:43:50.109 INFO 4640 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2874 ms 2016-10-18 08:43:50.334 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2016-10-18 08:43:50.340 INFO 4640 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2016-10-18 08:43:50.766 INFO 4640 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@69cc1627: startup date [Tue Oct 18 08:43:47 CST 2016]; root of context hierarchy 2016-10-18 08:43:50.863 INFO 4640 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello],methods=[GET]}" onto public java.lang.String net.mingyang.spring_boot_helloworld.SimpleController.hello() 2016-10-18 08:43:50.868 INFO 4640 --- [ 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) 2016-10-18 08:43:50.868 INFO 4640 --- [ 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) 2016-10-18 08:43:50.912 INFO 4640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-10-18 08:43:50.912 INFO 4640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-10-18 08:43:51.029 INFO 4640 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-10-18 08:43:51.767 INFO 4640 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2016-10-18 08:43:51.878 INFO 4640 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2016-10-18 08:43:51.884 INFO 4640 --- [ main] n.m.s.SimpleController : Started SimpleController in 5.658 seconds (JVM running for 5.99)
查看日誌能夠發現默認使用的是tomcat,端口綁定在8080,訪問:http://localhost:8080/hello,就能夠看到咱們代碼中輸出的字樣了。tomcat
是否是很簡單?app