Spring: Enable Components and Job Schedulers with Spring Profiles

Spring: Enable Components and Job Schedulers with Spring Profiles

In Spring you can enable a component using a profile:

@Profile("prod")
@Component
public class ProductionService() {
    // This will only be created with Spring_Profile: prod enable
}

Another use case is enabling/disabling schedulers:

@Profile("nightly-job")
@Component
public class NighlyJobScheduler() {
    // This will only be created with Spring_Profile: nightly-job
}

It can get cumbersome adding all the various profiles when you run the application. Spring now offer profile groups:

spring:
  profiles:
      active: development
      group:
        prod: prodJob1, prodJob2
        development: dev-scheduler-1, dev-notification

In the example above, running the profile “development” will automatically bring in the other profiles “dev-scheduler-1, dev-notification”