Spring Framework Professional Standards & Competencies 2 — Questions and Answers
Question 1: Which Spring annotation should a professional use to define a configuration class that replaces XML-based bean definitions?
- @Component
- @Configuration (Correct answer)
- @Service
- @Bean
Correct answer: @Configuration
@Configuration marks a class as a source of bean definitions and is processed by Spring's container at startup.
Question 2: A Spring developer needs to ensure a bean is created only when a specific property is set. Which annotation is the professional standard?
- @Profile
- @ConditionalOnBean
- @ConditionalOnProperty (Correct answer)
- @Lazy
Correct answer: @ConditionalOnProperty
@ConditionalOnProperty (Spring Boot) registers a bean only when the specified environment property exists and matches the expected value.
Question 3: What is the recommended professional practice when injecting dependencies in Spring to make unit testing easier?
- Field injection via @Autowired
- Setter injection
- Constructor injection (Correct answer)
- Static factory injection
Correct answer: Constructor injection
Constructor injection makes dependencies explicit, supports immutability, and allows easy unit testing without a Spring context.
Question 4: A team finds multiple beans of the same type in the context. Which annotation resolves the ambiguity according to Spring best practices?
- @Primary
- @Qualifier
- Both @Primary and @Qualifier (Correct answer)
- @Order
Correct answer: Both @Primary and @Qualifier
@Primary designates a default bean while @Qualifier targets a specific bean by name, and professionals use both depending on context.
Question 5: Which Spring module is the industry-standard choice for implementing RESTful web services in a Spring application?
- Spring Batch
- Spring Integration
- Spring Web MVC (Correct answer)
- Spring Shell
Correct answer: Spring Web MVC
Spring Web MVC (via @RestController and @RequestMapping) is the standard approach for building REST APIs in Spring applications.
Question 6: What professional practice should be followed when externalizing configuration in a Spring Boot application?
- Hard-code values in @Configuration classes
- Use application.properties or application.yml with @ConfigurationProperties (Correct answer)
- Store config in a static utility class
- Embed config in @Bean method bodies
Correct answer: Use application.properties or application.yml with @ConfigurationProperties
@ConfigurationProperties bound to application.properties/yml provides type-safe, testable externalized configuration.
Question 7: Which bean scope is the professional default in Spring and is suitable for stateless service components?
- prototype
- request
- session
- singleton (Correct answer)
Correct answer: singleton
Singleton scope is the default; one shared instance is created per application context, appropriate for stateless beans.
Which Spring annotation should a professional use to define a configuration class that replaces XML-based bean definitions?