SpringBoot基础24_SpringBoot快速入门2
一、实现步骤分析
1、创建Maven工程
2、添加SpringBoot的起步依赖
3、编写SpringBoot引导类
4、编写Controller
5、测试
二、代码实现
1、创建Maven工程
先创建一个空项目,然后使用idea工具创建一个maven工程,该module为普通的java工程即可
2、添加SpringBoot的起步依赖
SpringBoot要求,项目要继承SpringBoot的起步依赖spring-boot-starter-parent
SpringBoot要集成SpringMVC进行Controller的开发,所以项目要导入web的启动依赖
<?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>
<!--所有的springboot工程都必须继承spring-boot-start-parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <groupId>com.itheima</groupId> <artifactId>springboot_quick</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--web功能的起步依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
3、编写SpringBoot引导类
要通过SpringBoot提供的引导类起步SpringBoot才可以进行访问
package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
//声明该类是一个SpringBoot引导类 @SpringBootApplication public class MySpringBootApplication { public static void main(String[] args) {
//run方法 表示运行SpringBoot的引导类 run参数就是SpringBoot引导类的字节码对象 SpringApplication.run(MySpringBootApplication.class); } }
4、编写Controller
在引导类MySpringBootApplication同级包或者子级包中创建QuickStartController
package com.itheima.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class QuickStartController { @RequestMapping("/quick") @ResponseBody public String quick(){ return "hello springboot"; } }
5、测试
执行SpringBoot起步类的主方法,控制台打印日志如下:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.1.RELEASE) 2024-03-26 15:22:48.688 INFO 4492 --- [ main] com.itheima.MySpringBootApplication : Starting MySpringBootApplication on LAPTOP-VEKHRDDR with PID 4492 (C:\workFiles\javaLearn\code\SpringBoot\springboot_quick\target\classes started by ajing in C:\workFiles\javaLearn\code\SpringBoot) 2024-03-26 15:22:48.692 INFO 4492 --- [ main] com.itheima.MySpringBootApplication : No active profile set, falling back to default profiles: default 2024-03-26 15:22:48.742 INFO 4492 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5906ebcb: startup date [Tue Mar 26 15:22:48 CST 2024]; root of context hierarchy 2024-03-26 15:22:49.789 INFO 4492 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2024-03-26 15:22:49.815 INFO 4492 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2024-03-26 15:22:49.815 INFO 4492 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.29 2024-03-26 15:22:49.820 INFO 4492 --- [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: [C:\software\Java\jdk1.8.0_251\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\software\Java\jdk1.8.0_251\bin;C:\software\Anaconda3;C:\software\Anaconda3\Library\mingw-w64\bin;C:\software\Anaconda3\Library\usr\bin;C:\software\Anaconda3\Library\bin;C:\software\Anaconda3\Scripts;C:\Program Files (x86)\Common Files\NetSarang;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\;C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\software\apache-jmeter-3.1\bin;C:\strawberry\c\bin;C:\strawberry\perl\bin;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\software\MySQL Server 5.5\bin;C:\software\apache-maven-3.5.2\bin;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Users\ajing\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;.] 2024-03-26 15:22:49.938 INFO 4492 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2024-03-26 15:22:49.938 INFO 4492 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1196 ms 2024-03-26 15:22:50.065 INFO 4492 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2024-03-26 15:22:50.070 INFO 4492 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2024-03-26 15:22:50.070 INFO 4492 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2024-03-26 15:22:50.070 INFO 4492 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2024-03-26 15:22:50.070 INFO 4492 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2024-03-26 15:22:50.158 INFO 4492 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2024-03-26 15:22:50.339 INFO 4492 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5906ebcb: startup date [Tue Mar 26 15:22:48 CST 2024]; root of context hierarchy 2024-03-26 15:22:50.382 INFO 4492 --- [ 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) 2024-03-26 15:22:50.383 INFO 4492 --- [ 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) 2024-03-26 15:22:50.399 INFO 4492 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2024-03-26 15:22:50.399 INFO 4492 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler] 2024-03-26 15:22:50.505 INFO 4492 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup 2024-03-26 15:22:50.536 INFO 4492 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2024-03-26 15:22:50.539 INFO 4492 --- [ main] com.itheima.MySpringBootApplication : Started MySpringBootApplication in 2.177 seconds (JVM running for 3.031)
通过日志发现,Tomcat started on port(s): 8080 (http) with context path ''
tomcat已经起步,端口监听8080,web应用的虚拟工程名称为空
打开浏览器访问url地址为:http://localhost:8080/quick
三、快速入门解析
1、SpringBoot代码解析
- @SpringBootApplication:标注SpringBoot的启动类,该注解具备多种功能(后面详细剖析)
- SpringApplication.run(MySpringBootApplication.class) 代表运行SpringBoot的启动类,参数为SpringBoot启动类的字节码对象
2、 SpringBoot工程热部署
我们在开发中反复修改类、页面等资源,每次修改后都是需要重新启动才生效,这样每次启动都很麻烦,浪费了大量的时间,我们可以在修改代码后不重启就能生效,在 pom.xml 中添加如下配置就可以实现这样的功能,我们称之为热部署。
<dependency> <!--热部署配置--> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
注意:IDEA进行SpringBoot热部署失败原因
出现这种情况,并不是热部署配置问题,其根本原因是因为Intellij IEDA默认情况下不会自动编译,需要对IDEA进行自动编译的设置,如下:
然后 Shift+Ctrl+Alt+/,选择Registry
四、使用idea快速创建SpringBoot项目
通过idea快速创建的SpringBoot项目的pom.xml中已经导入了我们选择的web的起步依赖的坐标(手动修改了spring-boot-starter-parent和java版本)
注意:有可能在自动配置的过程中没有配置项目所需的相关依赖,这时就必须进行手动配置。
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itheima</groupId> <artifactId>springboot_quick2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_quick2</name> <description>Demo project for Spring Boot</description> <properties> <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>
自动生成了SpringbootQuick2Application引导类
package com.itheima; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringbootQuick2Application { public static void main(String[] args) { SpringApplication.run(SpringbootQuick2Application.class, args); } }
可以使用快速入门的方式创建Controller进行访问,此处不再赘述
编写Controller类QuickController
package com.itheima.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController //包含了Controller和ResponseBody注解 public class QuickController { @RequestMapping("/quick2") public String quick(){ return "springboot !!"; } }
启动项目,执行SpringbootQuick2Application中main方法
如出现报错信息:Error:(3, 29) java: 程序包org.junit.jupiter.api不存在,那么就需要手动配置
浏览器访问http://localhost:8080/quick2,打印成功