Bird
Raised Fist0
Spring Bootframework~20 mins

@Profile for environment-specific beans in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Profile Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What bean is loaded with active profile 'dev'?
Given these two Spring beans with @Profile annotations, which bean will be loaded when the active profile is set to 'dev'?
Spring Boot
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"; }
}
ABoth DevService and ProdService beans are loaded.
BProdService bean is loaded and DevService is ignored.
CDevService bean is loaded and ProdService is ignored.
DNo beans are loaded because profiles conflict.
Attempts:
2 left
💡 Hint
Think about how @Profile controls bean loading based on active profiles.
📝 Syntax
intermediate
2:00remaining
Which @Profile annotation syntax is correct?
Which option shows the correct way to use @Profile to specify multiple profiles for a bean in Spring Boot?
A@Profile(dev | test)
B@Profile({"dev", "test"})
C@Profile(dev, test)
D@Profile("dev, test")
Attempts:
2 left
💡 Hint
Check how arrays are passed in Java annotations.
🔧 Debug
advanced
2:00remaining
Why does this bean not load despite active profile?
Given this bean definition and active profile 'prod', why does Spring fail to load the bean?
Spring Boot
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

@Component
@Profile("production")
public class PaymentService {
    // service code
}
ABecause the active profile is 'prod' but the bean is annotated with 'production'.
BBecause @Component is missing.
CBecause the class is not public.
DBecause @Profile cannot be used on classes.
Attempts:
2 left
💡 Hint
Check if the profile names match exactly.
❓ state_output
advanced
2:00remaining
What is the output when no active profile is set?
Consider these two beans with @Profile annotations. What happens if no active profile is set in the Spring Boot application?
Spring Boot
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"; }
}
AOnly ProdConfig bean is loaded by default.
BBoth DevConfig and ProdConfig beans are loaded.
COnly DevConfig bean is loaded by default.
DNeither DevConfig nor ProdConfig beans are loaded.
Attempts:
2 left
💡 Hint
Think about what happens when no profile is active and beans require profiles.
🧠 Conceptual
expert
2:00remaining
How to load a bean for all profiles except 'test'?
Which @Profile expression correctly loads a bean for all profiles except 'test' in Spring Boot?
A@Profile("!test")
B@Profile("all & !test")
C@Profile("!test") and @Profile("*")
D@Profile("!test") and @Profile("default")
Attempts:
2 left
💡 Hint
Spring supports negation in @Profile expressions.

Practice

(1/5)
1. What is the main purpose of the @Profile annotation in Spring Boot?
easy
A. To define the database connection URL
B. To schedule tasks at fixed intervals
C. To create beans only for specific environments
D. To handle HTTP requests in controllers

Solution

  1. Step 1: Understand the role of @Profile

    The @Profile annotation is used to mark beans that should only be created when a specific environment profile is active.
  2. Step 2: Compare with other options

    Options A, C, and D relate to other Spring Boot features but not to environment-specific bean creation.
  3. Final Answer:

    To create beans only for specific environments -> Option C
  4. Quick Check:

    @Profile controls bean creation per environment [OK]
Hint: Remember: @Profile controls which beans load per environment [OK]
Common Mistakes:
  • Confusing @Profile with @Component or @Service
  • Thinking @Profile sets database URLs
  • Assuming @Profile schedules tasks
2. Which of the following is the correct way to use @Profile on a Spring bean class for the "dev" environment?
easy
A. @Profile("dev") public class DevService {}
B. @Profile(dev) public class DevService {}
C. @Profile({dev}) public class DevService {}
D. @Profile(["dev"]) public class DevService {}

Solution

  1. Step 1: Check correct syntax for @Profile

    The @Profile annotation requires a string value in quotes, e.g., @Profile("dev").
  2. Step 2: Analyze each option

    @Profile(["dev"]) public class DevService {} uses square brackets, invalid Java annotation syntax. @Profile(dev) public class DevService {} misses quotes around "dev", syntax error. @Profile({dev}) public class DevService {} uses braces but misses quotes around "dev", syntax error. @Profile("dev") public class DevService {} uses correct quoted string syntax.
  3. Final Answer:

    @Profile("dev") public class DevService {} -> Option A
  4. Quick Check:

    @Profile needs quoted string [OK]
