Spring Framework Basics
Duration: 7 min
This module provides an introduction to the Spring Framework, a powerful and versatile framework that simplifies the development of Java applications. We will cover fundamental concepts such as dependency injection, inversion of control, and core components like Spring Beans and Spring Context. Understanding these basics is crucial for building robust enterprise-level applications.
Dependency Injection
Visual Guide: This module includes diagrams and flowcharts. Check the course materials for detailed visualizations.
Dependency Injection (DI) is a core concept in Spring that allows objects to be created with their dependencies provided from an external source rather than the objects creating their dependencies themselves. This promotes loose coupling and enhances testability and maintainability of the code. Spring supports various types of DI such as constructor injection, setter injection, and field injection.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
// Define a bean
public class MessageService {
public void sendMessage(String message) {
System.out.println("Sending message: " + message);
}
}
// Define a bean with a dependency
public class MessagingClient {
private MessageService messageService;
// Constructor injection
public MessagingClient(MessageService messageService) {
this.messageService = messageService;
}
public void send() {
messageService.sendMessage("Hello, Spring!");
}
}
// Main class to run the application
public class Main {
public static void main(String[] args) {
// Create the Spring application context
ApplicationContext context = new AnnotationConfigApplicationContext(Main.class);
// Retrieve the bean from the context
MessagingClient client = context.getBean(MessagingClient.class);
// Use the bean
client.send();
}
}Sending message: Hello, Spring!Spring Context
The Spring Context is the central interface in the Spring Framework that provides a way to manage beans and their lifecycle. It acts as a container that loads bean definitions, wires beans together, configures them, and manages their lifecycle. The context is responsible for instantiating, configuring, and assembling the beans, and managing their dependencies.
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// Configuration class
@Configuration
public class AppConfig {
// Define a bean
@Bean
public MessageService messageService() {
return new MessageService();
}
// Define a bean with a dependency
@Bean
public MessagingClient messagingClient() {
return new MessagingClient(messageService());
}
}
// Main class to run the application
public class Main {
public static void main(String[] args) {
// Create the Spring application context
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// Retrieve the bean from the context
MessagingClient client = context.getBean(MessagingClient.class);
// Use the bean
client.send();
}
}💡 Tip: When using Spring Context, ensure that your configuration classes are annotated with @Configuration and your beans are annotated with @Bean to properly define and manage them.
❓ What is the primary purpose of Dependency Injection in Spring?
❓ Which annotation is used to define a bean in Spring?