The Spring Framework is an open-source framework for building Java applications, especially enterprise-level ones. It simplifies the development process by promoting good design practices like Dependency Injection (DI) and Inversion of Control (IoC).
🧩 Key Concepts
Dependency Injection (DI):
- Instead of creating objects manually, Spring "injects" the necessary dependencies into your classes, making code more modular and easier to maintain.
Inversion of Control (IoC):
- The Spring Container takes over the responsibility of managing object creation and wiring dependencies, freeing developers from this task.
Beans:
- These are the objects that Spring manages in its container.
Spring Container:
- It creates, manages, and configures beans. The container reads configuration (XML, annotations, or Java classes) to understand how to create and link objects.
🚀 Why Use Spring?
Reduces Boilerplate Code:
- Simplifies common tasks like object creation and dependency management.
Modular and Flexible:
- Use only what you need (e.g., web, data, or security modules).
Easy Integration:
- Works well with other technologies like Hibernate, JPA, and REST APIs.
Robust and Scalable:
- Suitable for applications of all sizes, from simple apps to large enterprise systems.
@Component
, @Service
, @Repository
) or by using a Java configuration class with @Bean
annotations.Understanding Bean Registration
In Spring, a bean is an object that is instantiated, assembled, and managed by the Spring IoC container. To register a bean, you typically annotate a class with @Component
, @Service
, @Repository
, or @Controller
. These annotations are used to scan and automatically register beans in the Spring application context.
Using @Component
Annotation
The @Component
annotation is the most generic annotation for registering a bean. Here's an example:
@Component
public class MyBean {
// ...
}
Using @Service
, @Repository
, and @Controller
Annotations
These annotations are more specific and are often used to indicate the role of a bean in the application:
@Service
: For business logic components.@Repository
: For data access components.@Controller
: For web controllers.
@Service
public class MyService {
// ...
}
@Repository
public class MyRepository {
// ...
}
@Controller
public class MyController {
// ...
}
Using CommandLineRunner
to Execute Code on Startup
The CommandLineRunner
interface allows you to execute code after the Spring application context is fully initialized. It's often used for initialization tasks, data loading, or running specific operations on startup.
@Component
public class MyCommandLineRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
// Code to be executed on startup
System.out.println("Application started!");
}
}
Key Points to Remember:
- Component Scanning: Spring Boot automatically scans the classpath for components annotated with
@Component
and its stereotypes. - Bean Naming: If you don't specify a bean name, Spring will generate a default name based on the class name.
- Dependency Injection: Spring handles dependency injection automatically, injecting required dependencies into beans.
- Configuration Properties: You can use
@ConfigurationProperties
to bind configuration properties to beans. - Profiles: You can use profiles to conditionally activate beans based on environment or other criteria.
By understanding these concepts and leveraging the power of annotations, you can effectively register beans and customize the startup behavior of your Spring Boot applications.
Comments
Post a Comment