Spring Boot 学习(一)
来源:互联网 发布:东北师大网络教育 编辑:程序博客网 时间:2024/05/17 02:53
根据Spring Boot中文参考手册及网上搜集的资料,系统的学习了一下Spring Boot, 总结如下, 希望对初学者有所帮助:
Spring Boot介绍
1.Spring Boot让程序只需"just run"。为Spring平台及第三方库提供开箱即用的设置
2.包含的特性:
- 创建可以独立运行的 Spring应用。
- 直接嵌入 Tomcat或Jetty服务器。
- 提供推荐的基础 POM文件来简化Apache Maven配置。
- 尽可能的根据项目依赖来自动配置 Spring框架。
- 提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。
- 没有代码生成,也没有 XML配置文件。
Springboot构建WEB项目实例
1.创建Maven工程
- 首先 使用eclipsej2ee创建一个maven工程
2.Start POMs
- 在pom.xml中引入spring-boot-start-parent,spring官方解释叫stater POMs
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.1.RELEASE</version> </parent>
spring-boot-starter-parent包含了以下信息:
- 使用java6编译级别
- 使用utf-8编码
- 实现了通用的测试框架 (JUnit, Hamcrest, Mockito).
- 智能资源过滤
- 智能的插件配置(exec plugin, surefire, Git commit ID, shade).
3.Spring Web 依赖
- spring-boot-starter-web包含了springwebmvc和tomcat等web开发的特性。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
4.启动类
- 申明让spring boot自动给我们配置spring需要的配置。
- 提倡基于Java的配置。可以使用一个XML源来调用SpringApplication.run(),通常建议使 用@Configuration类作为主要源。
<pre name="code" class="java">import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.context.annotation.Configuration;@Configuration@EnableAutoConfigurationpublic class Example { public static void main(String[] args) throws Exception { SpringApplication.run(Example.class, args); }}
- Run As Java Application,Spring boot项目启动结果
<pre name="code" class="plain"><span style="color:#404040;"> . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.2.1.RELEASE)2016-03-16 13:54:49.759 INFO 4788 --- [ main] Example : Starting Example on xyzhou-PC with PID 4788 (H:\demo\spring-boot\target\classes started by xyzhou in H:\demo\spring-boot)2016-03-16 13:54:49.786 INFO 4788 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5bfbf16f: startup date [Wed Mar 16 13:54:49 CST 2016]; root of context hierarchy2016-03-16 13:54:50.147 INFO 4788 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': 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-03-16 13:54:50.831 INFO 4788 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2016-03-16 13:54:51.028 INFO 4788 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat2016-03-16 13:54:51.030 INFO 4788 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.0.152016-03-16 13:54:51.122 INFO 4788 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2016-03-16 13:54:51.122 INFO 4788 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1339 ms2016-03-16 13:54:51.588 INFO 4788 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2016-03-16 13:54:51.594 INFO 4788 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2016-03-16 13:54:51.594 INFO 4788 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2016-03-16 13:54:51.766 INFO 4788 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5bfbf16f: startup date [Wed Mar 16 13:54:49 CST 2016]; root of context hierarchy2016-03-16 13:54:51.809 INFO 4788 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" 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)2016-03-16 13:54:51.809 INFO 4788 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)2016-03-16 13:54:51.834 INFO 4788 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-16 13:54:51.834 INFO 4788 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-16 13:54:51.870 INFO 4788 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-16 13:54:51.927 INFO 4788 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup</span><span style="color:#ff0000;">2016-03-16 13:54:51.996 INFO 4788 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)</span><span style="color:#404040;">2016-03-16 13:54:51.998 INFO 4788 --- [ main] Example : Started Example in 2.443 seconds (JVM running for 2.813)</span>
可以看到内嵌的Tomcat已启动, 具体Embeded Tomcat启动流程如下:
Embeded Tomcat的启动流程
- spring boot在启动时,先通过一个简单的查找Servlet类的方式来判断是不是在web环境:
- 如果是,则会创建AnnotationConfigEmbeddedWebApplicationContext,否则Springcontext就是AnnotationConfigApplicationContext
- 获取EmbeddedServletContainerFactory的实现类, returnedserver until EmbeddedServletContainer.start() iscalled (which happens when theApplicationContext hasbeen fully refreshed).
选择Spring Boot项目的内嵌容器
- SpringBoot工程的默认web容器是Tomcat,可修改,例如Jetty或者Undertow。
- 在pom文件中排除tomcat的starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
- 增加Jetty依赖
<span style="white-space:pre"></span><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> <!-- For External Provider --> <scope>provided</scope> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlets</artifactId> <version>${jetty.version}</version> </dependency>
- Run As Java Application, 可以看到Jetty已经启动
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.2.1.RELEASE)2016-03-16 14:02:57.698 INFO 5252 --- [ main] Example : Starting Example on xyzhou-PC with PID 5252 (H:\demo\spring-boot\target\classes started by xyzhou in H:\demo\spring-boot)2016-03-16 14:02:57.731 INFO 5252 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@704921a5: startup date [Wed Mar 16 14:02:57 CST 2016]; root of context hierarchy2016-03-16 14:02:58.234 INFO 5252 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': 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-03-16 14:02:58.886 INFO 5252 --- [ main] e.j.JettyEmbeddedServletContainerFactory : Server initialized with port: 80802016-03-16 14:02:58.889 INFO 5252 --- [ main] org.eclipse.jetty.server.Server : jetty-9.2.4.v201411032016-03-16 14:02:58.925 INFO 5252 --- [ main] / : Initializing Spring embedded WebApplicationContext2016-03-16 14:02:58.925 INFO 5252 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1198 ms2016-03-16 14:02:59.490 INFO 5252 --- [ main] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2016-03-16 14:02:59.494 INFO 5252 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2016-03-16 14:02:59.496 INFO 5252 --- [ main] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2016-03-16 14:02:59.497 INFO 5252 --- [ main] o.e.jetty.server.handler.ContextHandler : Started o.s.b.c.e.j.JettyEmbeddedWebAppContext@31024624{/,file:/H:/demo/spring-boot/src/main/webapp/,AVAILABLE}2016-03-16 14:02:59.497 INFO 5252 --- [ main] org.eclipse.jetty.server.Server : Started @2862ms2016-03-16 14:02:59.695 INFO 5252 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@704921a5: startup date [Wed Mar 16 14:02:57 CST 2016]; root of context hierarchy2016-03-16 14:02:59.741 INFO 5252 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" 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)2016-03-16 14:02:59.741 INFO 5252 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],methods=[],params=[],headers=[],consumes=[],produces=[text/html],custom=[]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest)2016-03-16 14:02:59.764 INFO 5252 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-16 14:02:59.764 INFO 5252 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-16 14:02:59.799 INFO 5252 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]2016-03-16 14:02:59.940 INFO 5252 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2016-03-16 14:02:59.950 INFO 5252 --- [ main] / : Initializing Spring FrameworkServlet 'dispatcherServlet'2016-03-16 14:02:59.950 INFO 5252 --- [ main] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2016-03-16 14:02:59.964 INFO 5252 --- [ main] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 14 ms2016-03-16 14:03:00.031 INFO 5252 --- [ main] o.eclipse.jetty.server.ServerConnector : Started ServerConnector@6594402a{HTTP/1.1}{0.0.0.0:8080}2016-03-16 14:03:00.034 INFO 5252 --- [ main] .s.b.c.e.j.JettyEmbeddedServletContainer : Jetty started on port(s) 8080 (http/1.1)2016-03-16 14:03:00.036 INFO 5252 --- [ main] Example : Started Example in 2.979 seconds (JVM running for 3.401)
自定义内嵌servlet容器
如果要对内嵌的servlet进行配置,有如下三种方法:
1.通过SpringEnvironment属性进行配置
- 你会把这些属性定义到application.properties.
- server.port -进来的HTTP请求的监听端口号
- server.address-绑定的接口地址
- server.sessionTimeout-session超时时间
2.编程方式的自定义
3.直接自定义ConfigurableEmbeddedServletContainer
- 可以自己注册TomcatEmbeddedServletContainerFactory, JettyEmbeddedServletContainerFactory或UndertowEmbeddedServletContainerFactory
以下是SpringBoot 推荐的基础POM文件,具体应用在下次学习中继续与大家分享。
0 0
- Spring Boot 学习(一)
- Spring Boot学习(一)
- Spring Boot学习一
- Spring Boot学习(一)
- Spring Boot学习(一)
- Spring Boot学习(一)
- Spring Boot学习(一)
- Spring Boot学习(一)
- spring boot 学习(一)
- Spring Boot学习(一)
- Spring Boot 学习笔记(一)
- spring boot零基础学习 spring boot简介(一)
- spring boot 学习一之基础构建
- Spring-Boot学习笔记一:Hello World
- Spring Boot学习记录(一)--环境搭建
- Spring Boot 学习一、搭建初步环境
- spring boot 学习笔记(一)
- 学习笔记一:Spring Boot快速入门
- PrintWriter out=response.getWriter()的问题
- SAX解析xml文件
- R and MongoDB
- PHP编码规范与PSR标准
- android教程之textview解析带图片的html示例
- Spring Boot 学习(一)
- 从零开始搭建 reviewboard 环境(五) -- reviewboard 在linux中的使用
- centos下为php添加gd扩展
- 图像特征提取之HOG+LBP+HAAR
- CSS入门介绍
- 【C++】《C++标准程序库》小结第六章(容器)
- 关于Hi3516D启动死机的问题
- C++继承 派生类中的内存布局 以及虚函数、虚函数表的一些总结
- ubuntu 12.04安装、配置和使用tftp