Spring is a popular Java framework that simplifies the development of enterprise applications. At the core of Spring are two powerful concepts: Dependency Injection (DI) and Inversion of Control (IoC). Let's explore these concepts and understand how they power Spring applications.
What is Dependency Injection (DI) and Inversion of Control (IoC)?
Dependency Injection (DI) is a design pattern that allows the creation and management of object dependencies to be handled by a framework or container, rather than being manually instantiated by the developer. It promotes loose coupling between components.
Inversion of Control (IoC) is a broader concept where the control of object creation and dependency management is transferred from the application code to the Spring IoC container.
In simple terms, IoC delegates the responsibility of managing objects and their dependencies to the framework, and DI is the mechanism by which this is achieved.
There is a presentation using classes to represent springioccontainer, dependency, and application objects :
Using Spring Annotations: @Autowired, @Component, @Service, and @Repository
@Component
: Marks a class as a Spring-managed component. It can be automatically detected during classpath scanning.@Service
: A specialized version of@Component
used to annotate service classes.@Repository
: Another specialization of@Component
, used for persistence or DAO (Data Access Object) classes. It also translates exceptions into Spring's DataAccessException.@Autowired
: Used to inject dependencies into a class automatically. It can be applied to constructors, fields, or setters.
Example:
@Component public class MyComponent { private final MyService myService; @Autowired public MyComponent(MyService myService) { this.myService = myService; } }
Constructor Injection vs Setter Injection
Spring supports two primary types of DI:
- Constructor Injection: Dependencies are provided through the class constructor. This approach ensures that the object is created in a fully initialized state.
- Setter Injection: Dependencies are set using setter methods. This allows for optional dependencies but may lead to partially initialized objects.
Example of Constructor Injection:
@Component public class ConstructorInjected { private final MyService myService; @Autowired public ConstructorInjected(MyService myService) { this.myService = myService; } }
Example of Setter Injection:
@Component public class SetterInjected { private MyService myService; @Autowired public void setMyService(MyService myService) { this.myService = myService; } }
Bean Scopes and Lifecycle
Spring manages beans within the IoC container. The scope of a bean defines how it is created and shared:
singleton
(default): A single instance is created and shared across the application context.prototype
: A new instance is created each time the bean is requested.request
: A new instance is created per HTTP request (used in web applications).session
: A new instance is created per HTTP session (used in web applications).
Example of defining bean scope:
@Component @Scope("prototype") public class PrototypeBean { // Bean logic }
Custom Bean Configuration
Sometimes, beans require custom initialization or specific configurations. This can be achieved using Java-based configuration with the @Configuration
and @Bean
annotations.
Example:
@Configuration public class AppConfig { @Bean public MyService myService() { return new MyServiceImpl(); } }
Conclusion
Dependency Injection (DI) and Inversion of Control (IoC) are fundamental concepts that enable Spring to manage objects and their dependencies efficiently. By understanding annotations, injection types, bean scopes, and custom configurations, developers can harness the full power of Spring to build robust and maintainable applications.
0 Comments