[Spring] Provider란?

민스킴·2023년 9월 13일

Spring

목록 보기
2/14

프로바이더는 주로 서로 다른 스코프를 가진 스프링 빈의 의존관계를 해결하기 위해서 사용합니다. 순환 의존관계를 가진 빈의 문제도 해결할 수 있습니다.

예를 들어 싱글톤 빈이 프로토타입이나 리퀘스트 빈을 의존하는 경우 프로바이더를 사용해서 필요한 시점에 DL(의존관계 조회)를 한 뒤에 주입 받을 수 있습니다. 또는 A가 B를 의존하고 B가 A를 의존하는 순환 의존 관계에서도 프로바이더를 사용하면 순환 의존 문제를 해결할 수 있어요.

사용 예제

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
@Scope(value = "prototype")
public class PrototypeBean {
    @PostConstruct
    public void init(){
        System.out.println("PrototypeBean.init");
    }

    public void logic(){
        System.out.println(this);
    }

}


참고: https://velog.io/@suk13574/spring-provider

profile
Boys, be ambitious!

0개의 댓글