SpringBoot笔记

application.properties

Property形式

server.port=80 // 端口号
aruge.arugement=value
@Value("{argue.argument}")
String argu;

YAML形式

server:
port: 80

spring:
datasource:
url: jdbc:mysql://localhost:3306/db_name
username:
password:
driver-class-name: com.mysql.cj.Driver
mvc:
static-path-pattern: /static/**
security:
filter:
order: -100 # Spring Security Filter 优先级
user:
name: 'admin'
password: 'Abc123.'
roles:
- admin
- user

Package

JAR

mvn package
java -jar yourProj.jar

WAR

  1. 排除SpringBoot内嵌Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 使用exclusion排除 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
  1. 添加Servlet依赖
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
  1. 将打包方式修改为WAR
<packaging>war</packaging>
  1. 修改主类Application
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer { //继承专用的初始化器
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

//重写configure方法,完成启动类配置
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(DemoApplication.class);
}
}
  1. 打包
mvn package

GraalVM Native EXE

[!WARNING]
Mybatis目前不支持Native-Image

mvn -Pnative -DskipTests native:compile

SLF4J

Simple Logging Facade for Java

日志门面,如Slf4j,是把不同的日志系统的实现进行了具体的抽象化。最终日志的格式、记录级别、输出方式等都要通过接口绑定的具体的日志系统,如log4j、jul来实现。

测试-生成环境隔离

SpringBoot只会读取application.ymlapplication.properties,可以在文件中指定使用不同的配置

spring:
profiles:
active: dev

然后编写一份application-dev.yml

Maven独立打包环境配置

[!Info]
切换配置文件后,一定要刷新!

<!--分别设置开发,生产环境-->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<activation>
<!-- 打包 -->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>dev</environment>
</properties>
</profile>

<!-- 生产环境 -->
<profile>
<id>prod</id>
<activation>
<!-- 不打包 -->
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment>prod</environment>
</properties>
</profile>
</profiles>
<resources>
<!--排除配置文件-->
<resource>
<directory>src/main/resources</directory>
<!--先排除所有的配置文件-->
<excludes>
<!--使用通配符,当然可以定义多个exclude标签进行排除-->
<exclude>application*.yml</exclude>
</excludes>
</resource>

<!--根据激活条件引入打包所需的配置和文件-->
<resource>
<directory>src/main/resources</directory>
<!--引入所需环境的配置文件-->
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<!--根据maven选择环境导入配置文件-->
<include>application-${environment}.yml</include>
</includes>
</resource>
</resources>
spring:
profiles:
active: '@environment@' # 匹配maven激活的环境