I'm new to Spring Boot, and I don't know if what I want to do is possible, but I have the following problem to solve.
1 - I have an API with Spring Boot and I need to configure two DBMS (MySQL and Postgres).
2 - For each DBMS I need to configure different profiles. (Dev, Prod)
Follow my MySQL configuration classes:
@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");
}
}
Follow my Postgres configuration classes:
@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");
}
}
my profile file looks like this:
Note: I'm pretty sure he's wrong, but I kept it that way for example.
spring.profiles.active=mysql
spring.profiles.active=dev
Always keep in mind that it is a single API, with two DBMS and different profiles.
When I run my application I have this output on the console
BD MySQL
****** Configuration dev MySQL... ******
DB postgres
****** Configuration dev Postgres... ******
I expected my exit to be:
DB MySQL
****** Configuration dev MySQL... ******
Important:
When I configure the profile to connect to my MySQL Dev database, I only want to connect to it. And I want the same result, when it is the basis of MySQL's Prod.
This also applies to Postgres.
When I configure the profile to connect to my Postgres Dev database, I want to connect only to it. And I want the same result, when it is the base of Prod of Postgres.
I really don't know if this is possible and I don't even know how to do it!
Does anyone know how to do this setup, and could you help me?
I thank you for your attention.