前言
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
工具:
idea 2021版本
maven 3.6.1版本(可兼容idea2018至idea2021版本)
jdk 1.8
一、创建项目(方法一)
点击New Project
直接点击Next
初学者项目名称自定义均可
在企业中一般位com.**** 之类的
在创建好的项目中我们找到pom.xm文件并打开
将以下依赖拷入pom.xml文件中
<!--SpringBoot工程需要继承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<!--web开发起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
考在<project> </project>里面即可
在src/main/java/路径下建com.mySpringBoot.Controller包
将鼠标 放在蓝色java标——》右击——》选择package——》输入包名com.mySpringBoot.Controller
选中新建的Controller包右击——》选中新建java文件——》命名位HelloController
并且在HelloController.class文件中写入如下代码做测试
package com.mySpringBoot.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.xml.ws.RequestWrapper;
@RestController
public class HellolController {
@RequestMapping("/hello")
public String hello(){
return "你好,我是SpringBoot";
}
}
继续选中Controller包——》右击选中新建java文件——》文件命名为HelloApplication
并且在HelloApplication.class文件中写入
写入一个main方法(用于运行项目)
写入注解引导类
package com.mySpringBoot.Controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/*引导类。SpringBoot项目的入口*/
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}
}
之后我们在main方法的左侧找到三角形点击运行run
运行结果如下
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2021.3.1\lib\idea_rt.jar=54956:D:\Program Files\JetBrains\IntelliJ IDEA 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\Users\86188\IdeaProjects2021\mySpringBoot-hello\target\classes;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-web\2.1.8.RELEASE\spring-boot-starter-web-2.1.8.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter\2.1.8.RELEASE\spring-boot-starter-2.1.8.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot\2.1.8.RELEASE\spring-boot-2.1.8.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-autoconfigure\2.1.8.RELEASE\spring-boot-autoconfigure-2.1.8.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-logging\2.1.8.RELEASE\spring-boot-starter-logging-2.1.8.RELEASE.jar;D:\apache-maven\Repositries\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\apache-maven\Repositries\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\apache-maven\Repositries\org\slf4j\slf4j-api\1.7.28\slf4j-api-1.7.28.jar;D:\apache-maven\Repositries\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;D:\apache-maven\Repositries\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;D:\apache-maven\Repositries\org\slf4j\jul-to-slf4j\1.7.28\jul-to-slf4j-1.7.28.jar;D:\apache-maven\Repositries\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\apache-maven\Repositries\org\springframework\spring-core\5.1.9.RELEASE\spring-core-5.1.9.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\spring-jcl\5.1.9.RELEASE\spring-jcl-5.1.9.RELEASE.jar;D:\apache-maven\Repositries\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-json\2.1.8.RELEASE\spring-boot-starter-json-2.1.8.RELEASE.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\core\jackson-databind\2.9.9.3\jackson-databind-2.9.9.3.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\core\jackson-core\2.9.9\jackson-core-2.9.9.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.9\jackson-datatype-jdk8-2.9.9.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.9\jackson-datatype-jsr310-2.9.9.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.9\jackson-module-parameter-names-2.9.9.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-tomcat\2.1.8.RELEASE\spring-boot-starter-tomcat-2.1.8.RELEASE.jar;D:\apache-maven\Repositries\org\apache\tomcat\embed\tomcat-embed-core\9.0.24\tomcat-embed-core-9.0.24.jar;D:\apache-maven\Repositries\org\apache\tomcat\embed\tomcat-embed-el\9.0.24\tomcat-embed-el-9.0.24.jar;D:\apache-maven\Repositries\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.24\tomcat-embed-websocket-9.0.24.jar;D:\apache-maven\Repositries\org\hibernate\validator\hibernate-validator\6.0.17.Final\hibernate-validator-6.0.17.Final.jar;D:\apache-maven\Repositries\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\apache-maven\Repositries\org\jboss\logging\jboss-logging\3.3.3.Final\jboss-logging-3.3.3.Final.jar;D:\apache-maven\Repositries\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\apache-maven\Repositries\org\springframework\spring-web\5.1.9.RELEASE\spring-web-5.1.9.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\spring-beans\5.1.9.RELEASE\spring-beans-5.1.9.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\spring-webmvc\5.1.9.RELEASE\spring-webmvc-5.1.9.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\spring-aop\5.1.9.RELEASE\spring-aop-5.1.9.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\spring-context\5.1.9.RELEASE\spring-context-5.1.9.RELEASE.jar;D:\apache-maven\Repositries\org\springframework\spring-expression\5.1.9.RELEASE\spring-expression-5.1.9.RELEASE.jar" com.mySpringBoot.Controller.HelloApplication
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE)
2022-07-23 21:07:30.843 INFO 26960 --- [ main] c.m.Controller.HelloApplication : Starting HelloApplication on LAPTOP-E6O84DR8 with PID 26960 (D:\Users\86188\IdeaProjects2021\mySpringBoot-hello\target\classes started by 86188 in D:\Users\86188\IdeaProjects2021\mySpringBoot-hello)
2022-07-23 21:07:30.845 INFO 26960 --- [ main] c.m.Controller.HelloApplication : No active profile set, falling back to default profiles: default
2022-07-23 21:07:31.575 INFO 26960 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-07-23 21:07:31.592 INFO 26960 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-07-23 21:07:31.592 INFO 26960 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2022-07-23 21:07:31.807 INFO 26960 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-07-23 21:07:31.808 INFO 26960 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 935 ms
2022-07-23 21:07:31.950 INFO 26960 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2022-07-23 21:07:32.086 INFO 26960 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-23 21:07:32.089 INFO 26960 --- [ main] c.m.Controller.HelloApplication : Started HelloApplication in 1.494 seconds (JVM running for 4.346)
我们只需要在游览器导航栏中输入:
localhost:8080/hello
运行结果为:
创建项目(方法二)
虽然SpringBoot代码比较精简,但是在新建项目的过程中还是需要写一定的代码,为了更加简便,idea提供了更加简便项目创建方法。
选中New Project
我们在目录中选中Spring Initialiar
项目名之类的均可以自己修改然后Next
在web——》选中SpringWeb 直接FInish
项目打开之后 已经为我们自行创建好了
运行项目的main方法,以及引导类
我们在选中myspringboothello2包右击新建一个HelloController的java文件
在HelloController文件中书写如下代码:
package com.example.myspringboothello2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "你好 我第二个是SpringBoot项目";
}
}
然后找到main方法左侧的三角形点击Run
运行结果如下
"C:\Program Files\Java\jdk1.8.0_291\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\Program Files\JetBrains\IntelliJ IDEA 2021.3.1\lib\idea_rt.jar=55192:D:\Program Files\JetBrains\IntelliJ IDEA 2021.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_291\jre\lib\rt.jar;D:\Users\86188\IdeaProjects2021\mySpringBoot-Hello2\target\classes;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-web\2.7.2\spring-boot-starter-web-2.7.2.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter\2.7.2\spring-boot-starter-2.7.2.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot\2.7.2\spring-boot-2.7.2.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-autoconfigure\2.7.2\spring-boot-autoconfigure-2.7.2.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-logging\2.7.2\spring-boot-starter-logging-2.7.2.jar;D:\apache-maven\Repositries\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\apache-maven\Repositries\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\apache-maven\Repositries\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\apache-maven\Repositries\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\apache-maven\Repositries\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\apache-maven\Repositries\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\apache-maven\Repositries\org\yaml\snakeyaml\1.30\snakeyaml-1.30.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-json\2.7.2\spring-boot-starter-json-2.7.2.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\core\jackson-databind\2.13.3\jackson-databind-2.13.3.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\core\jackson-annotations\2.13.3\jackson-annotations-2.13.3.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\core\jackson-core\2.13.3\jackson-core-2.13.3.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.3\jackson-datatype-jdk8-2.13.3.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.3\jackson-datatype-jsr310-2.13.3.jar;D:\apache-maven\Repositries\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.3\jackson-module-parameter-names-2.13.3.jar;D:\apache-maven\Repositries\org\springframework\boot\spring-boot-starter-tomcat\2.7.2\spring-boot-starter-tomcat-2.7.2.jar;D:\apache-maven\Repositries\org\apache\tomcat\embed\tomcat-embed-core\9.0.65\tomcat-embed-core-9.0.65.jar;D:\apache-maven\Repositries\org\apache\tomcat\embed\tomcat-embed-el\9.0.65\tomcat-embed-el-9.0.65.jar;D:\apache-maven\Repositries\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.65\tomcat-embed-websocket-9.0.65.jar;D:\apache-maven\Repositries\org\springframework\spring-web\5.3.22\spring-web-5.3.22.jar;D:\apache-maven\Repositries\org\springframework\spring-beans\5.3.22\spring-beans-5.3.22.jar;D:\apache-maven\Repositries\org\springframework\spring-webmvc\5.3.22\spring-webmvc-5.3.22.jar;D:\apache-maven\Repositries\org\springframework\spring-aop\5.3.22\spring-aop-5.3.22.jar;D:\apache-maven\Repositries\org\springframework\spring-context\5.3.22\spring-context-5.3.22.jar;D:\apache-maven\Repositries\org\springframework\spring-expression\5.3.22\spring-expression-5.3.22.jar;D:\apache-maven\Repositries\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\apache-maven\Repositries\org\springframework\spring-core\5.3.22\spring-core-5.3.22.jar;D:\apache-maven\Repositries\org\springframework\spring-jcl\5.3.22\spring-jcl-5.3.22.jar" com.example.myspringboothello2.MySpringBootHello2Application
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.2)
2022-07-23 21:22:45.931 INFO 31568 --- [ main] c.e.m.MySpringBootHello2Application : Starting MySpringBootHello2Application using Java 1.8.0_291 on LAPTOP-E6O84DR8 with PID 31568 (D:\Users\86188\IdeaProjects2021\mySpringBoot-Hello2\target\classes started by 86188 in D:\Users\86188\IdeaProjects2021\mySpringBoot-Hello2)
2022-07-23 21:22:45.934 INFO 31568 --- [ main] c.e.m.MySpringBootHello2Application : No active profile set, falling back to 1 default profile: "default"
2022-07-23 21:22:46.617 INFO 31568 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-07-23 21:22:46.624 INFO 31568 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-07-23 21:22:46.624 INFO 31568 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65]
2022-07-23 21:22:46.881 INFO 31568 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-07-23 21:22:46.881 INFO 31568 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 903 ms
2022-07-23 21:22:47.162 INFO 31568 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-07-23 21:22:47.170 INFO 31568 --- [ main] c.e.m.MySpringBootHello2Application : Started MySpringBootHello2Application in 1.622 seconds (JVM running for 2.629)
我们只需要在游览器导航栏中输入:
localhost:8080/hello
运行结果为:
小结
springBoot提供了一种快速开发Sprint项目的方式,而不是对Spring功能上的增强
Spring的缺点:
- 配置繁琐
- 依赖繁琐
SpringBoot功能:
- 自动配置(用于简化配置繁琐)
- 起步依赖:依赖传递(用于简化依赖繁琐)
- 辅助功能:(嵌入式服务器、检测等等)
- SpringBoot在创建项目时,使用jar的打包方式(创建项目直接创建,不用勾选)
- SpringBoot的引导类,是项目入口,运行main方法就可以启动项目
- 使用spring Boot和spring构建的项目,业务代码编写方式完全一样