博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot学习(四)-->SpringBoot快速入门,开山篇
阅读量:5129 次
发布时间:2019-06-13

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

Spring Boot简介

Spring Boot的目的在于创建和启动新的基于Spring框架的项目。Spring Boot会选择最适合的Spring子项目和第三方开源库进行整合。大部分Spring Boot应用只需要非常少的配置就可以快速运行起来

Spring Boot是伴随着Spring4.0诞生的,旨在简化开发。

Spring Boot提供了一种快速使用Spring的方式

SpringBoot官方文档:

>>Spring Boot特点

1:为基于Spring的开发提供更快的入门体验2:创建可以独立运行的Spring应用3:直接嵌入Tomcat或Jetty服务器,不需要打包成WAR文件4:提供推荐的基础POM文件(starter)来简化Apache Maven配置5:尽可能的根据项目依赖来自动配置Spring框架 6:Spring Boot使编码、配置、部署、监控变简单7:提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查8:开箱即用,没有代码生成,也无需XML配置。同时也可以修改默认值来满足特定的需求9:其他大量的项目都是基于Spring Boot之上的,如Spring Cloud

>>Spring Boot缺点

1:依赖太多,随便的一个Spring Boot应用都有好几十M2:缺少服务的注册和发现等解决方案3:缺少监控集成方案、安全管理方案4:中文的文档和资料太少且不够深入

>>Spring Boot应用场景

1:Spring能够应用的场景2:java web应用3:微服务

 

写个示例:Hello SpringBoot

1、创建Maven工程

工程结构如下:

2、配置pom.xml文件

4.0.0
com.mmzs
springBoot00
war
0.0.1-SNAPSHOT
springBoot00 Maven Webapp
http://maven.apache.org
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
1.7
org.springframework.boot
spring-boot-starter-web
spring_boot
org.springframework.boot
spring-boot-maven-plugin

3、编写代码

1 package com.mmzs.springboot; 2   3 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestParam; 6 import org.springframework.web.bind.annotation.ResponseBody; 7 import org.springframework.web.bind.annotation.RestController; 8 import java.util.ArrayList; 9 import java.util.HashMap;10 import java.util.List;11 import java.util.Map;12  13 /**14  * Created by mmzs 2018年4月2日 11:50:5715  * springboot注解详解:http://www.cnblogs.com/mmzs/p/8874349.html16  */17 //用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集,18 //这样子获取的数据返回前台时也会自动转发为json格式。 19 @RestController20 //Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。21 @EnableAutoConfiguration22 public class HelloController {23  24     @RequestMapping("/hello")25     @ResponseBody //会使用详细转换器输出结果26     public String hello() {27         return "Hello Spring-Boot";28     }29  30     @RequestMapping("/info")31     public Map
getInfo(@RequestParam String name) {32 Map
map = new HashMap<>();33 map.put("name", name);34 return map;35 }36 37 @RequestMapping("/list")38 public List
> getList() {39 List
> list = new ArrayList<>();40 Map
map = null;41 for (int i = 1; i <= 5; i++) {42 map = new HashMap<>();43 map.put("name", "mmzs+" + i);44 list.add(map);45 }46 return list;47 }48 }49
HelloController
1 package com.mmzs.springboot; 2  3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5  6 /** 7  * Created by mmzs 2018年4月2日 11:48:38 8  * springboot注解详解:http://www.cnblogs.com/mmzs/p/8874349.html 9  */10 //Spring Boot项目的核心注解,主要目的是开启自动配置。11 //包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。12 //其中@ComponentScan默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。13 @SpringBootApplication14 public class Application {15     public static void main(String[] args) {16         SpringApplication.run(Application.class, args);17     }18 }19
Application启动类

4、测试效果

运行Application类,结果如下:

.   ____          _            __ _ _ /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/  ___)| |_)| | | | | || (_| |  ) ) ) )  '  |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot ::        (v1.5.2.RELEASE)2018-07-02 12:17:03.434  INFO 5988 --- [           main] com.mmzs.springboot.Application          : Starting Application on CVHOPASEHFXPHFV with PID 5988 (D:\Project\springBoot00\target\classes started by Administrator in D:\Project\springBoot00)2018-07-02 12:17:03.442  INFO 5988 --- [           main] com.mmzs.springboot.Application          : No active profile set, falling back to default profiles: default2018-07-02 12:17:03.562  INFO 5988 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27fe3806: startup date [Mon Jul 02 12:17:03 CST 2018]; root of context hierarchy2018-07-02 12:17:06.724  INFO 5988 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2018-07-02 12:17:06.751  INFO 5988 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat2018-07-02 12:17:06.753  INFO 5988 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.112018-07-02 12:17:06.971  INFO 5988 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2018-07-02 12:17:06.972  INFO 5988 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3415 ms2018-07-02 12:17:07.260  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]2018-07-02 12:17:07.266  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]2018-07-02 12:17:07.267  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2018-07-02 12:17:07.267  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]2018-07-02 12:17:07.267  INFO 5988 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]2018-07-02 12:17:07.788  INFO 5988 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@27fe3806: startup date [Mon Jul 02 12:17:03 CST 2018]; root of context hierarchy2018-07-02 12:17:07.928  INFO 5988 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/list]}" onto public java.util.List
> com.mmzs.springboot.HelloController.getList()2018-07-02 12:17:07.929 INFO 5988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.mmzs.springboot.HelloController.hello()2018-07-02 12:17:07.931 INFO 5988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/info]}" onto public java.util.Map
com.mmzs.springboot.HelloController.getInfo(java.lang.String)2018-07-02 12:17:07.936 INFO 5988 --- [ 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)2018-07-02 12:17:07.937 INFO 5988 --- [ 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)2018-07-02 12:17:08.012 INFO 5988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-07-02 12:17:08.013 INFO 5988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-07-02 12:17:08.103 INFO 5988 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2018-07-02 12:17:08.403 INFO 5988 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-07-02 12:17:08.531 INFO 5988 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2018-07-02 12:17:08.538 INFO 5988 --- [ main] com.mmzs.springboot.Application : Started Application in 5.736 seconds (JVM running for 6.592)
启动成功界面

访问界面:

 

转载于:https://www.cnblogs.com/mmzs/p/9253276.html

你可能感兴趣的文章
DrawerLayout实现双向侧滑
查看>>
MySQL入门很简单-触发器
查看>>
LVM快照(snapshot)备份
查看>>
绝望的第四周作业
查看>>
一月流水账
查看>>
数论四大定理
查看>>
npm 常用指令
查看>>
C#基础知识面试经典[整理]
查看>>
20几个正则常用正则表达式
查看>>
TextArea中定位光标位置
查看>>
非常棒的Visual Studo调试插件:OzCode 2.0 下载地址
查看>>
判断字符串在字符串中
查看>>
hdu4374One hundred layer (DP+单调队列)
查看>>
类间关系总结
查看>>
properties配置文件读写,追加
查看>>
Linux环境下MySql安装和常见问题的解决
查看>>
lrzsz——一款好用的文件互传工具
查看>>
ZPL语言完成条形码的打印
查看>>
这20件事千万不要对自己做!
查看>>
Linux环境下Redis安装和常见问题的解决
查看>>