springboot2官网入门学习记录
springboot官网地址
检查jdk版本和maven版本
1.在命令行输入 java -version,检查当前系统中JDK版本是否为1.8
2.在命令行输入 mvn -v,检查当前系统中maven版本是否为3.5.4,
3.如果jdk版本和maven版本不符合,升级JDK和maven版本
创建maven项目
1.pom.xml添加依赖
<?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>
<groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Additional lines to be added here... -->
</project>
编写代码
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和@RequestMapping 不是springboot特有的,springmvc中就有
*/
@RestController
@EnableAutoConfiguration
public class MyApplication {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
打开浏览器访问http://localhost:8080,就会看见下面输出
Hello World!
springboot打包成jar包
需要在pom.xml
中,在dependencies
下面添加如下代码
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
保存pom.xml文件,然后运行 mvn package,打包springboot项目为jar.
打包后的jar包,可以在当前项目的target文件夹下可查看到
运行该应用程序,使用java -jar
命令,如下
java -jar target/springbootDemo-1.0-SNAPSHOT.jar
spring-boot-starter-parent POM包含<executions>
配置来绑定重新打包的目标。
如果不使用父POM,则需要自己声明此配置。
详情请参阅插件文档
springboot中自定义Log
2022-04-18 15:00:12.349 INFO 12404 — [ main] org.example.App : Starting App using Java 1.8.0_281 on Ov-cit-fanwei04 with PID 12404 (Q:\test\springbootDemo\target\classes started by vendor.cit.huibo01 in Q:\test\springbootDemo)
2022-04-18 15:00:12.353 INFO 12404 — [ main] org.example.App : No active profile set, falling back to 1 default profile: “default”
2022-04-18 15:00:12.974 INFO 12404 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-18 15:00:12.981 INFO 12404 — [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-18 15:00:12.981 INFO 12404 — [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.60]
2022-04-18 15:00:13.114 INFO 12404 — [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-04-18 15:00:13.115 INFO 12404 — [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 735 ms
2022-04-18 15:00:13.361 INFO 12404 — [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ‘’
2022-04-18 15:00:13.369 INFO 12404 — [ main] org.example.App
输出日志格式:
日期 log等级 进程ID — [线程名]日志名称:日志信息
日期格式为 年-月-日 时:分:秒.毫秒
log等级:ERROR, WARN, INFO, DEBUG,TRACE;Logback没有FATAL级别。它被映射到ERROR
进程ID:如上述日志等级INFO之后的12404就是进程ID
— 分隔符就是用来区分日志消息的开始
线程名:用中括号[]包含起来的
日志名称:用中括号[]包含起来之后,在:之前
日志信息:在日志名称之后用:分割的
springboot自定义banner
关闭banner
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class SpringbootStudyApplication {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication application =new SpringApplication(SpringbootStudyApplication.class);
// 关闭springboot中输出的banner
application.setBannerMode(Banner.Mode.OFF);
}
}
配置自定义Banner
在resources下添加文件banner.txt
在application.properties中配置自定义Banner的位置
spring.banner.location=classpath:banner.txt
在idea中关闭banner
这种方式就不用每次都写代码了,可以在每个项目的Edit Configurations中的spring boot中,
找到Hide Banner,勾选,就可以关闭了!