博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Intellij IDEA 2018.2 搭建Spring Boot 应用
阅读量:6567 次
发布时间:2019-06-24

本文共 16207 字,大约阅读时间需要 54 分钟。

前言


简介: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如下:

4.0.0
com.demo
springboot
0.0.1-SNAPSHOT
jar
springboot
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin

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 2018/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\.IntelliJIdea2018.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\Idea2018.2.2\InstallFiles\IntelliJ IDEA 2018.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)2018-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)2018-09-20 10:43:25.299  INFO 8884 --- [           main] c.demo.springboot.SpringbootApplication  : No active profile set, falling back to default profiles: default2018-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 2018]; root of context hierarchy2018-09-20 10:43:25.908  INFO 8884 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)2018-09-20 10:43:25.924  INFO 8884 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]2018-09-20 10:43:25.924  INFO 8884 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.342018-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;;.]2018-09-20 10:43:25.971  INFO 8884 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext2018-09-20 10:43:25.987  INFO 8884 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 672 ms2018-09-20 10:43:26.018  INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]2018-09-20 10:43:26.018  INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]2018-09-20 10:43:26.018  INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2018-09-20 10:43:26.018  INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]2018-09-20 10:43:26.018  INFO 8884 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]2018-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]2018-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 2018]; root of context hierarchy2018-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()2018-09-20 10:43:26.284  INFO 8884 --- [           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-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)2018-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]2018-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]2018-09-20 10:43:26.393 INFO 8884 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2018-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 ''2018-09-20 10:43:26.424 INFO 8884 --- [ main] c.demo.springboot.SpringbootApplication : Started SpringbootApplication in 1.375 seconds (JVM running for 2.532)2018-09-20 10:43:41.334 INFO 8884 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'2018-09-20 10:43:41.334 INFO 8884 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started2018-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:

        效果如图所示:

 

你可能感兴趣的文章
Trafficserver Cluster模式
查看>>
亚马逊推出 Blox,用于 EC2 容器服务的开源工具集合
查看>>
Linux:在中国没有真正的新闻
查看>>
iOS推送功能极光推送的介绍与实现
查看>>
单用户模式与grub加密
查看>>
Chromium Graphics: 3D上下文及其虚拟化 - Part I
查看>>
jquery javascript获得网页的高度和宽度
查看>>
2019 -2-15 复习
查看>>
vim锁定屏幕
查看>>
实用的 JavaScript 调试小技巧
查看>>
027移除元素
查看>>
Linux下清理内存和Cache方法
查看>>
CodeVS 1018 单词接龙(DFS)
查看>>
我的博客园的CSS和html设置
查看>>
工作中简单的kettle使用
查看>>
spark shuffle:分区原理及相关的疑问
查看>>
Laravel5.5 使用第三方Vendor添加注册验证码
查看>>
06- Linux下sublime下载与使用
查看>>
前端文摘:Web 开发模式演变历史和趋势
查看>>
将图片序列转化为视频文件
查看>>