Spring Profiles

  • Can be set a runtime
  • When we set a profile, Spring is going to take only beans with that profile. To use profile on bean we use the annotation @Profile on classes
//This will only run if the 'dev' profile is active
@Profile("dev")
@Service
public InMemoryDatabaseService implements Database{

}

@Service("prod")
public MySQLDatabaseService implements Database{

}

How to set the profile:

In the application.yaml or application.properties:

spring.active.profile="dev"

How is that useful?

Can be used for Integration Test

@Service
@Profile(value = "integration")
public class EmailServiceStub implements EmailService {

    @Override
    public void sendEmail() {
        //Empty body as we do not send an email
    }

}
@Service
@Profile(value = {"!integration"})
public class DefaultEmailService implements EmailService {

    @Override
    public void sendEmail() {
        //Empty body as we do not send an email
    }

}

results matching ""

    No results matching ""