Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Spring] - How to register bean in spring container and how container find injection location? #166

Open
Irisation23 opened this issue May 15, 2023 Discussed in #156 · 0 comments
Assignees
Labels
Java Java 관련 라벨입니다.

Comments

@Irisation23
Copy link
Member

Discussed in https://github.com/orgs/cs-collections/discussions/156

Originally posted by bunsung92 April 24, 2023
해당 Disscussions에서는 Spring ContainerBean RegistryDI의 내부 동작에 대해 알아보도록 한다.


1. Spring Container

  • 스프링 컨테이너에서 빈은 BeanDefinition 객체들로 정의해두고 객체를 생성한다.
  • Bean 생성시 BeanDefinition 정의에 따라 객체 생성에 대한 정보를 참조하며, 일반적으로 리플렉션을 통해 객체를 생성한다.

그리고

  • 컨테이너가 빈을 생성할 때, Service-locator 패턴(서비스 중개자 브로커)과 비슷한 매커니즘을 통해 의존성을 주입하며 생성한다. 디커플링이 주된 목적
  • 객체를 사용하는곳에서 직접 생성해 객체 간 강결합을 갖는것이 아닌 외부(Container)에서 생성된 객체를 주입 함으로써 결합도를 낮추는 효과를 가질 수 있게 된다.

짧 정리

Spring Container에 등록된 빈들의 객체는 리플렉션을 통해 객체를 생성한다.
DI의 주된 목적은 객체간 강결합을 끊기 위해 컨테이너를 사용한다.


2. How To Injection?

위에서 정리된 내용처럼 Applicaiton Context 의 구현체중 적절한 구현체가 선택되면
해당 구현체가 빈을 가져오고 주입시키는데 이때 Reflection을 통해 구현된다.

스프링의 빈 주입에는

  1. 생성자 주입 (Constructor Injection)
  2. 필드 주입 (Field Injection)
  3. 수정자 주입 (Setter Injection)

으로 정리 할 수 있다.

해당 주입에 대한 자세한 정리는
Spring 생성자 주입 vs 필드 주입 글을 읽어보며 정리하길 바란다.

번외로 해당 주제의 깊은 내용을 알아 보고 싶다면?
https://taes-k.github.io/2021/05/23/spring-di-reflection/

@Irisation23 Irisation23 added the Java Java 관련 라벨입니다. label May 15, 2023
@Irisation23 Irisation23 self-assigned this May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java Java 관련 라벨입니다.
Projects
None yet
Development

No branches or pull requests

1 participant