What are Spring profiles?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid Spring Boot basics — a prerequisite for any developer role.
Answer
Spring profiles allow defining different configurations for different environments (development, testing, production) within the same application. Beans and properties can be conditionally activated based on the active profile. Defining profile-specific properties: create files: application-dev.properties (dev profile), application-prod.properties (prod profile), application-test.properties (test profile). Properties in profile-specific files override application.properties. Activating profiles: property file: spring.profiles.active=dev,aws; command line: java -jar app.jar --spring.profiles.active=prod; environment variable: SPRING_PROFILES_ACTIVE=prod; in tests: @ActiveProfiles("test"). Profile-specific beans: @Profile("dev") @Bean public DataSource h2DataSource() { ... } @Profile("prod") @Bean public DataSource postgresDataSource() { ... } @Profile("!prod") @Service public class MockEmailService implements EmailService { ... }. @Profile on @Configuration: entire configuration class active only for a profile. Profile groups (Spring Boot 2.4+): group profiles: spring.profiles.group.production=prod,cloud,monitoring — activating "production" activates all three. Default profile: beans with no @Profile are always active. Beans with @Profile("default") are active when no other profile is active. Conditional beans without profiles: use @Conditional annotations for more flexible conditions.
Pro Tip
Before answering, structure your response: one-line definition → real-world analogy → concrete example from a project. This makes even complex Spring Boot answers easy to follow.