新手必看!SpringBoot 系列之从 0 搭建项目

 


前言

使用SpringBoot已经也有两年多了,从一开始对SpringBoot的零认知到如今平常开发必接触的框架,说实话受益良多,其实SpringBoot就是Spring的扩展,之前咱们作框架整合以及开发过程当中会有大量的配置文件须要配置,而SpringBoot的出现就是把咱们从大量配置文件xml中解救出来,再也不须要作过多bean配置、DI配置,使用SpringBoot以后只须要集中在application配置文件中作简单属性配置便可,因为SpringBoot内嵌了Tomcat这样还免去了咱们安装Tomcat的麻烦,咱们只须要运行项目根目录下启动类的main方法便可启动项目,是否是对比以往的项目有没有感受牛逼plus,今天先说到这,接下来咱们学习如何从零搭建SpringBoot项目。java


提示:如下是本篇文章正文内容,下面案例可供参考web

1、开发工具安装,环境安装准备工做

  1. 开发工具:Eclipse/IntelliJ IDEA(我用的IDEA)
    开发工具可自行去官网下载
  2. JAVA环境:JDK(我用的1.8版本)
    JDK自行去官网下载, window环境变量配置教程.
  3. Jar管理:Maven(我用的IDEA插件maven3)
    也可自行安装maven在开发工具setting中进行配置。

2、开发工具安装Spring帮助插件

1.Eclipse安装Spring Tools4插件

因为我使用的是IntelliJ IDEA开发工具,这里我就不作详细介绍了。spring

2.IntelliJ IDEA安装Spring Assistant插件

  1. 打开IDEA,单击菜单栏中的“File->Setting->plugins”打开插件窗口。apache

  2. 在插件窗口搜索“spring”或“Spring Assistant”回车,找到以下图Install安装。(我已经安装过了,因此是按钮是Uninstall)
    在这里插入图片描述
    浏览器

  3. 重启IDEA生效。tomcat

3、搭建SpringBoot项目工程

1.使用IDEA中的插件“Spring Assistant”建立项目。

①菜单栏“file->new->project”打开建立项目窗口。
在这里插入图片描述
②默认next
在这里插入图片描述
③根据本身须要更改,next。
在这里插入图片描述
④本次就建立一个web项目因此咱们勾选spring web ,而后next。
在这里插入图片描述
springboot

⑤Finish。
在这里插入图片描述
app

2.项目结构。

①下图是使用插件“Spring Assistant”帮咱们生成的项目。
在这里插入图片描述
框架

②从①图中能够看出来建立SpringBoot项目仍是很简单的。maven

  1. maven项目pom.xml加入springboot依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
  1. 建立启动类DemoApplication.java,注意启动类必定要建立在代码根目录哦,否则后续加入其余代码会启动报错。
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
  1. application配置文件,此文件能够放项目使用的一些属性。
    更改项目启动端口,项目全局路径,等等均可以。
  2. 咱们能够建立一个Controller控制器,加上@RestController注解,在控制器中加一个test方法,方法加上@GetMapping("/test")
package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DemoController { @GetMapping("/test") public String test() { return "Hello World"; } }
  1. 项目都建立好了,启动看看实际效果,DemoApplication.java类右键而后Run 'DemoApplication’或Debug 'DemoApplication’启动。
    在这里插入图片描述
    项目默认启动端口是8080,也可在application配置文件自定义,启动日志:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.4.RELEASE) 2020-10-24 14:19:35.103 INFO 109976 --- [ restartedMain] com.example.demo.DemoApplication : Starting DemoApplication on wb_lichao001 with PID 109976 2020-10-24 14:19:35.108 INFO 109976 --- [ restartedMain] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default 2020-10-24 14:19:35.236 INFO 109976 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2020-10-24 14:19:35.236 INFO 109976 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2020-10-24 14:19:37.408 INFO 109976 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-10-24 14:19:37.426 INFO 109976 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-10-24 14:19:37.427 INFO 109976 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.38] 2020-10-24 14:19:37.579 INFO 109976 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-10-24 14:19:37.580 INFO 109976 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2342 ms 2020-10-24 14:19:37.907 INFO 109976 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-10-24 14:19:38.088 INFO 109976 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2020-10-24 14:19:38.145 INFO 109976 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-10-24 14:19:38.164 INFO 109976 --- [ restartedMain] com.example.demo.DemoApplication : Started DemoApplication in 4.024 seconds (JVM running for 6.96)
  1. 打开浏览器,访问http://localhost:8080/test,成功返回Hello World。
    在这里插入图片描述

总结

今天建立是使用插件的方式来帮咱们建立SpringBoot项目,其实咱们能够先建立一个maven项目,而后pom.xml加入依赖,建立Application.java启动类(加@SpringBootApplication注解,main方法),新增application配置文件,最终也能建立出一个最简单的springboot项目,今天分享就到这里,但愿可以帮助到你们,记得收藏点赞哦!!!有补充/须要均可评论留言,大家的支持就是博主的动力!!!

更多好文敬请关注公众号:main方法

相关文章
相关标签/搜索