【发布时间】:2020-09-20 04:37:13
【问题描述】:
我是Spring Boot的新手,不知道自己想做的是否可行,但是我有以下问题要解决。
1 - 我有一个带有 Spring Boot 的 API,我需要配置两个 DBMS(MySQL 和 Postgres)。
2 - 对于每个 DBMS,我需要配置不同的配置文件。 (开发,产品)
关注我的 MySQL 配置类:
@Component
@Profile("mysql")
public class ConfigMySQL {
public ConfigMySQL() {
System.out.println("BD MySQL");
}
}
@Component
@Profile("dev")
public class ConfigDevMySQL extends ConfigMySQL {
public ConfigDevMySQL() {
System.out.println("\n\n");
System.out.println(" ****** Configuration dev MySQL... ******");
System.out.println("\n\n");
}
}
@Component
@Profile("prod")
public class ConfigProdMySQL extends ConfigMySQL{
public ConfigProdMySQL() {
System.out.println("\n\n");
System.out.println(" ****** Configuration prod MySQL... ******");
System.out.println("\n\n");
}
}
关注我的 Postgres 配置类:
@Component
@Profile("postgres")
public class ConfigPostgres {
public ConfigPostgres() {
System.out.println("DB postgres");
}
}
@Component
@Profile("dev")
public class ConfigDevPostgres extends ConfigPostgres{
public ConfigDevPostgres() {
System.out.println("\n\n");
System.out.println(" ****** Configuration dev Postgres... ******");
System.out.println("\n\n");
}
}
@Component
@Profile("prod")
public class ConfigProdPostgres extends ConfigPostgres {
public ConfigProdPostgres() {
System.out.println("\n\n");
System.out.println(" ****** Configuration prod Postgres... ******");
System.out.println("\n\n");
}
}
我的个人资料文件如下所示:
注意:我很确定他错了,但例如我一直这样。
spring.profiles.active=mysql
spring.profiles.active=dev
请始终牢记,它是一个 API,具有两个 DBMS 和不同的配置文件。
当我运行我的应用程序时,我在控制台上有这个输出
BD MySQL
****** Configuration dev MySQL... ******
DB postgres
****** Configuration dev Postgres... ******
我预计我的退出是:
DB MySQL
****** Configuration dev MySQL... ******
重要:
当我将配置文件配置为连接到我的 MySQL Dev 数据库时,我只想连接到它。当它是 MySQL Prod 的基础时,我想要同样的结果。
这也适用于 Postgres。
当我将配置文件配置为连接到我的 Postgres Dev 数据库时,我只想连接到它。当它是 Postgres Prod 的基础时,我想要同样的结果。
我真的不知道这是否可能,我什至不知道该怎么做!
有谁知道如何进行此设置,您能帮我吗?
感谢您的关注。
【问题讨论】:
标签: java spring spring-boot spring-profiles spring-properties