import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @Component @Profile("dev") public class DevService { public String getName() { return "Development Service"; } } @Component @Profile("prod") public class ProdService { public String getName() { return "Production Service"; } }
The @Profile annotation tells Spring to load a bean only if the specified profile is active. Since the active profile is 'dev', only the DevService bean is loaded. The ProdService bean is ignored because its profile 'prod' is not active.
The correct syntax to specify multiple profiles is to pass an array of strings: @Profile({"dev", "test"}). Other options are either invalid Java syntax or incorrect usage.
import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @Component @Profile("production") public class PaymentService { // service code }
The active profile is 'prod' but the bean is annotated with @Profile("production"), which does not match. Profile names must match exactly for the bean to load.
import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; @Component @Profile("dev") public class DevConfig { public String getConfig() { return "Dev Config"; } } @Component @Profile("prod") public class ProdConfig { public String getConfig() { return "Prod Config"; } }
Beans annotated with @Profile are only loaded if their profile is active. If no profile is active, none of these beans load.
The @Profile annotation supports negation with !. Using @Profile("!test") loads the bean for all profiles except 'test'. Other options are invalid or unsupported syntax.