I think that now Enterprise Java Web projects should be more or less used in the spring framework.
Looking back on our previous use of the spring framework, we need to first add dependency dependencies (and basically the same as using gradle) to the Pom file (if you use Maven) and then create a new spring-related XML file, often with fewer XML files. Then continue to use Tomcat or jetty as a container to run the project. Basically creating a new project every time is such a process, and we sometimes just want to quickly create a spring Web project to test something, or to save time.
Now we can do this quickly with spring boot.
Let's start by looking at a very simple example of using spring boot:
1. We create a MAVEN project, assuming the project name is Spring-boot, then we add dependencies in the Pom.xml file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-web</artifactid> <version>1.0.2.RELEASE</version> </ Dependency>
2. Create a new controller to accept the processing of our request:
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);} }
I believe you have seen this controller has a main method, do not worry, we directly run this main method:
. ____ _ _ _ _/\\/___ _ _ _ _ (_) _ __ _ \ \ \ (() \___ | ' _ | ' _| | ' _ \ _ ' | \ \ \ \\/ ___)| |_)| | | | | || (_| | ))) ' |____|. __|_| |_|_| |_\__, | ////=========|_|==============|___/=/_/_/_/:: Spring Boot:: (v1.0.2.release) 2014-04-26 22:54:40.985 INFO 723 6---[main] c.r.spring.boot.simplecontroller:starting Simplecontroller on Rollen with PID 7236 (d:\w Orkspace\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[email protected]50de0926:startup Date [Sat Apr 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: ' Hidd Enhttpmethodfilter ' to: [/*]2014-04-26 22:54:42.289 INFO 7236---[main] O.s.w.s.handler.simpleurlhandlermappi ng: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.requestmappinghandlermappi Ng: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---[Mai N] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/webjars/**] onto handler of type [class Org.springframewor k.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.Reso urcehttprequesthandler]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.tomcatemb Eddedservletcontainer: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-ex EC-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
will produce the above output, look at the log can be found by default Tomcat, port binding at 8080, now let us visit: Http://localhost:8080/hello
You can see the words that are printed in our code: Hello World.
Looking back on the process, is it a lot faster than before?
Resources:
1. Spring Boot Reference Guide
Use spring boot to accelerate the development of Java Web projects