赞
踩
创建POM
因为是3.0.0-M1版本,是⾥程碑版本,不是正式发布版,需要从Spring提⾼的Maven仓库中才能下载到3.0.0-M1版本的依赖包,需要在pom.xml⽂件中单独指定仓库地址。
如果使⽤的是正式版,是不需要加下⾯这段配置的
- <!-- jar包的仓库地址-->
- <repositories>
- <repository>
- <id>spring-snapshots</id>
- <url>https://repo.spring.io/snapshot</url>
- <snapshots><enabled>true</enabled></snapshots>
- </repository>
- <repository>
- <id>spring-milestones</id>
- <url>https://repo.spring.io/milestone</url>
- </repository>
- </repositories>
- <!-- maven插件的仓库地址 -->
- <pluginRepositories>
- <pluginRepository>
- <id>spring-snapshots</id>
- <url>https://repo.spring.io/snapshot</url>
- </pluginRepository>
- <pluginRepository>
- <id>spring-milestones</id>
- <url>https://repo.spring.io/milestone</url>
- </pluginRepository>
- </pluginRepositories>
另外还要在pom.xml⽂件中,添加Spring Boot所提供的⽗pom.xml。
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>3.0.0-M1</version>
- </parent>
关于这个parent的作⽤,后续课程中会介绍。
添加依赖
最后添加⼀个开发web应⽤的starter依赖
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
写代码
在写代码之前,请注意,我现在⽤的JDK8,也许你也是。
创建⼀个类,类名随便取,我的叫MyApplication,包路径为com.zhouyu
- package com.zhouyu;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- @RestController
- @EnableAutoConfiguration
- public class MyApplication {
- @RequestMapping("/")
- String home() {
- return "Hello World!";
- }
- public static void main(String[] args) {
- SpringApplication.run(MyApplication.class, args);
- }
- }
然后,运⾏main⽅法。
我这报错了:
因为Spring Boot 3⽤的是Spring 6,⽽Spring 6需要⽤JDK17。
所以,我们要在IDEA中配上JDK17,JDk17的安装没什么特殊的,和JDK8的安装⼀样,就不在笔记⾥浪费篇幅了。
⽤上JDK17之后,再次运⾏main⽅法,就能正常启动我们的第⼀个Spring Boot应⽤程序了,并且会看到如下⽇志:
- "C:\Program Files\Java\jdk-17.0.1\bin\java.exe" "-javaagent:C:\Program
- Files\JetBrains\IntelliJ IDEA 2021.1.3\lib\idea_rt.jar=64855:C:\Program
- Files\JetBrains\IntelliJ IDEA 2021.1.3\bin" -Dfile.encoding=UTF-8 -
- classpath D:\IdeaProjects\ZhouyuSpringBootTeacher\target\classes;C:\Users\zhouyu\.m2\repository\org\springframework
- \boot\spring-boot-starter-web\3.0.0-M1\spring-boot-starter-web-3.0.0-
- M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot-starter\3.0.0-M1\spring-boot-starter-3.0.0-
- M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot\3.0.0-M1\spring-boot-3.0.0-
- M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot-autoconfigure\3.0.0-M1\spring-boot-autoconfigure-3.0.0-
- M1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\springboot-starter-logging\3.0.0-M1\spring-boot-starter-logging-3.0.0-
- 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
- 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-
- M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springjcl\6.0.0-M2\spring-jcl-6.0.0-
- 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-
- 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
- son-annotations\2.13.1\jackson-annotations2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\core\jack
- son-core\2.13.1\jackson-core2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\datatype\
- jackson-datatype-jdk8\2.13.1\jackson-datatype-jdk8-
- 2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\datatype\
- jackson-datatype-jsr310\2.13.1\jackson-datatype-jsr310-
- 16
- 2.13.1.jar;C:\Users\zhouyu\.m2\repository\com\fasterxml\jackson\module\ja
- ckson-module-parameter-names\2.13.1\jackson-module-parameter-names2.13.1.jar;C:\Users\zhouyu\.m2\repository\org\springframework\boot\spring
- -boot-starter-tomcat\3.0.0-M1\spring-boot-starter-tomcat-3.0.0-
- 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
- -embed-el\10.0.16\tomcat-embed-el10.0.16.jar;C:\Users\zhouyu\.m2\repository\org\apache\tomcat\embed\tomcat
- -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-
- M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springbeans\6.0.0-M2\spring-beans-6.0.0-
- M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springwebmvc\6.0.0-M2\spring-webmvc-6.0.0-
- M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springaop\6.0.0-M2\spring-aop-6.0.0-
- M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springcontext\6.0.0-M2\spring-context-6.0.0-
- M2.jar;C:\Users\zhouyu\.m2\repository\org\springframework\springexpression\6.0.0-M2\spring-expression-6.0.0-M2.jar
- com.zhouyu.MyApplication
- . ____ _ __ _ _
- /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
- ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
- \\/ ___)| |_)| | | | | || (_| | ) ) ) )
- ' |____| .__|_| |_|_| |_\__, | / / / /
- =========|_|==============|___/=/_/_/_/
- :: Spring Boot :: (v3.0.0-M1)
- 2022-03-16 20:43:21.849 INFO 17520 --- [ main]
- com.zhouyu.MyApplication : Starting MyApplication using
- Java 17.0.1 on DESKTOP-P9KHN97 with PID 17520
- (D:\IdeaProjects\ZhouyuSpringBoot-Teacher\target\classes started by
- zhouyu in D:\IdeaProjects\ZhouyuSpringBoot-Teacher)
- 2022-03-16 20:43:21.852 INFO 17520 --- [ main]
- com.zhouyu.MyApplication : No active profile set, falling
- back to default profiles: default
- 2022-03-16 20:43:23.257 INFO 17520 --- [ main]
- o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with
- port(s): 8080 (http)
- 2022-03-16 20:43:23.271 INFO 17520 --- [ main]
- o.apache.catalina.core.StandardService : Starting service [Tomcat]
- 2022-03-16 20:43:23.272 INFO 17520 --- [ main]
- org.apache.catalina.core.StandardEngine : Starting Servlet engine:
- [Apache Tomcat/10.0.16]
- 2022-03-16 20:43:23.406 INFO 17520 --- [ main] o.a.c.c.C.
- [Tomcat].[localhost].[/] : Initializing Spring embedded
- WebApplicationContext
- 2022-03-16 20:43:23.409 INFO 17520 --- [ main]
- w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext:
- initialization completed in 1472 ms
- 2022-03-16 20:43:24.147 INFO 17520 --- [ main]
- o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s):
- 8080 (http) with context p
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。