Intro to Spring Framework Flashcards
How does a framework differ from a library?
A framework dictates the overall architecture, controls the flow of the application, and imposes certain rules, while a library offers specific functionalities that developers can use as needed within their applications.
What is coherence in a software system?
Coherence refers to the state where all components of a software system work together seamlessly, maintaining consistency and uniformity in their behavior and interactions.
How can consistent coding practices contribute to coherence?
Adopting consistent coding standards, naming conventions, and design patterns across the software system ensures uniformity, readability, and easier collaboration among developers.
How does centralized data management contribute to system coherence?
Centralized data management ensures that all parts of the system access and modify data consistently, avoiding conflicts and discrepancies in information across different components.
What is Dependency Injection (DI)?
Dependency Injection is a design pattern where the dependencies of a component are provided or “injected” from the outside rather than created within the component itself.
What problem does Dependency Injection aim to solve?
Dependency Injection addresses tight coupling between components by removing their direct dependencies on other components or services, making them more reusable and easier to maintain.
What are the key components in Dependency Injection?
In DI, there are typically three main components: the client or consumer that requires a service or dependency, the service or dependency itself, and the injector that provides the dependency to the client.
What are the types of Dependency Injection?
There are primarily three types of DI: Constructor Injection, Setter Injection, and Interface Injection. Constructor Injection and Setter Injection are the most commonly used forms.
What are the benefits of Dependency Injection?
DI promotes code reusability, testability, and easier maintenance by reducing coupling between components, making the application more flexible and easier to extend or modify.
How is dependency achieved between classes through an interface in Java?
In Java, dependency between classes through an interface is established by having a class implement an interface, and another class consuming that interface.
What is an example of an interface in Java that defines a contract?
// Example of an interface defining a contract
public interface MessagingService {
void sendMessage(String message);
}
How can a class implement an interface in Java?
// Example of a class implementing an interface
public class EmailService implements MessagingService {
@Override
public void sendMessage(String message) {
// Implementation specific to sending emails
System.out.println(“Sending email: “ + message);
}
}
How can another class depend on the interface rather than the concrete implementation?
// Example of a class depending on the interface
public class NotificationService {
private MessagingService messagingService;
// Constructor or setter for dependency injection public NotificationService(MessagingService messagingService) { this.messagingService = messagingService; } public void sendNotification(String message) { // Using the interface method, unaware of the concrete implementation messagingService.sendMessage(message); } }
How does this approach aid in flexibility and extensibility?
By depending on the MessagingService interface, NotificationService can be easily switched to use different implementations (e.g., EmailService, SMSService) without modifying its code.
What is the Dependency Inversion Principle (DIP)?
The Dependency Inversion Principle is a design principle that states that high-level modules/classes should not depend on low-level modules/classes. Instead, both should depend on abstractions.
What are the two key aspects of Dependency Inversion?
Dependency Inversion involves two key aspects: Abstraction and Inversion of Control (IoC) (IoC - a framework doing DI on your behalf ), where abstractions are used to decouple high-level modules from concrete implementations, and IoC containers manage dependencies.
How does Dependency Inversion relate to interfaces and abstractions?
In DIP, interfaces or abstractions are used to define contracts between modules/classes, allowing different implementations to adhere to those contracts without affecting higher-level modules.
What benefits does Dependency Inversion offer in terms of testing?
Dependency Inversion facilitates easier unit testing by allowing the substitution of dependencies with mock objects or stubs, enabling isolated testing of individual components.
How are Spring Beans created and managed?
Spring Beans are created, configured, and managed by the Spring IoC container, which handles their lifecycle, dependencies, and instantiation based on the configuration provided.
How are Spring Beans defined in the Spring framework?
Spring Beans are typically defined using XML-based configuration, Java annotations, or through Java-based configuration classes, specifying their properties, dependencies, and behaviors.
How does the Spring IoC container manage dependencies between Spring Beans?
The Spring IoC container handles dependency injection, resolving and injecting dependencies into Spring Beans, promoting loose coupling and modularity.
Where are Spring Bean objects stored in a Spring application?
Spring Bean objects are stored in the Spring IoC (Inversion of Control) container, which manages their lifecycle, configurations, and dependencies.
What is the role of the Spring IoC container in managing Spring Bean objects?
The Spring IoC container stores, creates, configures, and manages Spring Bean objects, handling their instantiation, lifecycle, and providing them when requested by the application.
What are the two types of Spring IoC containers?
The Spring IoC container comes in two types: the BeanFactory and the ApplicationContext, offering different levels of functionalities and services.