Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
使用spring boot有什么好处
其实就是简单、快速、方便!平时如果我们需要搭建一个spring web项目的时候需要怎么做呢?
1)配置web.xml,加载spring和spring mvc
2)配置数据库连接、配置spring事务
3)配置加载配置文件的读取,开启注解
4)配置日志文件
...
配置完成之后部署tomcat 调试
...
现在非常流行微服务,如果我这个项目仅仅只是需要发送一个邮件,如果我的项目仅仅是生产一个积分;我都需要这样折腾一遍!
但是如果使用spring boot呢?
很简单,我仅仅只需要非常少的几个配置就可以迅速方便的搭建起来一套web项目或者是构建一个微服务!
1.创建一个新项目(Create New Project)
选择 Spring Initializr,Project SDK 选择 1.8,URL 填写默认的,点击 Next
2.继续项目配置
Name:项目名称
Type:我们是Maven构建的,那么选择第一个Maven Project
Packaging:打包类型,打包成Jar文件
Java Version:jdk版本,选择1.8
Language:开发语言,选择Java
Group:对应pom文件中的groupId,项目组织的唯一标识,对应Java包的结构
Artifact:对应pom文件的artifactId,项目唯一标识,对应项目名称
Version:项目版本,对应pom文件的version
Description:项目描述,对应pom文件的description
Package:包名
3.Spring Boot 版本选择,我这里选择了1.5.2,只要勾选Web下的web就可以了,然后Next
4.填写项目名称,项目存放的目录,点击 Finish
5.可以将下面这三个无关的文件删除,干净结构
6.最后的项目结构
这里看一下自动生成的pom.xml文件
<?xml version="1.0" encoding="UTF-8"?> <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>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <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> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
7.新建一个Controller进行测试
@RestController public class HelloController { @RequestMapping(value = "/hello") public String hello() { return "hello,Spring Boot"; } }
@RestController的意思就是controller里面的方法都以json格式输出,不用再写什么jackjson配置的了!
整个项目的启动类就是上面的DemoApplication
@SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
8.启动成功信息,端口8080
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.3.RELEASE) 2017-04-24 01:29:17.293 INFO 19480 --- [ restartedMain] com.example.DemoApplication : Starting DemoApplication on DESKTOP-4U9C0CH with PID 19480 (D:\IdeaProjects\test\demo\target\classes started by winner_0715 in D:\IdeaProjects\test\demo) 2017-04-24 01:29:17.295 INFO 19480 --- [ restartedMain] com.example.DemoApplication : No active profile set, falling back to default profiles: default 2017-04-24 01:29:17.432 INFO 19480 --- [ restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f5f2168: startup date [Mon Apr 24 01:29:17 CST 2017]; root of context hierarchy 2017-04-24 01:29:21.450 INFO 19480 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http) 2017-04-24 01:29:21.493 INFO 19480 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service Tomcat 2017-04-24 01:29:21.495 INFO 19480 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.14 2017-04-24 01:29:21.820 INFO 19480 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2017-04-24 01:29:21.820 INFO 19480 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4396 ms 2017-04-24 01:29:22.136 INFO 19480 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/] 2017-04-24 01:29:22.149 INFO 19480 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2017-04-24 01:29:22.150 INFO 19480 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2017-04-24 01:29:22.150 INFO 19480 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2017-04-24 01:29:22.151 INFO 19480 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2017-04-24 01:29:22.642 INFO 19480 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1f5f2168: startup date [Mon Apr 24 01:29:17 CST 2017]; root of context hierarchy 2017-04-24 01:29:22.785 INFO 19480 --- [ restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/index]}" onto public java.util.Map<java.lang.String, java.lang.String> com.example.test.IndexController.test(java.lang.String) 2017-04-24 01:29:22.791 INFO 19480 --- [ restartedMain] 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) 2017-04-24 01:29:22.792 INFO 19480 --- [ restartedMain] 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) 2017-04-24 01:29:22.896 INFO 19480 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-04-24 01:29:22.896 INFO 19480 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-04-24 01:29:22.963 INFO 19480 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2017-04-24 01:29:23.250 INFO 19480 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2017-04-24 01:29:23.351 INFO 19480 --- [ restartedMain] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2017-04-24 01:29:23.492 INFO 19480 --- [ restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) 2017-04-24 01:29:23.507 INFO 19480 --- [ restartedMain] com.example.DemoApplication : Started DemoApplication in 6.736 seconds (JVM running for 7.717)
9.到浏览器中输入 http://localhost:8080/hello
上面讲的打包方式是jar,试了一下war的打包方式,生成的目录结构是这样的