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 user: name: 'admin' password: 'Abc123.' roles: - admin - user
|
Package
JAR
mvn package java -jar yourProj.jar
|
WAR
- 排除SpringBoot内嵌Tomcat
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>
|
- 添加Servlet依赖
<dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <scope>provided</scope> </dependency>
|
- 将打包方式修改为WAR
<packaging>war</packaging>
|
- 修改主类Application
@SpringBootApplication public class DemoApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
@Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }
|
- 打包
GraalVM Native EXE
[!WARNING]
Mybatis目前不支持Native-Image
mvn -Pnative -DskipTests native:compile
|
SLF4J
Simple Logging Facade for Java
日志门面,如Slf4j,是把不同的日志系统的实现进行了具体的抽象化。最终日志的格式、记录级别、输出方式等都要通过接口绑定的具体的日志系统,如log4j、jul来实现。
测试-生成环境隔离
SpringBoot只会读取application.yml
或application.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>application*.yml</exclude> </excludes> </resource>
<resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application.yml</include> <include>application-${environment}.yml</include> </includes> </resource> </resources>
|
spring: profiles: active: '@environment@'
|