本次我重新更新是发现现在好多文章,写的文章都很松散,这次我准备从SpringBoot开始介绍,然后介绍SpringCloud一直到前后端分离,希望大家可以进行关注。
SpringBoot初始化
1.SpringBoot创建方法
SpringBoot有三种方式进行创建分别是:
Spring Initializr(Web 界面)
SpringBoot ClI (命令行)
Soring Boot IDEA(Eclipse、IntelliJ IDEA等)
一般来说创建SpringBoot的构建类型可以是Maven或者Gradle,可以自由选择自己喜欢的方式,一般我选择Java的Maven工程比较多。
Springboot执行过程如下:
2.SpringBoot创建的三种方式
======================
2.1 Spring Initializr创建SpringBoot
通过访问https://start.spring.io/地址进行创建SpringBoot
将代码进行解压F:\Java_File\myfirst-springboot-demo1\myfirst-springboot-demo1,然后通过“mvn spring-boot:run”进行启动。
cd F:\Java_File\myfirst-springboot-demo1\myfirst-springboot-demo1 mvn spring-boot:run
运行结果
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.3) 2022-02-14 16:12:05.920 INFO 33816 --- [ main] c.m.s.MyfirstSpringbootDemo1Application : Starting MyfirstSpringbootDemo1Application using Java 1.8.0_291 on dq18-180454w with PID 33816 (F:\Java_File\myfirst-springboot-demo1\myfirst-springboot-demo1\target\classes started by 180454 in F:\Java_File\myfirst-springboot-demo1\myfirst-springboot-demo1) 2022-02-14 16:12:05.922 INFO 33816 --- [ main] c.m.s.MyfirstSpringbootDemo1Application : No active profile set, falling back to default profiles: default 2022-02-14 16:12:06.934 INFO 33816 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-02-14 16:12:06.942 INFO 33816 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-02-14 16:12:06.942 INFO 33816 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.56] 2022-02-14 16:12:07.447 INFO 33816 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-02-14 16:12:07.447 INFO 33816 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1489 ms 2022-02-14 16:12:07.730 INFO 33816 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-02-14 16:12:07.739 INFO 33816 --- [ main] c.m.s.MyfirstSpringbootDemo1Application : Started MyfirstSpringbootDemo1Application in 2.199 seconds (JVM running for 3.735) 2022-02-14 16:12:19.518 INFO 33816 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2022-02-14 16:12:19.518 INFO 33816 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2022-02-14 16:12:19.524 INFO 33816 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
2.2 Spring Boot CLI创建项目
下载并安装SpringBoot ClI
(1) 从官网下载spring-boot-cli-2.6.3-bin.zip https://repo.spring.io/artifactory/release/org/springframework/boot/spring-boot-cli/2.6.3/spring-boot-cli-2.6.3-bin.zip 选择对应的版本 (2) 解压到 D:\software\exe\ (3) 将D:\software\exe\spring-boot-cli-2.6.3-bin\spring-2.6.3\bin 添加到环境变量PATH里。 (4) 打开cmd C:\Users\Administrator>spring --version Spring CLI v2.6.3
创建SpringBoot并运行
D:\software\file\java>mkdir spring-myfirst-demo2 D:\software\file\java>cd spring-myfirst-demo2 D:\software\file\java\spring-myfirst-demo2>spring init -d=web -g=com.myfirst.springboot -a=spring-myfirst-demo2 --package-name=com.myfirst.springboot --name=spring-myfirst-demo2 --javaVersion=8 Using service at https://start.spring.io Project extracted to 'D:\software\file\java\spring-myfirst-demo2' D:\software\file\java\spring-myfirst-demo2>mvn spring-boot:run
运行结果
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.3) 2022-02-14 21:03:21.017 INFO 83252 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1434 ms 2022-02-14 21:03:21.220 INFO 83252 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-02-14 21:03:21.227 INFO 83252 --- [ main] c.m.s.SpringMyfirstDemo2Application : Started SpringMyfirstDemo2Application in 2.149 seconds (JVM running for 2.555)
Spring init命令参数
-d(dependencies 依赖包) -g(Group Id) -a(Artifact Id) --package-name(Package name) --name(Project name) -x(Extract compatible archives)
查看对应的命令帮助
spring help init
查看所有可用依赖:
Parameters +-------------+------------------------------------------+------------------------------+ | Id | Description | Default value | +-------------+------------------------------------------+------------------------------+ | artifactId | project coordinates (infer archive name) | demo | | bootVersion | spring boot version | 2.6.3 | | description | project description | Demo project for Spring Boot | | groupId | project coordinates | com.example | | javaVersion | language level | 11 | | language | programming language | java | | name | project name (infer application name) | demo | | packageName | root package | com.example.demo | | packaging | project packaging | jar | | type | project type | maven-project | | version | project version | 0.0.1-SNAPSHOT | +-------------+------------------------------------------+------------------------------+
2.3 IDEA创建SpringBoot项目
2.3.1创建过程如图所示
初始化Spring Initializr
SpringBoot项目命名
选择相关依赖
生成的项目目录
运行项目结果:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.6.3) 2022-02-14 16:42:29.198 INFO 31784 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1979 ms 2022-02-14 16:42:29.645 INFO 31784 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-02-14 16:42:29.653 INFO 31784 --- [ main] c.m.s.SpringBootDemo3Application : Started SpringBootDemo3Application in 2.897 seconds (JVM running for 5.56)
2.4访问运行地址
访问 http://localhost:8080/,结果为:
报错的原因:这是因为现在项目中还没有存在任何一个Controller,所以只能看到404页面
2.5 SpringBoot项目文件夹构成介绍
SpringBootDemo3Application是项目的启动项代码如下:
@SpringBootApplication public class SpringBootDemo3Application { public static void main(String[] args) { SpringApplication.run(SpringBootDemo3Application.class, args); } }
@SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiration
默认扫描的package是Application类所在的package.
2.6添加一个TestController
代码如下:
package com.myfirst.springboot.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @program: spring-boot-demo3 * @package: com.myfirst.springboot.controller * @classname: TestController * @description: test * @author: zbb * @create: 2022-02-14 */ @RestController public class TestController { @RequestMapping("/") public String testIndex(){ return "This is a Test index"; } }
访问 http://localhost:8080/,结果为:
注:如果存在多个MAIN函数,可能需要指定启动类
xml代码
<properties> <!-- The main class to start by executing java -jar --> <start-class>com.myfirst.springboot.SpringBootDemo3Application</start-class> </properties>
或者
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.myfirst.springboot.SpringBootDemo3Application</mainClass> </configuration> </plugin> </plugins> </build>