SpringBoot学习(四)-->SpringBoot快速入门,开山篇

时间:2024-04-01 09:05:38

Spring Boot简介

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

Spring Boot是伴随着Spring4.0诞生的,旨在简化开发。
Spring Boot提供了一种快速使用Spring的方式

SpringBoot官方文档:http://spring.io/projects/spring-boot

>>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应用都有好几十M
2:缺少服务的注册和发现等解决方案
3:缺少监控集成方案、安全管理方案
4:中文的文档和资料太少且不够深入

>>Spring Boot应用场景

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

写个示例:Hello SpringBoot

1、创建Maven工程

工程结构如下:

SpringBoot学习(四)-->SpringBoot快速入门,开山篇

2、配置pom.xml文件

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mmzs</groupId>
<artifactId>springBoot00</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>springBoot00 Maven Webapp</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<!-- 一定要有spring-boot-starter-parent,其中包含了spring的各种插件版本号 -->
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath /><!-- lookup parent from repository -->
</parent> <!-- 父类统一管理版本信息 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- springboot 默认java版本是1.6,这里显示给它指定为1.7 -->
<java.version>1.7</java.version>
</properties> <dependencies>
<dependency>
<!-- 导入spring boot的web支持,可以不写版本号,在spring-boot-starter-parent已经包含 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> <build>
<finalName>spring_boot</finalName>
<!-- 添加Spring boot的maven插件,可以不写版本号,在spring-boot-starter-parent已经包含 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> </build>
</project>

3、编写代码

 package com.mmzs.springboot;

 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* Created by mmzs 2018年4月2日 11:50:57
* springboot注解详解:http://www.cnblogs.com/mmzs/p/8874349.html
*/
//用于标注控制层组件(如struts中的action),@ResponseBody和@Controller的合集,
//这样子获取的数据返回前台时也会自动转发为json格式。
@RestController
//Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。
@EnableAutoConfiguration
public class HelloController { @RequestMapping("/hello")
@ResponseBody //会使用详细转换器输出结果
public String hello() {
return "Hello Spring-Boot";
} @RequestMapping("/info")
public Map<String, String> getInfo(@RequestParam String name) {
Map<String, String> map = new HashMap<>();
map.put("name", name);
return map;
} @RequestMapping("/list")
public List<Map<String, String>> getList() {
List<Map<String, String>> list = new ArrayList<>();
Map<String, String> map = null;
for (int i = 1; i <= 5; i++) {
map = new HashMap<>();
map.put("name", "mmzs+" + i);
list.add(map);
}
return list;
}
}

HelloController

 package com.mmzs.springboot;

 import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* Created by mmzs 2018年4月2日 11:48:38
* springboot注解详解:http://www.cnblogs.com/mmzs/p/8874349.html
*/
//Spring Boot项目的核心注解,主要目的是开启自动配置。
//包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。
//其中@ComponentScan默认扫描@SpringBootApplication所在类的同级目录以及它的子目录。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

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: default
2018-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 hierarchy
2018-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 Tomcat
2018-07-02 12:17:06.753 INFO 5988 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2018-07-02 12:17:06.971 INFO 5988 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-07-02 12:17:06.972 INFO 5988 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3415 ms
2018-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 hierarchy
2018-07-02 12:17:07.928 INFO 5988 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/list]}" onto public java.util.List<java.util.Map<java.lang.String, java.lang.String>> 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<java.lang.String, java.lang.String> 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<java.util.Map<java.lang.String, java.lang.Object>> 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 startup
2018-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)

启动成功界面

访问界面:

SpringBoot学习(四)-->SpringBoot快速入门,开山篇

SpringBoot学习(四)-->SpringBoot快速入门,开山篇

SpringBoot学习(四)-->SpringBoot快速入门,开山篇