Explore key concepts of Spring Boot profiles and environment management with this quiz designed to reinforce understanding of configuration, property handling, and deployment environments in Java applications.
Which property should be set to activate the 'dev' profile in a Spring Boot application?
Explanation: The correct way to activate a profile is by setting the spring.profiles.active property to the desired profile name, such as 'dev'. Other options like profile.activate=dev and profiles.set=dev are not recognized configuration properties. Using spring.active=dev is incorrect and will not have any effect. Only spring.profiles.active=dev is valid for enabling profiles.
If you want to provide database settings only for the 'prod' profile, which file should you use?
Explanation: Profile-specific configuration is conventionally managed in files named application-profile.properties, so application-prod.properties applies settings only when the 'prod' profile is active. application.properties is loaded for all profiles unless overridden. prod-application.properties does not follow the expected naming pattern. database-prod.conf is not recognized by default.
What is the default active profile in a Spring Boot application if no profile is set?
Explanation: If no profile is explicitly set, the default profile named 'default' is active. 'local', 'dev', and 'prod' need to be activated through configuration and are not automatically chosen. Therefore, only 'default' is correct when no other profile is specified.
How can you activate both 'dev' and 'qa' profiles at the same time?
Explanation: You can activate multiple profiles by specifying them as a comma-separated list in spring.profiles.active, such as 'dev,qa'. The other options use incorrect property names or formats, such as 'profile.active=dev-qa' or 'profiles.active=dev+qa', which are invalid. Only the correct property will enable multiple profiles.
Which source has the highest precedence when overriding properties: environment variable, application.properties, or command-line argument?
Explanation: Command-line arguments have the highest precedence and will override values set by environment variables or properties files. Environment variables come after command-line arguments but before application.properties. System property files are not a standard configuration mechanism and have no direct effect here.
Where should the @Profile annotation be used to conditionally enable a bean for the 'test' profile?
Explanation: The @Profile annotation is used on the bean class or the bean-producing method to limit its loading to specific profiles like 'test'. Applying it to the main application class or property files has no effect. You also cannot place it on the package declaration.
If a setting exists both in application.properties and application-staging.properties with different values, and the 'staging' profile is active, which value is used?
Explanation: Profile-specific property files like application-staging.properties override any matching properties from application.properties when the profile is active. The base properties file would be ignored for these keys, not merged or made undefined. Only the profile-specific value applies.
Which property allows adding extra profiles on top of those set in spring.profiles.active?
Explanation: The spring.profiles.include property lets you include extra profiles to be activated along with those already specified. The other options such as profiles.additional, profile.include.active, and spring.include.profiles are not supported property names, so they would have no effect.
What method allows accessing the value of an environment property in an application component?
Explanation: To access environment properties in a component, the Environment.getProperty() method is used. @PropertyValue is incorrect (the correct annotation is @Value), and @ConfigSettings and Profile.get() are not valid methods for accessing properties. Only Environment.getProperty() allows for property retrieval.
Which method checks if a specific profile is currently active at runtime in a component?
Explanation: The Environment.acceptsProfiles() method is used to check if a certain profile is active at runtime within application components. The other options, like Profile.activateNow(), SpringProfile.active(), and isActiveProfile(), are not standard methods for this purpose. Only acceptsProfiles() provides this checking functionality.