First of all, Spring-boot is a service framework, more accurate is a microservices framework, in fact, "micro" is not "micro", Spring-boot contains a lot of embedded components, through these components can be done by our services,
In the past when we used spring, we needed to first add dependencies to the Pom file (similar to using gradle) and then create a new spring-related configuration file, usually XML, and then add Jetty\tomcat, And now we can do all these complicated things through spring-boot, here's how to use Spring-boot:
1) First of all, our pom.xml relies on:
<?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>com.example</groupId> <artifactid>sping_boot_test</ Artifactid> <version>0.0.1-snapshot</version> <!--Inherit defaults fromSpring Boot-<parent> <groupId>org.springframework.boot</groupId> <artifactid& Gt;spring-boot-starter-parent</artifactid> <version>1.4.0. Build-snapshot</version> </parent> <!--Add Typical dependencies forA Web application-<dependencies> <dependency> <groupid>org.springframework.b Oot</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--Package asAn executable jar--<build> <plugins> <plugin> <groupid>o Rg.springframework.boot</groupid> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <!--Add Spring repositories--<!--(You Don'T need this if you are using a. RELEASE version) --<repositories> <repository> <id>spring-snapshots</id> <url>http ://repo.spring.io/snapshot</url><snapshots><enabled>true</enabled></snapshots> </repository> <repository> <id>spring-milest Ones</id> <url>http://repo.spring.io/milestone</url></repository> </repositories> <pluginRepositories> <pluginRepository> < Id>spring-snapshots</id> <url>http://repo.spring.io/snapshot</url></pluginRepository> <pluginRepository> <id>spring-milestones</id> < ; Url>http://repo.spring.io/milestone</url></pluginRepository> </pluginRepositories></project>
View Code
2) Then you can write our code:
Startup class:
Package Example;import org.springframework.boot.SpringApplication;//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;//import Org.springframework.context.annotation.ComponentScan;//import org.springframework.context.annotation.Configuration;import org.springframework.boot.autoconfigure.SpringBootApplication;//@Configuration//@EnableAutoConfiguration//@ComponentScan@SpringBootApplication Public classApplication { Public Static voidMain (string[] args) {springapplication.run (application.class, args); }}
View Code
Controler class:
Package Example;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RestController; @RestController @requestmapping (Value="Users") Public classMyrestcontroller {@RequestMapping ("/") String Home () {return "Hello world!"; } @RequestMapping ("/test") String Test () {return "Hello test!"; }}
View Code
Package Example;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.RestController; @RestController Public classExample {@RequestMapping ("/") String Home () {return "Hello world!"; } @RequestMapping ("/test") String Test () {return "Hello test!"; } }
View Code
3) Start and run:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ |'_ | '_| |'_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/: : Spring Boot:: (v1.4.0. build-SNAPSHOT) .- .-Geneva xx: .:11.194INFO91890---[main] Example.Application:Starting application on ademacbook-pro.local with PID91890(/users/apple/documents/javademoworkspace/spring_boot_test/target/classes started by Appleinch/users/apple/documents/javademoworkspace/spring_boot_test) .- .-Geneva xx: .:11.199INFO91890---[main] Example.Application:No active profileSet, falling back todefaultProfilesdefault .- .-Geneva xx: .:11.312INFO91890---[main] ationconfigembeddedwebapplicationcontext:refreshing Org.springframework.boot[email PROTECTED]3B1DD 4fb:startup Date [Sat JuneGeneva xx: .: OneCst .]; Root of context Hierarchy .- .-Geneva xx: .:13.117INFO91890---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat initialized with Port (s):8080(HTTP) .- .-Geneva xx: .:13.140INFO91890---[main] o.apache.catalina.core.standardservice:starting service Tomcat .- .-Geneva xx: .:13.142INFO91890---[main] org.apache.catalina.core.StandardEngine:Starting Servlet engine:apache tomcat/8.0. - .- .-Geneva xx: .:13.316INFO91890---[ost-startstop-1] O.a.c.c.c.[tomcat]. [localhost]. [/]: Initializing Spring embedded Webapplicationcontext .- .-Geneva xx: .:13.316INFO91890---[ost-startstop-1] O.s.web.context.contextloader:root Webapplicationcontext:initialization completedinch -Ms .- .-Geneva xx: .:13.743INFO91890---[ost-startstop-1] o.s.b.w.servlet.servletregistrationbean:mapping servlet:'Dispatcherservlet'to [/] .- .-Geneva xx: .:13.748INFO91890---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping Filter:'Characterencodingfilter'To: [/*]2016-06-04 00:06:13.748 INFO 91890---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping filter: ' HID Denhttpmethodfilter ' to: [/*]2016-06-04 00:06:13.748 INFO 91890---[ost-startstop-1] O.s.b.w.servlet.filterregistrationbean:mapping filter: ' Httpputformcontentfilter ' to: [/*]2016-06-04 00:06:13.749 in FO 91890---[ost-startstop-1] o.s.b.w.servlet.filterregistrationbean:mapping filter: ' Requestcontextfilter ' to: [/*]2 016-06-04 00:06:14.000 INFO 91890---[main] s.w.s.m.m.a.requestmappinghandleradapter:looking for @Controlle Radvice:org.springframework.boot[email protected]3b1dd4fb:startup Date [Sat June 00:06:11 CST 2016]; Root of context hierarchy2016-06-04 00:06:14.155 INFO 91890---[main] s.w.s.m.m.a.requestmappinghandlermappin G:mapped "{[/]}" onto Java.lang.String Example.Example.home () 2016-06-04 00:06:14.157 INFO 91890---[main] s . w.s.m.m.a.requestmappinghandlermapping:mapped "{[/test]}" onto JAVA.LAng. String Example.Example.test () 2016-06-04 00:06:14.160 INFO 91890---[main] S.w.s.m.m.a.requestmappinghandlerma Pping:mapped "{[/users/]}" onto Java.lang.String Example.MyRestController.home () 2016-06-04 00:06:14.161 INFO 91890--- [main] s.w.s.m.m.a.requestmappinghandlermapping:mapped "{[/users/test]}" onto Java.lang.String example.myres Tcontroller.test () 2016-06-04 00:06:14.165 INFO 91890---[main] s.w.s.m.m.a.requestmappinghandlermapping:map Ped "{[/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-06-04 00:06:14.165 INFO 91890---[main] s.w.s.m.m.a.requestmapping Handlermapping: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-06-04 00:06:14.299 INFO 91890---[main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/webjars/**] onto handler of type [Class org.springframework.web.servlet.resource.resourcehttprequesthandler]2016-06-04 00:06:14.299 INFO 91890---[ Main] o.s.w.s.handler.simpleurlhandlermapping:mapped URL path [/**] onto handler of type [class Org.springframewor k.web.servlet.resource.resourcehttprequesthandler]2016-06-04 00:06:14.387 INFO 91890---[main] o.s.w.s.handle r.simpleurlhandlermapping:mapped URL Path [/**/Favicon.ico] onto handler of type [classOrg.springframework.web.servlet.resource.ResourceHttpRequestHandler] .- .-Geneva xx: .:14.969INFO91890---[main] o.s.j.e.a.annotationmbeanexporter:registering beans forJMX exposure on startup .- .-Geneva xx: .:15.070INFO91890---[main] s.b.c.e.t.tomcatembeddedservletcontainer:tomcat started on port (s):8080(HTTP) .- .-Geneva xx: .:15.077INFO91890---[main] Example.Application:Started applicationinch 4.612Seconds (JVM running for 5.156)
To access the app:
Http://127.0.0.1:8080/users/test
Spring Boot uses