1

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.

1 Answer 1

1

Rather than have multiple spring components with the @Profile annotation you could use config classes with @Profile(db_name) and then have the environment-specific profile activation in those classes. For example for the MySQL profile:

    @Configuration
    @Profile("mysql")
    public class MySqlConfiguration{

        public MySqlConfiguration() {
            System.out.println("BD MySQL");
        }

        @Bean
        @Profile("dev") 
        public ConfigDevMySQL() {
            return new ConfigDevMySQL();
        } 

        @Bean
        @Profile("prod") 
        public ConfigProdMySQL () {
            return new ConfigProdMySQL();
        } 
    }

This should mean that the Configuration classes are only read when the relevant DB profile is set and the subsequent Beans only created when the relevant environment profile is set.

You'd also remove the @Component annotation from your current classes - ConfigProdMySQL etc - and instantiate them manually in the configuration classes.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, this is helped me so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.