当前位置:   article > 正文

全网最细的SpringBoot3系列教程

springboot3

1、开发第⼀个Spring Boot应用

创建POM

因为是3.0.0-M1版本,是⾥程碑版本,不是正式发布版,需要从Spring提⾼的Maven仓库中才能下载到3.0.0-M1版本的依赖包,需要在pom.xml⽂件中单独指定仓库地址。

如果使⽤的是正式版,是不需要加下⾯这段配置的

  1. <!-- jar包的仓库地址-->
  2. <repositories>
  3.        <repository>
  4.            <id>spring-snapshots</id>
  5.            <url>https://repo.spring.io/snapshot</url>
  6.            <snapshots><enabled>true</enabled></snapshots>
  7.        </repository>
  8.        <repository>
  9.            <id>spring-milestones</id>
  10.            <url>https://repo.spring.io/milestone</url>
  11.        </repository>
  12.    </repositories>
  13. <!-- maven插件的仓库地址 -->
  14.    <pluginRepositories>
  15.        <pluginRepository>
  16.            <id>spring-snapshots</id>
  17.            <url>https://repo.spring.io/snapshot</url>
  18.        </pluginRepository>
  19.        <pluginRepository>
  20.            <id>spring-milestones</id>
  21.            <url>https://repo.spring.io/milestone</url>
  22.        </pluginRepository>
  23.    </pluginRepositories>

另外还要在pom.xml⽂件中,添加Spring Boot所提供的⽗pom.xml。

  1. <parent>
  2.        <groupId>org.springframework.boot</groupId>
  3.        <artifactId>spring-boot-starter-parent</artifactId>
  4.        <version>3.0.0-M1</version>
  5.    </parent>

关于这个parent的作⽤,后续课程中会介绍。

添加依赖

最后添加⼀个开发web应⽤的starter依赖

  1. <dependencies>
  2.    <dependency>
  3.        <groupId>org.springframework.boot</groupId>
  4.        <artifactId>spring-boot-starter-web</artifactId>
  5.    </dependency>
  6. </dependencies>

写代码

在写代码之前,请注意,我现在⽤的JDK8,也许你也是。
创建⼀个类,类名随便取,我的叫MyApplication,包路径为com.zhouyu

  1. package com.zhouyu;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @EnableAutoConfiguration
  8. public class MyApplication {
  9.    @RequestMapping("/")
  10.    String home() {
  11.        return "Hello World!";
  12.   }
  13.    public static void main(String[] args) {
  14.        SpringApplication.run(MyApplication.class, args);
  15.   }
  16. }

然后,运⾏main⽅法。
我这报错了:

因为Spring Boot 3⽤的是Spring 6,⽽Spring 6需要⽤JDK17


所以,我们要在IDEA中配上JDK17,JDk17的安装没什么特殊的,和JDK8的安装⼀样,就不在笔记⾥浪费篇幅了。


