博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot 初探[1]--快速搭建Spring Boot项目
阅读量:6676 次
发布时间:2019-06-25

本文共 9136 字,大约阅读时间需要 30 分钟。

写在前面:

为什么使用Spring Boot?

  1.现在的项目使用tomcat+springMVC进行构建.每次搭建一个新的环境都需要重新配置tomcat等等一堆东西.水平扩展性比较差.

  2.Spring Boot 可以简单的一个java -jar就能运行.后续结合容器技术部署更为方便.开发,运行,调试也更为方便.

0.环境说明:

  Java 1.7 + Eclipse 4.3 + maven 3.3.3 + Spring Boot1.3.1.RELEASE

1.构建maven项目

eclipse:

  file -> new -> project -> maven project -> quick start

  填写各种名称

  配置jdk版本为1.7

2.加入Spring Boot相关配置

  

org.springframework.boot
spring-boot-starter-parent
1.3.1.RELEASE
org.springframework.boot
spring-boot-starter-web

3.编写Boot类

import java.util.Arrays;import org.springframework.context.ApplicationContext;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * boot class *  * @author BeeNoisy
*/@SpringBootApplicationpublic class Boot { public static void main( String[] args) { ApplicationContext ctx = SpringApplication.run(Boot.class, args); System.out.println("Let's inspect the beans provided by Spring Boot:"); }}

4.编写Controller类

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;/** * boot *  * @author BeeNoisy
*/@Controller@EnableAutoConfigurationpublic class HelloController { @RequestMapping("/") @ResponseBody public String hello() { return "Hello spring boot"; }}
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;/** * rest controller *  * @author ziyi.wang
*/@Controller@EnableAutoConfiguration@RequestMapping("rest")public class RestController { @RequestMapping(method = RequestMethod.GET) @ResponseBody public String get() { return "RestController.get()"; } @RequestMapping(method = RequestMethod.POST) public String post() { return "RestController.post()"; }}

5.运行

直接运行Boot类:

.   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v1.3.1.RELEASE)2016-01-14 16:20:04.721  INFO 21956 --- [           main] com.didichuxing.didi.cx.app.boot.Boot    : Starting Boot on Keen with PID 21956 (C:\dev\workspace\didi-cx-app-spring-boot\target\classes started by BeeNoisy in C:\dev\workspace\didi-cx-app-spring-boot)2016-01-14 16:20:04.723  INFO 21956 --- [           main] com.didichuxing.didi.cx.app.boot.Boot    : No active profile set, falling back to default profiles: default2016-01-14 16:20:04.764  INFO 21956 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@78fd4889: startup date [Thu Jan 14 16:20:04 CST 2016]; root of context hierarchy2016-01-14 16:20:05.750  INFO 21956 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]2016-01-14 16:20:06.290  INFO 21956 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2016-01-14 16:20:06.299  INFO 21956 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat2016-01-14 16:20:06.300  INFO 21956 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.302016-01-14 16:20:06.372  INFO 21956 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2016-01-14 16:20:06.372  INFO 21956 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1610 ms2016-01-14 16:20:06.652  INFO 21956 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]2016-01-14 16:20:06.656  INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]2016-01-14 16:20:06.657  INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2016-01-14 16:20:06.657  INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]2016-01-14 16:20:06.657  INFO 21956 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]2016-01-14 16:20:06.803  INFO 21956 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@78fd4889: startup date [Thu Jan 14 16:20:04 CST 2016]; root of context hierarchy2016-01-14 16:20:06.871  INFO 21956 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.didichuxing.didi.cx.app.boot.controller.HelloController.hello()2016-01-14 16:20:06.875  INFO 21956 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/rest],methods=[GET]}" onto public java.lang.String com.didichuxing.didi.cx.app.boot.controller.RestController.get()2016-01-14 16:20:06.875  INFO 21956 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/rest],methods=[POST]}" onto public java.lang.String com.didichuxing.didi.cx.app.boot.controller.RestController.post()2016-01-14 16:20:06.877  INFO 21956 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity
> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)2016-01-14 16:20:06.878 INFO 21956 --- [ 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-01-14 16:20:06.907 INFO 21956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-01-14 16:20:06.907 INFO 21956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-01-14 16:20:06.946 INFO 21956 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-01-14 16:20:07.067 INFO 21956 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2016-01-14 16:20:07.144 INFO 21956 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2016-01-14 16:20:07.148 INFO 21956 --- [ main] com.didichuxing.didi.cx.app.boot.Boot : Started Boot in 2.681 seconds (JVM running for 2.896)Let's inspect the beans provided by Spring Boot:

服务器启动完毕,看倒数第二行:Tomcat started on port(s): 8080 (http)

然后测试即可.

curl http://127.0.0.1:8080/ Hello spring boot curl http://127.0.0.1:8080/restRestController.get()

 至此,SpringBoot的Demo工程搭建完毕.

参考:

http://projects.spring.io/spring-boot/#quick-start

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

转载于:https://www.cnblogs.com/beenoisy/p/5130632.html

你可能感兴趣的文章
输出 pdf
查看>>
PHPCMS一个BUG
查看>>
APP云测试
查看>>
3-unit3 高速缓存DNS
查看>>
spark mllib 协同过滤算法,基于余弦相似度的用户相似度计算
查看>>
openwrt 基于qmi的 3G|4G拨号
查看>>
俞敏洪励志语
查看>>
开源|基于TensorFlow的聊天机器人-ErGo
查看>>
lucene4.0入门1
查看>>
Svn结合hook实现自动更新及多Project管理更新
查看>>
第三十六讲:tapestry表单组件详解之PasswordField
查看>>
Ubuntu11.10下安装JDK+Eclipse+Maven
查看>>
sgu 222
查看>>
让spring-data-jpa解放你的DAO
查看>>
58沈剑:架构师的平凡之路
查看>>
Hibernate问题-read-write缓存策略
查看>>
C/C++语言中“:”的使用方法
查看>>
sql中实现汉字的拼音首字母查询
查看>>
Android 动态布局 (代码布局)
查看>>
MYSQL备份和恢复
查看>>