在Spring Boot项目中,你可以通过配置不同的application-{profile}.properties文件来管理不同环境下的配置。具体来说,你可以使用application-dev.properties、application-test.properties和application-pro.properties分别用于开发、测试和生产环境。
当前采用的方案是,每个环境的配置文件都做打包,在运行时指定配置文件
以下是详细的步骤来实现这一点:
1. 创建配置文件
在你的src/main/resources目录下创建以下配置文件:
application-dev.properties:开发环境配置
application-test.properties:测试环境配置
application-pro.properties:生产环境配置
例如:
application-dev.properties
Properties
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update
application-test.properties
Properties
spring.datasource.url=jdbc:mysql://localhost:3306/test_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=testuser
spring.datasource.password=testpass
spring.jpa.hibernate.ddl-auto=validate
application-pro.properties
Properties
spring.datasource.url=jdbc:mysql://prod-db-server:3306/prod_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=produser
spring.datasource.password=prodpass
spring.jpa.hibernate.ddl-auto=none
2. 设置默认配置文件
你可以在application.properties或application.yml中设置默认的配置文件,以便在没有指定活动配置文件时使用默认配置。通常情况下,你会将其设置为开发环境的配置文件。
application.properties
Properties
spring.profiles.active=dev
或者使用YAML格式:
application.yml
Yaml
spring:
profiles:
active: dev
3. 启动应用程序时指定配置文件
在启动Spring Boot应用程序时,可以通过命令行参数或IDE的运行配置来指定要使用的配置文件。
使用命令行参数
开发环境
Sh
mvn spring-boot:run -Dspring-boot.run.profiles=dev
测试环境
Sh
mvn spring-boot:run -Dspring-boot.run.profiles=test
生产环境
Sh
mvn spring-boot:run -Dspring-boot.run.profiles=pro
在IDEA中配置运行配置
打开IntelliJ IDEA。
进入 Run -> Edit Configurations。
选择你的Spring Boot应用程序配置。
在 VM options 或 Environment variables 中添加以下内容:
VM options: -Dspring.profiles.active=dev
Environment variables: SPRING_PROFILES_ACTIVE=dev
4. 打包成JAR文件并指定配置文件
当你将应用程序打包成JAR文件时,也可以通过命令行参数来指定配置文件。
打包成JAR文件
Sh
mvn clean package
运行JAR文件并指定配置文件
开发环境
Sh
java -jar your-application.jar --spring.profiles.active=dev
测试环境
Sh
java -jar your-application.jar --spring.profiles.active=test
生产环境
Sh
java -jar your-application.jar --spring.profiles.active=pro
完整示例
假设你的项目结构如下:
深色版本
your-springboot-project/
├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/demo/
│ │ └── DemoApplication.java
│ └── resources/
│ ├── application.properties
│ ├── application-dev.properties
│ ├── application-test.properties
│ └── application-pro.properties
└── pom.xml
pom.xml(为避免错误,可以在打包完成后删除不是该环境需要的配置,以免错误)
确保你的pom.xml文件中包含必要的依赖项:Xml
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<!-- MySQL Connector -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
DemoApplication.java
这是你的主应用程序类:Java
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
总结
通过上述步骤,你可以在Spring Boot项目中灵活地管理和切换不同环境的配置文件。确保每个环境的配置文件正确无误,并且在启动应用程序时正确指定所需的配置文件。这样可以方便地进行开发、测试和部署。如果有任何问题或需要进一步的帮助,请随时告诉我!