博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot 初试,springboot入门,springboot helloworld例子
阅读量:2381 次
发布时间:2019-05-10

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

原文地址为:

因为项目中使用了 ,之前没接触过,所以写个玩玩看,当做的一个例子。搜索 spring boot。得到官方地址:http://projects.spring.io/spring-boot/

本文脉络:

1.一句话介绍 spring boot是干啥的。 

2.为啥要用spring boot.

3.用一个helloworld 打开springboot。

 

 

  • springboot是干啥的,可以吃吗。

一句话:做过Javaweb开发的程序员,肯定都知道spring框架。springboot就是一个能够快速搭起spring项目的神器。

  • 为啥要用spring boot

  在没有springboot之前,假如我们需要使用spring来搭建一个项目,比如说搭建一个spring mvc的项目。我们需要干啥:

假如说我们是 那么步骤是这样的:

 1.新建一个maven 项目,package 方式为war.

 2.加入相关依赖,什么spring-bean,spring-context,spring-webmvc等等,在web.xml中配置spring listener,配置spring.xml之类的。

 3.在2完成之后,依赖都配置好了,编译通过了,就可以运行项目了。

假如我们没有使用maven,就是单纯的建个web项目,然后到处copy jar包到webapp lib 下面,将会更麻烦,各种容易漏jar包。

所以,上面的方式,对于新手而言,可能一个小时也跑不起一个项目。而有了springboot之后可能只需要十几分钟。

 

  • 一个demo打开springboot。

 

1.新建maven项目,package方式为war. (Maven的介绍,安装与使用见 )

   使用maven的时候,maven会从仓库中下载依赖的jar包,国外仓库可能会很慢,所以最好有个私服。一般公司都会有私服,。

2.在pom文件spring boot需要的依赖,配置文件如下:

4.0.0
com.example
springbootdemo
0.0.1-SNAPSHOT
war
org.springframework.boot
spring-boot-starter-parent
1.3.4.RELEASE
org.springframework.boot
spring-boot-starter-web

 看这个pom文件,dependencies 节点下面只配置了一个 spring-boot-starter-web 依赖,加了一个parent配置,实在是太简洁了。

 

3.编写测试action,这个action跟你用其他方式创建spring项目里边的action个没啥区别,@controller 注解action类 ,@Requestmapping 映射请求地址。

  唯一不同的就是多了一个Main方法和EnableAutoConfiguration注解,这个main方法使用来运行这个demo的。

package com.example; 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.ResponseBody; @Controller @EnableAutoConfiguration public class HelloController {
@RequestMapping("/hello") @ResponseBody String home() {
return "Hello ,spring boot!"; } public static void main(String[] args) throws Exception {
SpringApplication.run(HelloController.class, args); //运行之后在浏览器中访问:http://localhost:8080/hello } }

4.启动项目,只需要运行上面代码的main方法,运行成功,控制台输出如下:

.   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \  \\/  ___)| |_)| | | | | || (_| |  ) ) ) ) '  |____| .__|_| |_|_| |_\__, | / / / /  =========|_|==============|___/=/_/_/_/  :: Spring Boot ::        (v1.3.4.RELEASE) 2016-05-10 15:21:46.299  INFO 6668 --- [           main] com.example.HelloController              : Starting HelloController on 0KE99GYM83Y5KGX with PID 6668 (D:\newspace\springbootdemo\target\classes started by Administrator in D:\newspace\springbootdemo) 2016-05-10 15:21:46.301  INFO 6668 --- [           main] com.example.HelloController              : No active profile set, falling back to default profiles: default 2016-05-10 15:21:46.337  INFO 6668 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@18b8315: startup date [Tue May 10 15:21:46 CST 2016]; root of context hierarchy 2016-05-10 15:21:47.385  INFO 6668 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2016-05-10 15:21:47.399  INFO 6668 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat 2016-05-10 15:21:47.400  INFO 6668 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.0.33 2016-05-10 15:21:47.482  INFO 6668 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext 2016-05-10 15:21:47.482  INFO 6668 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1148 ms 2016-05-10 15:21:47.714  INFO 6668 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/] 2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*] 2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2016-05-10 15:21:47.718  INFO 6668 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*] 2016-05-10 15:21:47.939  INFO 6668 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@18b8315: startup date [Tue May 10 15:21:46 CST 2016]; root of context hierarchy 2016-05-10 15:21:47.991  INFO 6668 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto java.lang.String com.example.HelloController.home() 2016-05-10 15:21:47.993  INFO 6668 --- [           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-05-10 15:21:47.994 INFO 6668 --- [ 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-05-10 15:21:48.014 INFO 6668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-05-10 15:21:48.014 INFO 6668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-05-10 15:21:48.048 INFO 6668 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2016-05-10 15:21:48.128 INFO 6668 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2016-05-10 15:21:48.187 INFO 6668 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2016-05-10 15:21:48.190 INFO 6668 --- [ main] com.example.HelloController : Started HelloController in 2.166 seconds (JVM running for 2.401) 2016-05-10 15:22:03.171 INFO 6668 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet' 2016-05-10 15:22:03.171 INFO 6668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started 2016-05-10 15:22:03.181 INFO 6668 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 10 ms

这是啥,这是应用启动了啊。我们之前在没有使用springboot的时候,就是一般的spring项目启动应用需要干啥。之前是deploy到tomcat里边,然后启动tomcat。

这里我啥都没干,连web.xml都没写,直接运行这个Main方法就可以了。

5.在浏览器中访问,如下:

 

可以看到,这种方式创建一个spring 的web项目比较方便。如果是传统的spring项目,需要添加各种依赖,然后发布到tomcat运行看效果。

这里只需要很少的依赖引用配置,运行一个方法便可以直接在浏览器访问。正如springboot官网上说的 Just run。

 

 

 

本例代码地址:http://git.oschina.net/dimixu/springbootdemo

转载请注明本文地址:
你可能感兴趣的文章
使用Duplicate target database命令恢复线上oracle datagard备库
查看>>
源码编译安装MySQL5.6.12详细过程
查看>>
Emoji表情符号录入MySQL数据库报错的解决方案
查看>>
Linux系统CentOS6.2版本下安装JDK7详细过程
查看>>
Android Studio之Activity切换动画(三)
查看>>
我是怎样和Linux系统结缘并通过红帽RHCE认证的
查看>>
DIYer最担心的事来了!CPU降价彻底无望
查看>>
WannaCry勒索软件还在继续传播和感染中
查看>>
为发展中国家儿童提供的OLPC OS 13.2.10 发布
查看>>
帅的代价!无框车门冻死:特斯拉一招解决
查看>>
美银美林提高Intel科技股的股票评级
查看>>
专家预测2019年的网络安全形势
查看>>
简单聊聊Linux学习经历
查看>>
欧盟即将在免费开源软件项目中推行“漏洞赏金”
查看>>
苹果股价下跌会迎来iPhone最黑暗时刻吗?
查看>>
智能校服受到多数学生追捧
查看>>
这么多CPU/显卡成就是AMD首创:大写的YES
查看>>
java实现解压缩(Unzip)功能的实现
查看>>
java操作Access *.mdb数据库的实现
查看>>
jdbc连接数据库的代码片段
查看>>