Bird
Raised Fist0
Spring Bootframework~20 mins

@Configuration and @Bean in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spring Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Spring Boot configuration?
Given the following Spring Boot configuration class, what will be printed when the application context is started and the messageService bean is used to print the message?
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    @Bean
    public String messageService() {
        return "Hello from @Bean!";
    }
}

// In main application or test:
// System.out.println(context.getBean("messageService"));
AHello from @Bean!
Bnull
CCompilation error due to missing @ComponentScan
DRuntime error: NoSuchBeanDefinitionException
Attempts:
2 left
💡 Hint
Remember that @Bean methods define beans in the Spring context and return the object to be managed.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines a @Bean method in a @Configuration class?
Select the option that correctly defines a Spring bean method inside a @Configuration class.
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfig {
    // Choose the correct method below
}
A
@Bean
public String greeting() {
    return "Hi!";
}
B
@Bean
public String greeting(String name) {
    return "Hi " + name;
}
C
@Bean
public void greeting() {
    System.out.println("Hi!");
}
D
public String greeting() {
    return "Hi!";
}
Attempts:
2 left
💡 Hint
A @Bean method must return an object to be managed by Spring and have no parameters unless they are other beans.
🔧 Debug
advanced
2:00remaining
Why does this @Bean method cause a runtime error?
Consider this @Configuration class: @Configuration public class Config { @Bean public Integer number() { return Integer.parseInt("abc"); } } What happens when the Spring context starts?
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Config {
    @Bean
    public Integer number() {
        return Integer.parseInt("abc");
    }
}
ABean 'number' is created with value 0
BBean 'number' is created with null value
CApplication context fails to start with NumberFormatException
DCompilation error due to invalid parse
Attempts:
2 left
💡 Hint
Parsing "abc" as an integer is invalid and throws an exception.
🧠 Conceptual
advanced
2:00remaining
What is the role of @Configuration in Spring Boot?
Choose the best description of what @Configuration does in a Spring Boot application.
A@Configuration is used to inject dependencies into beans
B@Configuration marks a class as a source of bean definitions for the application context
C@Configuration automatically scans all packages for components
D@Configuration replaces the need for @Component annotations
Attempts:
2 left
💡 Hint
Think about what Spring does with classes annotated with @Configuration.
❓ state_output
expert
2:00remaining
What is the value of the bean 'counter' after context initialization?
Given this configuration class: @Configuration public class CounterConfig { private int count = 0; @Bean public Integer counter() { return ++count; } @Bean public Integer anotherCounter() { return ++count; } } What is the value of the bean named 'counter' when retrieved from the context?
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CounterConfig {
    private int count = 0;

    @Bean
    public Integer counter() {
        return ++count;
    }

    @Bean
    public Integer anotherCounter() {
        return ++count;
    }
}
AThrows an exception due to bean initialization order
B2
C0
D1
Attempts:
2 left
💡 Hint
Each @Bean method is called once during context initialization in some order.

Practice

(1/5)
1. What is the main purpose of the @Configuration annotation in Spring Boot?
easy
A. To enable automatic component scanning
B. To mark a method that returns a bean instance
C. To mark a class as a source of bean definitions for the application context
D. To configure database connections automatically

Solution

  1. Step 1: Understand the role of @Configuration

    @Configuration tells Spring that this class contains methods to create beans.
  2. Step 2: Differentiate from other annotations

    @Bean marks methods, not classes; component scanning is done by @ComponentScan; database config is separate.
  3. Final Answer:

    To mark a class as a source of bean definitions for the application context -> Option C
  4. Quick Check:

    @Configuration = class for beans [OK]
Hint: Remember: @Configuration is for classes, @Bean is for methods [OK]
Common Mistakes:
  • Confusing @Configuration with @Bean
  • Thinking @Configuration enables scanning
  • Assuming it configures databases automatically
2. Which of the following is the correct way to define a bean method inside a @Configuration class?
easy
A. @Bean public MyBean myBean() { return new MyBean(); }
B. @Configuration public MyBean myBean() { return new MyBean(); }
C. public void createBean() { return new MyBean(); }
D. @Component public MyBean myBean() { return new MyBean(); }

Solution

  1. Step 1: Check method signature for bean creation

    Bean methods must be annotated with @Bean and return the bean object.
  2. Step 2: Verify syntax correctness

    @Bean public MyBean myBean() { return new MyBean(); } correctly uses @Bean and returns a new instance.
  3. Final Answer:

    @Bean public MyBean myBean() { return new MyBean(); } -> Option A
  4. Quick Check:

    @Bean + return bean instance = correct method [OK]
Hint: Bean methods need @Bean and must return the object [OK]
Common Mistakes:
  • Missing @Bean annotation
  • Using void return type
  • Using @Configuration on methods
3. Given the following code, what will be the output when retrieving the bean named "service" from the Spring context?
@Configuration
public class AppConfig {
  @Bean
  public String service() {
    return "MyService";
  }
}
medium
A. "service"
B. "MyService"
C. An exception is thrown
D. null

Solution

  1. Step 1: Identify the bean method and its return value

    The method named service returns the string "MyService" and is annotated with @Bean.
  2. Step 2: Understand bean retrieval by name

    Spring registers this bean with the name "service" and returns the method's return value when requested.
  3. Final Answer:

    "MyService" -> Option B
  4. Quick Check:

    Bean name returns method's return value [OK]
Hint: Bean name matches method name; returns method's return value [OK]
Common Mistakes:
  • Expecting bean name as output instead of return value
  • Thinking Spring returns null if no explicit name
  • Assuming exception without error in code
4. Identify the error in the following Spring configuration code:
@Configuration
public class Config {
  @Bean
  public void createBean() {
    // no return statement
  }
}
medium
A. Method must return an object, not void
B. Missing @Configuration annotation
C. Method name cannot be createBean
D. No error, code is correct

Solution

  1. Step 1: Check method return type for @Bean

    Bean methods must return the bean object; void return type is invalid.
  2. Step 2: Confirm annotations and method name

    @Configuration is present; method name can be anything; error is only return type.
  3. Final Answer:

    Method must return an object, not void -> Option A
  4. Quick Check:

    @Bean method needs return value, not void [OK]
Hint: Bean methods must return an object, never void [OK]
Common Mistakes:
  • Using void return type for @Bean methods
  • Confusing method name restrictions
  • Missing @Configuration annotation (not in this case)
5. You want to create two beans of the same type but with different configurations using @Bean methods inside a @Configuration class. How can you ensure Spring distinguishes between them when injecting?
hard
A. Define both beans in separate @Configuration classes only
B. Use @Primary on both methods
C. Use the same method name but different return types
D. Use different method names for each @Bean method

Solution

  1. Step 1: Understand bean naming in Spring

    By default, Spring uses the @Bean method name as the bean name to distinguish beans.
  2. Step 2: Differentiate beans of same type

    Using different method names creates distinct bean names, allowing injection by name or qualifier.
  3. Final Answer:

    Use different method names for each @Bean method -> Option D
  4. Quick Check:

    Different method names = different bean names [OK]
Hint: Different method names create distinct bean names automatically [OK]
Common Mistakes:
  • Using @Primary on both beans (conflicts)
  • Same method name with different return types (not allowed)
  • Thinking beans must be in separate classes
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