Hint: Use quotes around profile names in @Profile [OK]
Common Mistakes:
  • Omitting quotes around profile names
  • Using square brackets instead of curly braces
  • Confusing array syntax with string
3. Given the following Spring Boot configuration and beans, what will be the output when the active profile is set to "prod"?
@Profile("dev")
@Component
public class DevBean {
    public String getMessage() { return "Development Bean"; }
}

@Profile("prod")
@Component
public class ProdBean {
    public String getMessage() { return "Production Bean"; }
}

@Component
public class BeanUser {
    private final Object bean;
    public BeanUser(@Autowired(required = false) Object bean) { this.bean = bean; }
    public String show() { return bean == null ? "No Bean" : ((ProdBean)bean).getMessage(); }
}
medium
A. Development Bean
B. Production Bean
C. ClassCastException at runtime
D. No Bean

Solution

  1. Step 1: Identify active profile and beans created

    With profile "prod" active, only ProdBean is created. DevBean is not created.
  2. Step 2: Analyze BeanUser constructor injection

    BeanUser tries to autowire an Object bean with @Autowired(required = false). Spring injects the available ProdBean instance as it matches.
  3. Step 3: Check the show() method

    In show(), bean is cast to ProdBean, which succeeds, returning "Production Bean".
  4. Final Answer:

    Production Bean -> Option B
  5. Quick Check:

    Prod profile loads ProdBean successfully [OK]
Hint: Profile determines available beans for injection [OK]
Common Mistakes:
  • Thinking Object type causes injection failure
  • Expecting ClassCastException with matching profile
  • Confusing which beans load per profile
4. You have two beans annotated with @Profile("test") and @Profile("prod") respectively. When running the app with no active profile set, you get a NoSuchBeanDefinitionException. What is the most likely cause?
medium
A. The beans are not annotated with @Component
B. The beans have syntax errors in @Profile annotation
C. Spring Boot does not support @Profile annotation
D. No profile is active, so no beans with @Profile are created

Solution

  1. Step 1: Understand @Profile behavior with no active profile

    If no profile is active, Spring does not create beans annotated with any specific profile.
  2. Step 2: Analyze exception cause

    NoSuchBeanDefinitionException means Spring could not find a bean to inject because none were created due to missing active profile.
  3. Final Answer:

    No profile is active, so no beans with @Profile are created -> Option D
  4. Quick Check:

    No active profile means no profile-specific beans [OK]
Hint: Beans with @Profile need active profile to load [OK]
Common Mistakes:
  • Assuming beans load without active profile
  • Blaming syntax errors without checking profiles
  • Ignoring need for @Component on beans
5. You want to create a bean that is active in both "dev" and "test" profiles but not in "prod". Which @Profile annotation usage correctly achieves this?
hard
A. @Profile({"dev", "test"})
B. @Profile("dev & test")
C. @Profile("dev | test")
D. @Profile({"dev & test"})

Solution

  1. Step 1: Understand @Profile multiple values usage

    Using @Profile({"dev", "test"}) means the bean is active if either "dev" OR "test" profile is active.
  2. Step 2: Analyze other options

    @Profile("dev & test") and D use string with "&" which is invalid syntax for multiple profiles. @Profile("dev | test") uses "|" inside string which is not supported syntax.
  3. Final Answer:

    @Profile({"dev", "test"}) -> Option A
  4. Quick Check:

    Use array of profiles for OR condition in @Profile [OK]
Hint: Use array syntax for multiple profiles in @Profile [OK]
Common Mistakes:
  • Using string with & or | inside @Profile
  • Confusing AND and OR logic in profiles
  • Forgetting to use curly braces for multiple profiles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard