使用Spring Boot来加速Java web项目的开发
我想,現(xiàn)在企業(yè)級(jí)的Java web項(xiàng)目應(yīng)該或多或少都會(huì)使用到Spring框架的。
?
回首我們以前使用Spring框架的時(shí)候,我們需要首先在(如果你使用Maven的話)pom文件中增加對(duì)相關(guān)的的依賴(lài)(使用gradle來(lái)構(gòu)建的話基本也一樣)然后新建Spring相關(guān)的xml文件,而且往往那些xml文件還不會(huì)少。然后繼續(xù)使用tomcat或者jetty作為容器來(lái)運(yùn)行這個(gè)工程。基本上每次創(chuàng)建一個(gè)新的項(xiàng)目都是這么一個(gè)流程,而我們有時(shí)候僅僅想快速的創(chuàng)建一個(gè)Spring web工程來(lái)測(cè)試一些東西,或者是希望能節(jié)省時(shí)間。
?
現(xiàn)在我們使用Spring Boot就可以快速的做到這些了。
?
我們先來(lái)看一個(gè)非常簡(jiǎn)單的使用Spring boot的例子吧:
?
1. 我們創(chuàng)建一個(gè)Maven工程,假定工程名字為spring-boot,然后我們?cè)趐om.xml文件中加入依賴(lài):
| 12345 | <dependency>????<groupId>org.springframework.boot</groupId>????<artifactId>spring-boot-starter-web</artifactId>????<version>1.0.2.RELEASE</version></dependency> |
2. 新建一個(gè)Controller來(lái)接受處理我們的請(qǐng)求:
| 123456789101112131415161718192021222324 | import?org.springframework.boot.SpringApplication;import?org.springframework.boot.autoconfigure.EnableAutoConfiguration;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;/**?* Created by wenchao.ren on 2014/4/26.?*/@Controller@EnableAutoConfigurationpublic?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);????}} |
相信大家已經(jīng)看到了這個(gè)Controller有一個(gè)main方法,不要急,我們直接運(yùn)行這個(gè)main方法:
| 123456789101112131415161718192021222324252627 | ??.?? ____????????? _??????????? __ _ _?/\\ / ___'_ __ _ _(_)_ __? __ _ \ \ \ \( ( )\___ |?'_ | '_| | '_ \/ _` | \ \ \ \?\\/? ___)| |_)| | | | | || (_| |? ) ) ) )??'? |____| .__|_| |_|_| |_\__, | / / / /?=========|_|==============|___/=/_/_/_/?:: 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 hierarchy2014-04-26?22:54:41.583??INFO?7236?--- [?????????? main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port:?80802014-04-26?22:54:41.706??INFO?7236?--- [?????????? main] o.apache.catalina.core.StandardService?? : Starting service Tomcat2014-04-26?22:54:41.706??INFO?7236?--- [?????????? main] org.apache.catalina.core.StandardEngine? : Starting Servlet Engine: Apache Tomcat/7.0.522014-04-26?22:54:41.785??INFO?7236?--- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]?????? : Initializing Spring embedded WebApplicationContext2014-04-26?22:54:41.785??INFO?7236?--- [ost-startStop-1] o.s.web.context.ContextLoader??????????? : Root WebApplicationContext: initialization completed in?779?ms2014-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 startup2014-04-26?22:54:42.459??INFO?7236?--- [?????????? main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s):?8080/http2014-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 started2014-04-26?22:54:54.971??INFO?7236?--- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet??????? : FrameworkServlet?'dispatcherServlet': initialization completed in?8?ms |
? ? 會(huì)產(chǎn)生上面的輸出,查看日志可以發(fā)現(xiàn)默認(rèn)使用的是tomcat,端口綁定在8080,現(xiàn)在讓我們來(lái)訪問(wèn):http://localhost:8080/hello
? ? 就可以看到我們代碼中輸出的字樣:hello world了。
? ? 回首這個(gè)過(guò)程,是不是相比于以前快速了許多呢
總結(jié)
以上是生活随笔為你收集整理的使用Spring Boot来加速Java web项目的开发的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Jeewx捷微 , 免费微信公众账号管家
- 下一篇: hdu 4193(单调队列)