⽤上JDK17之后,再次运⾏main⽅法,就能正常启动我们的第⼀个Spring Boot应⽤程序了,并且会看到如下⽇志: 

  1. "C:\Program Files\Java\jdk-17.0.1\bin\java.exe" "-javaagent:C:\Program
  2. Files\JetBrains\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=64855:C:\Program
  3. Files\JetBrains\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -
  4. classpath D:\IdeaProjects\ZhouyuSpringBootTeacher\target\classes;C:\Users\zhouyu\.m2\repository\org\springframework
  5. \boot\spring-boot-starter-web\3.0.0-M1\spring-boot-starter-web-3.0.0-
  6. M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot-starter\3.0.0-M1\spring-boot-starter-3.0.0-
  7. M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot\3.0.0-M1\spring-boot-3.0.0-
  8. M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot-autoconfigure\3.0.0-M1\spring-boot-autoconfigure-3.0.0-
  9. M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot-starter-logging\3.0.0-M1\spring-boot-starter-logging-3.0.0-
  10. M1.jar;C:\Users\zhouyu\.m2\repository\ch\qos\logback\logbackclassic\1.2.10\logback-classic1.2.10.jar;C:\Users\zhouyu\.m2\repository\ch\qos\logback\logbackcore\1.2.10\logback-core1.2.10.jar;C:\Users\zhouyu\.m2\repository\org\slf4j\slf4japi\1.7.33\slf4j-api1.7.33.jar;C:\Users\zhouyu\.m2\repository\org\apache\logging\log4j\log4jto-slf4j\2.17.1\log4j-to-slf4j2.17.1.jar;C:\Users\zhouyu\.m2\repository\org\apache\logging\log4j\log4japi\2.17.1\log4j-api2.17.1.jar;C:\Users\zhouyu\.m2\repository\org\slf4j\jul-toslf4j\1.7.33\jul-to-slf4j1.7.33.jar;C:\Users\zhouyu\.m2\repository\jakarta\annotation\jakarta.anno
  11. tation-api\2.0.0\jakarta.annotation-api2.0.0.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springcore\6.0.0-M2\spring-core-6.0.0-
  12. M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springjcl\6.0.0-M2\spring-jcl-6.0.0-
  13. M2.jar;C:\Users\zhouyu\.m2\repository\org\yaml\snakeyaml\1.30\snakeyaml1.30.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot-starter-json\3.0.0-M1\spring-boot-starter-json-3.0.0-
  14. M1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\core\jacksondatabind\2.13.1\jackson-databind2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\core\jack
  15. son-annotations\2.13.1\jackson-annotations2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\core\jack
  16. son-core\2.13.1\jackson-core2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\datatype\
  17. jackson-datatype-jdk8\2.13.1\jackson-datatype-jdk8-
  18. 2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\datatype\
  19. jackson-datatype-jsr310\2.13.1\jackson-datatype-jsr310-
  20. 16
  21. 2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\module\ja
  22. ckson-module-parameter-names\2.13.1\jackson-module-parameter-names2.13.1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\spring
  23. -boot-starter-tomcat\3.0.0-M1\spring-boot-starter-tomcat-3.0.0-
  24. M1.jar;C:\Users\zhouyu\.m2\repository\org\apache\tomcat\embed\tomcatembed-core\10.0.16\tomcat-embed-core10.0.16.jar;C:\Users\zhouyu\.m2\repository\org\apache\tomcat\embed\tomcat
  25. -embed-el\10.0.16\tomcat-embed-el10.0.16.jar;C:\Users\zhouyu\.m2\repository\org\apache\tomcat\embed\tomcat
  26. -embed-websocket\10.0.16\tomcat-embed-websocket10.0.16.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springweb\6.0.0-M2\spring-web-6.0.0-
  27. M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springbeans\6.0.0-M2\spring-beans-6.0.0-
  28. M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springwebmvc\6.0.0-M2\spring-webmvc-6.0.0-
  29. M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springaop\6.0.0-M2\spring-aop-6.0.0-
  30. M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springcontext\6.0.0-M2\spring-context-6.0.0-
  31. M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springexpression\6.0.0-M2\spring-expression-6.0.0-M2.jar
  32. com.zhouyu.MyApplication
  33. .   ____          _            __ _ _
  34. /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
  35. ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
  36. \\/  ___)| |_)| | | | | || (_| | ) ) ) )
  37.  ' |____| .__|_| |_|_| |_\__, | / / / /
  38. =========|_|==============|___/=/_/_/_/
  39. :: Spring Boot ::             (v3.0.0-M1)
  40. 2022-03-16 20:43:21.849  INFO 17520 --- [           main]
  41. com.zhouyu.MyApplication                 : Starting MyApplication using
  42. Java 17.0.1 on DESKTOP-P9KHN97 with PID 17520
  43. (D:\IdeaProjects\ZhouyuSpringBoot-Teacher\target\classes started by
  44. zhouyu in D:\IdeaProjects\ZhouyuSpringBoot-Teacher)
  45. 2022-03-16 20:43:21.852  INFO 17520 --- [           main]
  46. com.zhouyu.MyApplication                 : No active profile set, falling
  47. back to default profiles: default
  48. 2022-03-16 20:43:23.257  INFO 17520 --- [           main]
  49. o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with
  50. port(s): 8080 (http)
  51. 2022-03-16 20:43:23.271  INFO 17520 --- [           main]
  52. o.apache.catalina.core.StandardService   : Starting service [Tomcat]
  53. 2022-03-16 20:43:23.272  INFO 17520 --- [           main]
  54. org.apache.catalina.core.StandardEngine : Starting Servlet engine:
  55. [Apache Tomcat/10.0.16]
  56. 2022-03-16 20:43:23.406  INFO 17520 --- [           main] o.a.c.c.C.
  57. [Tomcat].[localhost].[/]       : Initializing Spring embedded
  58. WebApplicationContext
  59. 2022-03-16 20:43:23.409  INFO 17520 --- [           main]
  60. w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext:
  61. initialization completed in 1472 ms
  62. 2022-03-16 20:43:24.147  INFO 17520 --- [           main]
  63. o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s):
  64. 8080 (http) with context p
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/504817
推荐阅读
  

闽ICP备14008679号