上一篇:01-Springboot优点&缺点https://blog.csdn.net/fsjwin/article/details/109698409
Springboot可以快速构建出一个web项目,仅需要一下几步:
1. 创建项目
创建细节在此非重点哦!
2. pom文件
<?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>
<groupId>com.yuhl</groupId>
<artifactId>springboot-20201114</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>01_first</module>
</modules>
<!--https://docs.spring.io/spring-boot/docs/2.4.0/reference/html/getting-started.html#getting-started-first-application-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<dependencies>
引入web模块
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--打包的插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 使用springboot2.3.0
- 使用springweb仅需引入web的starter即可
到此环境已经准备好了,使用起来是不是超级简单。
3. 主启动类FirstSpringBootApplication
package com.yuhl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
/**
* @author yuhl
* @Date 2020/11/14 8:54
* @Classname FisrSpringBootApplication
* @Description TODO
*/
@SpringBootApplication
public class FirstSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstSpringBootApplication.class, args);
}
}
主启动类不可少,他是整个程序的入口。
注意:
1. @SpringBootApplication注解必不可少。
2. pringApplication.run(FirstSpringBootApplication.class, args);这一句就可使程序跑起来了。
4. 控制器HelloController
package com.yuhl;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yuhl
* @Date 2020/11/14 8:54
* @Classname controller
* @Description TODO
*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello";
}
}
5. 启动项目
启动成功,日志如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)
2020-11-15 08:11:35.095 INFO 6816 --- [ main] com.yuhl.FirstSpringBootApplication : Starting FirstSpringBootApplication on LAPTOP-SGL4V58K with PID 6816 (D:\Idea2020Projects\springboot-20201114\01_first\target\classes started by yuhl in D:\Idea2020Projects\springboot-20201114)
2020-11-15 08:11:35.100 INFO 6816 --- [ main] com.yuhl.FirstSpringBootApplication : No active profile set, falling back to default profiles: default
2020-11-15 08:11:36.141 INFO 6816 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-11-15 08:11:36.148 INFO 6816 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-11-15 08:11:36.148 INFO 6816 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35]
2020-11-15 08:11:36.250 INFO 6816 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-11-15 08:11:36.250 INFO 6816 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1036 ms
2020-11-15 08:11:36.383 INFO 6816 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-15 08:11:36.505 INFO 6816 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-15 08:11:36.512 INFO 6816 --- [ main] com.yuhl.FirstSpringBootApplication : Started FirstSpringBootApplication in 1.955 seconds (JVM running for 3.975)
6. 页面访问正常
7. 总结
创建boot项目的步骤
- 创建maven项目
- 引入web的starter
- 写主启动类添加注解@SpringbootApplication注解
- 启动即可
- 注意点:选择需要使用的boot版本即可。
下一篇:03-Springboot配置文件application.yml || application.properties https://blog.csdn.net/fsjwin/article/details/109699856