前言
简介: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项目或者是构建一个微服务!
Spring Boot 应用搭建
1、创建一个新项目(Create New Project)
选择Spring Initializr,Project SDK 选择1.8,Service URL选择默认,点击Next,如图所示:
2、项目配置,点击Next,如图所示:
解释:
Group:对应pom文件中的groupId,项目组织的唯一标识,对应Java包的结构
Artifact:对应pom文件的artifactId,项目唯一标识,对应项目名称
Type:我们是Maven构建的,那么选择第一个Maven Project
Language:开发语言,选择Java
Packaging:打包类型,打包成Jar文件
Java Version:jdk版本,选择1.8
Version:项目版本,对应pom文件的version
Name:项目名称
Description:项目描述,对应pom文件的description
Package:包名
3、Spring Boot 版本选择,我这里选择了2.0.5,勾选Web下的web,点击Next,如图所示:
4、填写项目名称,项目存放的目录,点击 Finish
5、根据个人习惯,可以将一下三个无关文件删除,干净目录,如图所示:
6、清理后的项目目录,如图所示:
到此为止,一个Spring boot 应用在Intellij IDEA上搭建完成。
其中创建项目时填写的属性在pom文件中可以看到,项目生成的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.demo</groupId><artifactId>springboot</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springboot</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.5.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进行测试
package com.demo.springboot.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** TestController** @author WuYuxiang* @date /9/20*/@RestController@RequestMapping("/")public class TestController {@RequestMapping("springboot")public String testSpringBoot() {return "Hello Spring Boot ! ";}}
@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面,如果需要返回jsp,html页面,则使用@Controller注解。
整个项目的启动类就是上面的SpringbootApplication
package com.demo.springboot;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class SpringbootApplication {public static void main(String[] args) {SpringApplication.run(SpringbootApplication.class, args);}}
8、启动项目,如图所示:
9、启动成功信息,端口8080
信息:Tomcat started on port(s): 8080 (http) with context path ''
E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:62736,suspend=y,server=n -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=62733 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:C:\Users\Administrator\.IntelliJIdea.2\system\captureAgent\debugger-agent.jar=file:/C:/Users/Administrator/AppData/Local/Temp/capture4324.props -Dfile.encoding=UTF-8 -classpath "E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\charsets.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\deploy.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\dnsns.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\jaccess.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\localedata.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\nashorn.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\sunec.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\ext\zipfs.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\javaws.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\jce.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\jfr.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\jfxswt.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\jsse.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\management-agent.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\plugin.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\resources.jar;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\lib\rt.jar;D:\DevProject\java\demo\springboot\target\classes;E:\Maven\MavenRepository\org\springframework\boot\spring-boot-starter-web\2.0.5.RELEASE\spring-boot-starter-web-2.0.5.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\boot\spring-boot-starter\2.0.5.RELEASE\spring-boot-starter-2.0.5.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\boot\spring-boot\2.0.5.RELEASE\spring-boot-2.0.5.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\boot\spring-boot-autoconfigure\2.0.5.RELEASE\spring-boot-autoconfigure-2.0.5.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\boot\spring-boot-starter-logging\2.0.5.RELEASE\spring-boot-starter-logging-2.0.5.RELEASE.jar;E:\Maven\MavenRepository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;E:\Maven\MavenRepository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;E:\Maven\MavenRepository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;E:\Maven\MavenRepository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;E:\Maven\MavenRepository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;E:\Maven\MavenRepository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;E:\Maven\MavenRepository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;E:\Maven\MavenRepository\org\springframework\boot\spring-boot-starter-json\2.0.5.RELEASE\spring-boot-starter-json-2.0.5.RELEASE.jar;E:\Maven\MavenRepository\com\fasterxml\jackson\core\jackson-databind\2.9.6\jackson-databind-2.9.6.jar;E:\Maven\MavenRepository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;E:\Maven\MavenRepository\com\fasterxml\jackson\core\jackson-core\2.9.6\jackson-core-2.9.6.jar;E:\Maven\MavenRepository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.6\jackson-datatype-jdk8-2.9.6.jar;E:\Maven\MavenRepository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.6\jackson-datatype-jsr310-2.9.6.jar;E:\Maven\MavenRepository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.6\jackson-module-parameter-names-2.9.6.jar;E:\Maven\MavenRepository\org\springframework\boot\spring-boot-starter-tomcat\2.0.5.RELEASE\spring-boot-starter-tomcat-2.0.5.RELEASE.jar;E:\Maven\MavenRepository\org\apache\tomcat\embed\tomcat-embed-core\8.5.34\tomcat-embed-core-8.5.34.jar;E:\Maven\MavenRepository\org\apache\tomcat\embed\tomcat-embed-el\8.5.34\tomcat-embed-el-8.5.34.jar;E:\Maven\MavenRepository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.34\tomcat-embed-websocket-8.5.34.jar;E:\Maven\MavenRepository\org\hibernate\validator\hibernate-validator\6.0.12.Final\hibernate-validator-6.0.12.Final.jar;E:\Maven\MavenRepository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;E:\Maven\MavenRepository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;E:\Maven\MavenRepository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;E:\Maven\MavenRepository\org\springframework\spring-web\5.0.9.RELEASE\spring-web-5.0.9.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\spring-beans\5.0.9.RELEASE\spring-beans-5.0.9.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\spring-webmvc\5.0.9.RELEASE\spring-webmvc-5.0.9.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\spring-aop\5.0.9.RELEASE\spring-aop-5.0.9.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\spring-context\5.0.9.RELEASE\spring-context-5.0.9.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\spring-expression\5.0.9.RELEASE\spring-expression-5.0.9.RELEASE.jar;E:\Maven\MavenRepository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;E:\Maven\MavenRepository\org\springframework\spring-core\5.0.9.RELEASE\spring-core-5.0.9.RELEASE.jar;E:\Maven\MavenRepository\org\springframework\spring-jcl\5.0.9.RELEASE\spring-jcl-5.0.9.RELEASE.jar;C:\Develop\IntellijIdea\Idea.2.2\InstallFiles\IntelliJ IDEA .2.2\lib\idea_rt.jar" com.demo.springboot.SpringbootApplicationConnected to the target VM, address: '127.0.0.1:62736', transport: 'socket'. _____ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: (v2.0.5.RELEASE)-09-20 10:43:25.299 INFO 8884 --- [ main] c.demo.springboot.SpringbootApplication : Starting SpringbootApplication on VikasDhanve with PID 8884 (D:\DevProject\java\demo\springboot\target\classes started by Administrator in D:\DevProject\java\demo\springboot)-09-20 10:43:25.299 INFO 8884 --- [ main] c.demo.springboot.SpringbootApplication : No active profile set, falling back to default profiles: default-09-20 10:43:25.330 INFO 8884 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1672fe87: startup date [Thu Sep 20 10:43:25 CST ]; root of context hierarchy-09-20 10:43:25.908 INFO 8884 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)-09-20 10:43:25.924 INFO 8884 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]-09-20 10:43:25.924 INFO 8884 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.34-09-20 10:43:25.924 INFO 8884 --- [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: [E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\bin;C:\windows\Sun\Java\bin;C:\windows\system32;C:\windows;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;E:\DevelopEnv\Python\Python37\Scripts\;E:\DevelopEnv\Python\Python37\;C:\Program Files (x86)\Common Files\Intel\Shared Libraries\redist\intel64\compiler;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\bin;E:\DevelopEnv\JDK\jdk-1.8\jdk1.8.0_181\jre\bin;C:\Program Files\dotnet\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;E:\VersionTools\Git\InstallFiles\Git\cmd;E:\Maven\apache-maven-3.5.4\bin;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;;.]-09-20 10:43:25.971 INFO 8884 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext-09-20 10:43:25.987 INFO 8884 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 672 ms-09-20 10:43:26.018 INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]-09-20 10:43:26.018 INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]-09-20 10:43:26.018 INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]-09-20 10:43:26.018 INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]-09-20 10:43:26.018 INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]-09-20 10:43:26.096 INFO 8884 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]-09-20 10:43:26.237 INFO 8884 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@1672fe87: startup date [Thu Sep 20 10:43:25 CST ]; root of context hierarchy-09-20 10:43:26.284 INFO 8884 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/springboot]}" onto public java.lang.String com.demo.springboot.controller.TestController.testSpringBoot()-09-20 10:43:26.284 INFO 8884 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)-09-20 10:43:26.284 INFO 8884 --- [ 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)-09-20 10:43:26.299 INFO 8884 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]-09-20 10:43:26.299 INFO 8884 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]-09-20 10:43:26.393 INFO 8884 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup-09-20 10:43:26.424 INFO 8884 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''-09-20 10:43:26.424 INFO 8884 --- [ main] c.demo.springboot.SpringbootApplication : Started SpringbootApplication in 1.375 seconds (JVM running for 2.532)-09-20 10:43:41.334 INFO 8884 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'-09-20 10:43:41.334 INFO 8884 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started-09-20 10:43:41.350 INFO 8884 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 16 ms
10、浏览器输入URL:http://localhost:8080/springboot
效果如图所示: