Spring Boot Core Container and Beans Questions and Answers — Questions and Answers
Question 1: Which of the following is the default scope for a Spring bean if no scope is explicitly specified?
- request
- prototype
- singleton (Correct answer)
- session
Correct answer: singleton
In the Spring Framework, if no scope is specified for a bean, it defaults to the 'singleton' scope. This means the Spring IoC container creates exactly one instance of the bean, and that single instance is shared for all requests for that bean.
Question 2: A developer needs to create a new instance of a `ShoppingCart` bean every time a user starts a new HTTP session. Which bean scope should be used?
- prototype
- session (Correct answer)
- request
- singleton
Correct answer: session
The 'session' scope creates a new bean instance for the lifetime of a single HTTP Session. This is ideal for stateful beans like a user's shopping cart, where the state is tied to a specific user's session.
Question 3: What is a primary difference between the `ApplicationContext` and `BeanFactory` IoC containers in Spring?
- `BeanFactory` supports annotation-based configuration while `ApplicationContext` only supports XML.
- `ApplicationContext` is a sub-interface of `BeanFactory` and provides more advanced, enterprise-specific features like event publication and internationalization. (Correct answer)
- `BeanFactory` eagerly initializes all singleton beans at startup, while `ApplicationContext` uses lazy initialization.
- `ApplicationContext` is considered a lightweight container, whereas `BeanFactory` is heavyweight.
Correct answer: `ApplicationContext` is a sub-interface of `BeanFactory` and provides more advanced, enterprise-specific features like event publication and internationalization.
The `ApplicationContext` is a complete superset of the `BeanFactory`. It extends `BeanFactory` to provide more enterprise-level features such as event publication, message resource handling (for internationalization), and easier integration with Spring's AOP features. `ApplicationContext` typically loads all singleton beans eagerly at startup, whereas `BeanFactory` loads them lazily on demand.
Question 4: In a Spring Boot application, which annotation is considered the most generic stereotype for marking a class as a Spring-managed component?
- @Service
- @Repository
- @Controller
- @Component (Correct answer)
Correct answer: @Component
@Component is the generic stereotype annotation for any Spring-managed component. @Service, @Repository, and @Controller are specializations of @Component for more specific use cases in the service, persistence, and presentation layers, respectively.
Question 5: A developer wants to ensure their dependency injection code is portable and not tightly coupled to the Spring Framework. Which annotation should they prefer for dependency injection?
- @Autowired
- @Resource
- @Inject (Correct answer)
- @Bean
Correct answer: @Inject
The `@Inject` annotation is part of the Java Dependency Injection standard (JSR-330). Using `@Inject` over Spring's specific `@Autowired` annotation makes the code more portable across different dependency injection frameworks that support the standard, like Google Guice.
Question 6: What is the primary role of the Inversion of Control (IoC) container in the Spring Framework?
- To manage database transactions and connections directly.
- To handle incoming HTTP requests and route them to controllers.
- To compile Java source code into bytecode for the JVM.
- To manage the lifecycle and configuration of application objects (beans), including their creation and dependency injection. (Correct answer)
Correct answer: To manage the lifecycle and configuration of application objects (beans), including their creation and dependency injection.
The core responsibility of the Spring IoC container is to manage the entire lifecycle of objects, known as beans. This includes instantiating the beans, configuring them, assembling their dependencies (Dependency Injection), and managing them through their complete lifecycle.
Which of the following is the default scope for a Spring bean if no scope is explicitly specified?