Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[스프링AOP실무주의사항] 내부호출 - 지연조회 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

[스프링AOP실무주의사항] 내부호출 - 지연조회

Seung__ 2022. 1. 11. 22:49

앞서 생성자 주입이 실패하는 이유는 자기 자신을 생성하면서 주입해야 하기 때문이다. 

이 경우 수정자 주입을 사용하거나 지금부터 설명하는 지연 조회를 사용하면 된다.

스프링 빈을 지연해서 조회하면 되는데, ObjectProvider(Provider) , ApplicationContext 를 사용

 

1. ApplicationContext


@Slf4j
@Component
public class CallServiceV2 {

    private final ApplicationContext applicationContext;

    public CallServiceV2(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }


    public void external(){
        log.info("call external");
        CallServiceV2 callServiceV2 = applicationContext.getBean(CallServiceV2.class);
        callServiceV2.internal(); //외부 메서드 호출
    }

    public void internal(){
        log.info("call internal");
    }
}
  • ApplicationContext 는 너무 많은 기능을 제공한다.
  • 여기서 사용할 기능은 단순히 빈은 지연조회하는 것이다. 흠..
@Slf4j
@Import(CallLogAspect.class)
@SpringBootTest
class CallServiceV2Test {

    @Autowired CallServiceV2 callServiceV2;

    @Test
    void external() {
        callServiceV2.external();
    }

    @Test
    void internal() {
        callServiceV2.internal();
    }
}
aop=void hello.aop.internalcall.CallServiceV2.external()
call external
aop=void hello.aop.internalcall.CallServiceV2.internal()
call internal

 

 

2. ObjectProvider


  • ObjectProvider : objectProvider에 요청 하면, 스프링 컨테이너에 요청해서 해당 타입 빈 가져옴.
 

Provider, ObjectProvider

싱글톤 빈과 프로토 빈 함께 사용 시 발생하는 문제점 해결해보자. 이 전 글에서 생긴 문제점은 다음과 같다. 프로토 타입 빈은 요청할 때 마다 새로 생성하고 싶다. 하지만 싱글톤 빈 생성시 프

bsh-developer.tistory.com

@Slf4j
@Component
public class CallServiceV2 {

    //private final ApplicationContext applicationContext;
    private final ObjectProvider<CallServiceV2> callServiceV2ObjectProvider;

    public CallServiceV2(ObjectProvider<CallServiceV2> callServiceV2ObjectProvider) {
        this.callServiceV2ObjectProvider = callServiceV2ObjectProvider;
    }
    

    public void external(){
        log.info("call external");
        //CallServiceV2 callServiceV2 = applicationContext.getBean(CallServiceV2.class);
        CallServiceV2 callServiceV2 = callServiceV2ObjectProvider.getObject();
        callServiceV2.internal(); //외부 메서드 호출
    }

    public void internal(){
        log.info("call internal");
    }
}
  • ObjectProvider 는 객체를 스프링 컨테이너에서 조회하는 것을 스프링 빈 생성 시점이 아니라 실제
    객체를 사용하는 시점으로 지연할 수 있다.
  • callServiceProvider.getObject() 를 호출하는 시점에 스프링 컨테이너에서 빈을 조회한다.
  • 여기서는 자기 자신을 주입 받는 것이 아니기 때문에 순환 사이클이 발생하지 않는다.
@Slf4j
@Import(CallLogAspect.class)
@SpringBootTest
class CallServiceV2Test {

    @Autowired CallServiceV2 callServiceV2;

    @Test
    void external() {
        callServiceV2.external();
    }

    @Test
    void internal() {
        callServiceV2.internal();
    }
}
aop=void hello.aop.internalcall.CallServiceV2.external()
call external
aop=void hello.aop.internalcall.CallServiceV2.internal()
call internal

 

3. GitHub: 220111 AOP & ObjectProvider


 

GitHub - bsh6463/SpringAOP

Contribute to bsh6463/SpringAOP development by creating an account on GitHub.

github.com

 

Comments