Home  >  Article  >  Java  >  How does dependency injection work in Spring Framework?

How does dependency injection work in Spring Framework?

WBOY
WBOYOriginal
2024-04-17 15:57:01449browse

Dependency injection (DI) in Spring Framework is implemented through an IoC container, which is responsible for managing object instances and injecting their dependencies. There are two approaches to DI: using constructors or field injection to inject dependencies in an automatic or explicit way, thus achieving loose component coupling and maintainability.

Spring Framework 中的依赖注入是如何工作的?

#How does dependency injection work in Spring Framework?

Dependency Injection (DI) is a fundamental feature in Spring Framework that allows components to obtain their dependencies without explicitly creating instances.

How DI works

DI works through an IoC (Inversion of Control) container, which is responsible for creating and managing instances of objects. When the container creates an object, it injects the required dependencies into the object.

Methods to implement DI

The Spring framework implements DI through two main methods:

  • Constructor injection: Dependent objects are injected into the target object through the constructor.
  • Field injection: Dependent objects are directly injected into the target object through fields.

Practical case: using constructor injection

The following is an example of using constructor injection:

public class UserService {

    private UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    // ...业务逻辑代码...
}

In this example, The UserService class accepts UserRepository dependencies through the constructor. The Spring container is responsible for creating an instance of UserService and injecting UserRepository.

Practical case: using field injection

The following is an example of using field injection:

public class OrderService {

    @Autowired
    private OrderRepository orderRepository;

    // ...业务逻辑代码...
}

In this example, OrderService The class uses the @Autowired annotation to inject the OrderRepository dependency into the orderRepository field. The Spring container is responsible for finding and injecting OrderRepository instances.

Conclusion

Through dependency injection, Spring Framework achieves loose coupling between components, improving the testability and maintainability of the code. Understanding how DI works is critical to developing robust and scalable Spring applications.

The above is the detailed content of How does dependency injection work in Spring Framework?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn