Provider

2024. 8. 19. 17:30SPRING

Spring bean Scope 중, web 에 사용되는 Scope (request, session 등)가 존재한다.

 

request 의 경우, 클라이언트에서 요청이 들어와서 response 될 때 까지의 라이프 사이클을 갖는다.

 

그러나, 프로그램 실행 후 스프링 빈을 생성, 초기화하는 단계에서 request Scope는 존재할 수 없다. 요청이 들어와야 생성되기 때문이다.

 

이에 따라, bean 초기화 시점이 어긋나 에러가 발생한다. 이를 해결하기 위해서는, 해당 bean 이 생성 시기를 지연시켜야 한다. Provider 는 제네릭 클래스로, 개발자가 입력한 매개타입<T> 를 검색할 수 있다.

이 프로바이더를 request 스코프 빈 wrapper 로 사용하고, bean으로 취급한 후, 사용 시점에 getObejct 를 통해 해당 빈을 탐색, 생성, 반환하게 함으로써, request 스코프 빈의 라이프 사이클을 스프링 프레임워크와 일체화 시킬 수 있다.

 

그런데...

Provider 를 사용하면, 결국 언박싱 작업이 필요하게 된다.

로그 같은 작업은 여러 곳에서 쓰게 될텐데 너무 번거롭다.

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)

명석하고 현명하고 귀찮은 걸 사랑하는 개발자님들께서 @Scope 애노테이션에 proxymode 프로퍼티와 그 기능을 만들어주셨다.

package org.springframework.context.annotation;

/**
 * Enumerates the various scoped-proxy options.
 *
 * <p>For a more complete discussion of exactly what a scoped proxy is, see the
 * section of the Spring reference documentation entitled '<em>Scoped beans as
 * dependencies</em>'.
 *
 * @author Mark Fisher
 * @since 2.5
 * @see ScopeMetadata
 */
public enum ScopedProxyMode {

    /**
     * Default typically equals {@link #NO}, unless a different default
     * has been configured at the component-scan instruction level.
     */
    DEFAULT,

    /**
     * Do not create a scoped proxy.
     * <p>This proxy-mode is not typically useful when used with a
     * non-singleton scoped instance, which should favor the use of the
     * {@link #INTERFACES} or {@link #TARGET_CLASS} proxy-modes instead if it
     * is to be used as a dependency.
     */
    NO,

    /**
     * Create a JDK dynamic proxy implementing <i>all</i> interfaces exposed by
     * the class of the target object.
     */
    INTERFACES,

    /**
     * Create a class-based proxy (uses CGLIB).
     */
    TARGET_CLASS

}

 

Create a class-based proxy (uses CGLIB).

스프링 빈의 DI 단계에서 가짜(프록시) 를 만들어 주입 하게 한다.

가짜는 진짜를 찾아올 방법을 알고 있다.

사용되는 시점에 실제 빈을 찾아 동작하게 한다.

 

 

 

'SPRING' 카테고리의 다른 글

로그  (0) 2024.08.27
MVC 패턴 - FrontController?  (0) 2024.08.21
WAS  (0) 2024.08.11
데이터 전달 방식 - API  (0) 2024.08.04
build 해보기  (0) 2024.08.03