Spring Boot 项目旨在简化创建产品级的 Spring 应用和服务。你可通过它来选择不同的 Spring 平台。可创建独立的 Java 应用和 Web 应用,同时提供了命令行工具来允许 'spring scripts'.
下图显示 Spring Boot 在 Spring 生态中的位置:
该项目主要的目的是:
为 Spring 的开发提供了更快更广泛的快速上手
使用默认方式实现快速开发
提供大多数项目所需的非功能特性,诸如:嵌入式服务器、安全、心跳检查、外部配置等
Spring Boot 不生成代码,完全无需 XML 配置。
新建一个Maven项目,修改POM加载需要的依赖
<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>test3</groupId> <artifactId>test3</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>test3</name> <description /> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 这里一定要配置上java的版本,如果是1.7版本的可不用配置 <java.version>1.6</java.version>--> <!-- 配置你的tomcat版本 <tomcat.version>7.0.55</tomcat.version> --> <spring.boot.version>1.1.4.RELEASE</spring.boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${spring.boot.version}</version> </dependency> </dependencies> </project>
看到以前的一些文章用的都是1.0版本,但是使用时Jar冲突问题非常突出,后来在IBM网站看到有更高版本,另外取消对J2EE的引用,就可以跑起来了。
编写Controller文件
package com.test3; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class Application { @RequestMapping("/test") String home() { return "Hello World baby!"; } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
直接运行Main方法,请求根路径http://localhost:8080/test 会打印一句话。
注意,这里虽然以Http方式启动了,但是仅用于测试SpringMVC的接口程序,如果你访问http://localhost:8080/index.jsp 会看到,不会解析JSP。
访问Spring提供的接口
附上示例代码:
网上有人总结的遇到的问题:
1.springboot 的logback-classes-1.1.2.jar的包下有一个org.slf4j.impl包 这是springboot真正需要的包而MyEclipse自带的javaEE6.0library里也有一个slf4j包但它不是springboot所需要的,会一直报 NoSuchMethod异常getSingleton()。所以测试事暂时不添加javaEE6.0Library。
2.官方文档的例子都是用java7运行的。不配置<java.version>1.6</java.version>的话可能 会报版本异常的错误。
3.如果也不配置tomcat版本的话springboot默认会使用8.x版本的tomcat。所以要加一个<tomcat.version>7.0.55</tomcat.version>来指定你所使用的tomcat版本(视你CATALINA_HOME配 置的所定)。
运行成功,后台打印:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.1.4.RELEASE)
2016-02-23 12:43:38.060 INFO 23064 --- [ main] com.test3.Application : Starting Application on cuisuqiang_work with PID 23064 (D:\JavaSpace\2014\test3\target\classes started by cuisuqiang in D:\JavaSpace\2014\test3)
2016-02-23 12:43:38.116 INFO 23064 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f7b9ed4: startup date [Tue Feb 23 12:43:38 CST 2016]; root of context hierarchy
2016-02-23 12:43:38.712 INFO 23064 --- [ 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-02-23 12:43:40.028 INFO 23064 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2016-02-23 12:43:40.281 INFO 23064 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2016-02-23 12:43:40.283 INFO 23064 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/7.0.54
2016-02-23 12:43:40.429 INFO 23064 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2016-02-23 12:43:40.429 INFO 23064 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2315 ms
2016-02-23 12:43:41.008 INFO 23064 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2016-02-23 12:43:41.010 INFO 23064 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2016-02-23 12:43:41.253 INFO 23064 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-23 12:43:41.370 INFO 23064 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto java.lang.String com.test3.Application.home()
2016-02-23 12:43:41.374 INFO 23064 --- [ 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-02-23 12:43:41.374 INFO 23064 --- [ 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-02-23 12:43:41.395 INFO 23064 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-23 12:43:41.395 INFO 23064 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2016-02-23 12:43:41.613 INFO 23064 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2016-02-23 12:43:41.701 INFO 23064 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080/http
2016-02-23 12:43:41.703 INFO 23064 --- [ main] com.test3.Application : Started Application in 4.202 seconds (JVM running for 4.589)