Challenge - 5 Problems
Spring Configuration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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"));
Attempts:
2 left
💡 Hint
Remember that @Bean methods define beans in the Spring context and return the object to be managed.
✗ Incorrect
The @Bean annotated method returns a String bean with the value "Hello from @Bean!". When retrieved from the context, it prints exactly that string.
📝 Syntax
intermediate2: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 }
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.
✗ Incorrect
Option A correctly defines a @Bean method returning a String. Option A lacks @Bean annotation. Option A returns void which is invalid for a bean. Option A has a parameter without Spring knowing how to inject it.
🔧 Debug
advanced2: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"); } }
Attempts:
2 left
💡 Hint
Parsing "abc" as an integer is invalid and throws an exception.
✗ Incorrect
Integer.parseInt("abc") throws NumberFormatException at runtime, causing the Spring context startup to fail.
🧠 Conceptual
advanced2:00remaining
What is the role of @Configuration in Spring Boot?
Choose the best description of what @Configuration does in a Spring Boot application.
Attempts:
2 left
💡 Hint
Think about what Spring does with classes annotated with @Configuration.
✗ Incorrect
@Configuration tells Spring that the class contains methods annotated with @Bean that define beans to be managed in the context.
❓ state_output
expert2: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; } }
Attempts:
2 left
💡 Hint
Each @Bean method is called once during context initialization in some order.
✗ Incorrect
Spring calls each @Bean method once. The first call to counter() increments count from 0 to 1 and returns 1. The second call to anotherCounter() increments count to 2. The bean 'counter' has value 1.