Spring Boot Profiles and Environment Management Quiz Quiz

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.

  1. Profile Activation Basics

    Which property should be set to activate the 'dev' profile in a Spring Boot application?

    1. profile.activate=dev
    2. spring.profiles.active=dev
    3. profiles.set=dev
    4. spring.active=dev

    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.

  2. Profile-Specific Properties File

    If you want to provide database settings only for the 'prod' profile, which file should you use?

    1. application-prod.properties
    2. database-prod.conf
    3. application.properties
    4. prod-application.properties

    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.

  3. Default Profile Usage

    What is the default active profile in a Spring Boot application if no profile is set?

    1. default
    2. local
    3. prod
    4. dev

    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.

  4. Specifying Multiple Profiles

    How can you activate both 'dev' and 'qa' profiles at the same time?

    1. spring.profile.active=dev-qa
    2. profiles.active=dev+qa
    3. activate.profiles=dev qa
    4. spring.profiles.active=dev,qa

    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.

  5. Environment Variable Overriding

    Which source has the highest precedence when overriding properties: environment variable, application.properties, or command-line argument?

    1. System property file
    2. application.properties
    3. Environment variable
    4. 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.

  6. @Profile Annotation Placement

    Where should the @Profile annotation be used to conditionally enable a bean for the 'test' profile?

    1. On the bean class or method
    2. On property file
    3. On main application class only
    4. On package declaration

    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.

  7. Profile Property Override

    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?

    1. The value from application-staging.properties
    2. The value from application.properties
    3. The value is undefined
    4. Both values are merged

    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.

  8. Including Additional Profiles

    Which property allows adding extra profiles on top of those set in spring.profiles.active?

    1. spring.include.profiles
    2. spring.profiles.include
    3. profiles.additional
    4. profile.include.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.

  9. Reading Environment Properties

    What method allows accessing the value of an environment property in an application component?

    1. Calling Profile.get()
    2. Using Environment.getProperty()
    3. Applying @ConfigSettings
    4. Setting @PropertyValue

    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.

  10. Profile Detection in Code

    Which method checks if a specific profile is currently active at runtime in a component?

    1. Profile.activateNow()
    2. Environment.acceptsProfiles()
    3. SpringProfile.active()
    4. isActiveProfile()

    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.