一.Spring Boot简介
官网地址:http://spring.io/projects/spring-boot
Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用程序。我们对Spring平台和第三方库进行了一种自以为是的观点,这样您就可以轻松上手了。大多数Spring Boot应用程序只需要很少的Spring配置。
您可以使用Spring Boot创建可以使用java -jar
或更传统的war部署启动的Java应用程序 。我们还提供了一个运行“spring脚本”的命令行工具。
我们的主要目标是:
- 为所有Spring开发提供从根本上更快且可广泛访问的入门体验。
- 开箱即用,但随着需求开始偏离默认值而迅速摆脱困境。
- 提供大型项目(例如嵌入式服务器,安全性,度量标准,运行状况检查和外部化配置)通用的一系列非功能性功能。
- 绝对没有代码生成,也不需要XML配置。
背景:
J2EE笨重的开发、繁多的配置、低下的开发效率、复杂的部署流程、第三方技术集成难度大。
优点:
快速创建独立运行的Spring项目以及与主流框架集成
使用嵌入式的Servlet容器,应用无需打成WAR包
starters自动依赖与版本控制
大量的自动配置,简化开发,也可修改默认值
无需配置XML,无代码生成,开箱即用
准生产环境的运行时应用监控
与云计算的天然集成
解决:
“Spring全家桶”时代。Spring Boot à J2EE一站式解决方案Spring Cloud à 分布式整体解决方。
单体应用
微服务
二.Spring Boot入门
1. 环境准备
jdk1.8
maven3.x
IntelliJ IDEA 2018
Spring Boot 2.0.5.RELEASE需要Java 8或9以及 Spring Framework 5.0.9.RELEASE或更高版本。
Spring Boot文档参考指南 :https://docs.spring.io/spring-boot/docs/2.0.5.RELEASE/reference/htmlsingle/#getting-started-system-requirements-servlet-containers
2. 创建maven项目
编辑pom.xml文件,引入相关依赖
"1.0" encoding="UTF-8"?>"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"> 4.0.0 com.xyg spring-boot 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE org.springframework.boot spring-boot-starter-web
org.springframework.boot spring-boot-maven-plugin
3. 创建主程序引导类
package com.xyg; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Author: Mr.Deng * Date: 2018/9/14 * Desc: @SpringBootConfiguration来标注一个主程序类,说明这是一个springboot应用 */ @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { //springboot应用启动起来,run方法里第一个是应用主程序类名,第二个是应用参数 SpringApplication.run(HelloWorldApplication.class,args); } }
编写Controller层Service
package com.xyg.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * Author: Mr.Deng * Date: 2018/9/14 * Desc: 编写Controller层,发送hello请求 */ @Controller public class HelloController { @ResponseBody @RequestMapping("/hello") public String Hello(){ return "Hello World!"; } }
4. 启动程序测试
执行主程序里的main方法,控制台输出如下信息
D:\Develop\Java\jdk1.8\bin\java "-javaagent:D:\IDEA\IntelliJ IDEA 2018.1\lib\idea_rt.jar=62470:D:\IDEA\IntelliJ IDEA 2018.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Develop\Java\jdk1.8\jre\lib\charsets.jar;D:\Develop\Java\jdk1.8\jre\lib\deploy.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\cldrdata.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\dnsns.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\jaccess.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\jfxrt.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\localedata.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\nashorn.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\sunec.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\Develop\Java\jdk1.8\jre\lib\ext\zipfs.jar;D:\Develop\Java\jdk1.8\jre\lib\javaws.jar;D:\Develop\Java\jdk1.8\jre\lib\jce.jar;D:\Develop\Java\jdk1.8\jre\lib\jfr.jar;D:\Develop\Java\jdk1.8\jre\lib\jfxswt.jar;D:\Develop\Java\jdk1.8\jre\lib\jsse.jar;D:\Develop\Java\jdk1.8\jre\lib\management-agent.jar;D:\Develop\Java\jdk1.8\jre\lib\plugin.jar;D:\Develop\Java\jdk1.8\jre\lib\resources.jar;D:\Develop\Java\jdk1.8\jre\lib\rt.jar;D:\IDEA\WorkSpace\spring-boot\target\classes;D:\Develop\maven\repository\org\springframework\boot\spring-boot-starter-web\2.0.5.RELEASE\spring-boot-starter-web-2.0.5.RELEASE.jar;D:\Develop\maven\repository\org\springframework\boot\spring-boot-starter\2.0.5.RELEASE\spring-boot-starter-2.0.5.RELEASE.jar;D:\Develop\maven\repository\org\springframework\boot\spring-boot\2.0.5.RELEASE\spring-boot-2.0.5.RELEASE.jar;D:\Develop\maven\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.5.RELEASE\spring-boot-autoconfigure-2.0.5.RELEASE.jar;D:\Develop\maven\repository\org\springframework\boot\spring-boot-starter-logging\2.0.5.RELEASE\spring-boot-starter-logging-2.0.5.RELEASE.jar;D:\Develop\maven\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Develop\maven\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Develop\maven\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\Develop\maven\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;D:\Develop\maven\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;D:\Develop\maven\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\Develop\maven\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Develop\maven\repository\org\springframework\spring-core\5.0.9.RELEASE\spring-core-5.0.9.RELEASE.jar;D:\Develop\maven\repository\org\springframework\spring-jcl\5.0.9.RELEASE\spring-jcl-5.0.9.RELEASE.jar;D:\Develop\maven\repository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;D:\Develop\maven\repository\org\springframework\boot\spring-boot-starter-json\2.0.5.RELEASE\spring-boot-starter-json-2.0.5.RELEASE.jar;D:\Develop\maven\repository\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;D:\Develop\maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Develop\maven\repository\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;D:\Develop\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;D:\Develop\maven\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;D:\Develop\maven\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;D:\Develop\maven\repository\org\springframework\boot\spring-boot-starter-tomcat\2.0.5.RELEASE\spring-boot-starter-tomcat-2.0.5.RELEASE.jar;D:\Develop\maven\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.34\tomcat-embed-core-8.5.34.jar;D:\Develop\maven\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.34\tomcat-embed-el-8.5.34.jar;D:\Develop\maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.34\tomcat-embed-websocket-8.5.34.jar;D:\Develop\maven\repository\org\hibernate\validator\hibernate-validator\6.0.12.Final\hibernate-validator-6.0.12.Final.jar;D:\Develop\maven\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Develop\maven\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\Develop\maven\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;D:\Develop\maven\repository\org\springframework\spring-web\5.0.9.RELEASE\spring-web-5.0.9.RELEASE.jar;D:\Develop\maven\repository\org\springframework\spring-beans\5.0.9.RELEASE\spring-beans-5.0.9.RELEASE.jar;D:\Develop\maven\repository\org\springframework\spring-webmvc\5.0.9.RELEASE\spring-webmvc-5.0.9.RELEASE.jar;D:\Develop\maven\repository\org\springframework\spring-aop\5.0.9.RELEASE\spring-aop-5.0.9.RELEASE.jar;D:\Develop\maven\repository\org\springframework\spring-context\5.0.9.RELEASE\spring-context-5.0.9.RELEASE.jar;D:\Develop\maven\repository\org\springframework\spring-expression\5.0.9.RELEASE\spring-expression-5.0.9.RELEASE.jar com.xyg.HelloWorldApplication . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.5.RELEASE) 2018-09-14 15:37:28.552 INFO 8120 --- [ main] com.xyg.HelloWorldApplication : Starting HelloWorldApplication on DTX with PID 8120 (D:\IDEA\WorkSpace\spring-boot\target\classes started by Administrator in D:\IDEA\WorkSpace\spring-boot) 2018-09-14 15:37:28.558 INFO 8120 --- [ main] com.xyg.HelloWorldApplication : No active profile set, falling back to default profiles: default 2018-09-14 15:37:28.953 INFO 8120 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@55040f2f: startup date [Fri Sep 14 15:37:28 CST 2018]; root of context hierarchy 2018-09-14 15:37:33.854 INFO 8120 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2018-09-14 15:37:34.001 INFO 8120 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2018-09-14 15:37:34.001 INFO 8120 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34 2018-09-14 15:37:34.024 INFO 8120 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\Develop\Java\jdk1.8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;D:\Develop\maven\apache-maven-3.5.0/bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\NetSarang;D:\Develop\Java\jdk1.8\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;D:\Develop\secureCRT\;D:\Develop\SVN\VisualSVN Server\bin;D:\Develop\SVN\TortoiseSVN\bin;D:\usr\hadoop-2.7.6\bin;D:\usr\hadoop-2.7.6\sbin;D:\Python tools\python2.7.9;D:\scalaTools\scala\bin;D:\usr\spark-1.6.0-bin-hadoop2.6\bin;D:\usr\spark-1.6.0-bin-hadoop2.6sbin;D:\Python tools\python3.6.5;D:\Python tools\python2.7.9;D:\Python tools\python2.7.9\Scripts;D:\Python tools\python3.6.5\Scripts;D:\Develop\Git\cmd;D:\Develop\Git\TortoiseGit\bin;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;.] 2018-09-14 15:37:34.291 INFO 8120 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2018-09-14 15:37:34.292 INFO 8120 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 5347 ms 2018-09-14 15:37:34.470 INFO 8120 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2018-09-14 15:37:34.479 INFO 8120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2018-09-14 15:37:34.480 INFO 8120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2018-09-14 15:37:34.480 INFO 8120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2018-09-14 15:37:34.480 INFO 8120 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2018-09-14 15:37:34.840 INFO 8120 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-14 15:37:35.146 INFO 8120 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@55040f2f: startup date [Fri Sep 14 15:37:28 CST 2018]; root of context hierarchy 2018-09-14 15:37:35.373 INFO 8120 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.xyg.controller.HelloController.Hello() 2018-09-14 15:37:35.382 INFO 8120 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest) 2018-09-14 15:37:35.385 INFO 8120 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) 2018-09-14 15:37:35.451 INFO 8120 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-14 15:37:35.451 INFO 8120 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2018-09-14 15:37:35.901 INFO 8120 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2018-09-14 15:37:35.992 INFO 8120 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2018-09-14 15:37:36.005 INFO 8120 --- [ main] com.xyg.HelloWorldApplication : Started HelloWorldApplication in 8.445 seconds (JVM running for 9.814)
通过查看日志信息发现tomcat:8080 启动,页面输入: http://localhost:8080/hello ,
5. 部署服务
package打包jar在target目录,拷贝到左面,然后进入jar包所在目录,执行命令 java -jar xxx.jar ,页面访问
6. POM文件解析
1)spring boot 版本仲裁中心
它的父项目依赖 org.springframework.boot spring-boot-starter-parent 2.0.5.RELEASE 他来真正管理spring boot应用里面所有的依赖版本,这里面定义了很多服务版本,当里面定义了服务版本时,我们就不需要再导入相关依赖,没有定义时,需手工导入依赖。 org.springframework.boot spring-boot-dependencies 2.0.5.RELEASE ../../spring-boot-dependencies
1)spring boot 场景启动器,帮我们导入web模块正常运行所依赖的组件。
它的父模块来自 org.springframework.boot spring-boot-starter-web 它的父模块来自 org.springframework.boot spring-boot-starters 2.0.5.RELEASE 它的父模块来自 org.springframework.boot spring-boot-parent 2.0.5.RELEASE ../spring-boot-parent org.springframework.boot spring-boot-dependencies 2.0.5.RELEASE ../spring-boot-dependencies
spring boot 将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里引入相关的starter,相关场景的所有依赖都会导入进来。
7.@SpringBootApplication
Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {
@SpringBootConfiguration:Spring Boot的配置类; 标注在某个类上,表示这是一个Spring Boot的配置类;
@Configuration:配置类上来标注这个注解;配置类 ----- 配置文件;配置类也是容器中的一个组件;@Component
@EnableAutoConfiguration:开启自动配置功能;以前我们需要配置的东西,SpringBoot帮我们自动配置;这个注解告诉SpringBoot开启自动配置功能;这样自动配置才能生效;
@AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自动配置包@Import(AutoConfigurationPackages.Registrar.class):
Spring的底层注解@Import,给容器中导入一个组件;导入的组件由AutoConfigurationPackages.Registrar.class;
将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有组件扫描到Spring容器;
@Import(EnableAutoConfigurationImportSelector.class); 给容器中导入组件?
EnableAutoConfigurationImportSelector:导入哪些组件的选择器;
将所有需要导入的组件以全类名的方式返回;这些组件就会被添加到容器中;
会给容器中导入非常多的自动配置类(xxxAutoConfiguration);就是给容器中导入这个场景需要的所有组件, 并配置好这些组件;
有了自动配置类,免去了我们手动编写配置注入功能组件等的工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
Spring Boot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,将这些值作为自动配置类导入到容器中,自动配置类就生效,帮我们进行自动配置工作;以前我们需要自己配置的东 西,自动配置类都帮我们;
J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-2.0.5.RELEASE.jar;
8.springboot向导创建
(前提条件-联网条件下)快速创建一个spring boot项目,new project
点击flish完成项目创建,把下面不用的几项删除掉
然后编写controller层代码测试(略)
三.配置文件
1.application.properties
resource里面有三个文件,application.properties 或者 application.yml ,两者的书写格式不一样,为SpringBoot的全局配置文件,配置文件名是固定的,
配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好;
2.static
静态资源都放在这个包下
3.templates
模板
四.日志框架
SpringBoot默认选用 SLF4j和logback;
org.springframework.boot spring‐boot‐starter‐logging
给类路径下放上每个日志框架自己的配置文件即可;SpringBoot就不使用他默认配置的了
Logging System | Customization |
---|---|
Logback |
|
Log4j2 |
|
JDK (Java Util Logging) |
|
logback.xml:直接就被日志框架识别了;logback-spring.xml:日志框架就不直接加载日志的配置项,由SpringBoot解析日志配置,可以使用SpringBoot的高级Profile功能。