Spring IoC Container
spring-ioc
1
Container
3
Male avatar

loveprogramming viết ngày 28/04/2021

https://grokonez.com/spring-framework/spring-ioc-container

Spring IoC Container

At the Core of Spring Framework, Spring IoC (Inversion of Control) is implemented using the Dependency Injection design pattern. The Spring IoC Container creates, configures and connects the objects (which are called Beans), then manage their life cycle.

This tutorial introduce principle of Spring IoC and a simple example using Containers to you.

I. Technology

  • Java 1.8
  • Maven 3.3.9
  • Spring Tool Suite – Version 3.8.1.RELEASE
  • Spring Boot: 1.4.0.RELEASE

    II. Overview

    1. Container & Dependency Injection

    The container helps inject the dependencies. Let's see an example: We want to provide a CustomerService with 2 implementations of CustomerPackage: BasicPackage and BusinessPackage. ioc-coupled This is content of CustomerService.java

public class CustomerService {
    private CustomerPackage customerPackage = new BasicPackage(); //or new BusinessPackage()

    public String callSupport() {
        return customerPackage.support();
    }
}

Because we create a CustomerPackage instance in the class CustomerService, it is tightly coupled. So if we want to change BasicPackage to BusinessPackage, we need to modify:


private CustomerPackage customerPackage = new BusinessPackage();

To avoid this kind of coupling, Spring Framework has a strong container to manage components. Instead of hard coding by creating new instance inside, we have the container inject the dependencies, by using setter injection or constructor injection.
Content of CustomerService.java after using Dependency Injection

More at:

https://grokonez.com/spring-framework/spring-ioc-container

Spring IoC Container

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 9 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 7 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 7 tháng trước
1 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!