inflearn logo
강의

講義

知識共有

Spring核心原理 - 基本編

9 - 프로토 타입 스코프를 싱글톤 빈과 함께 사용시 문제점 강의중 질문

288

15roa11211528

投稿した質問数 2

1

질문 1. 아래코드에서 ClientBean이 내부 메서드에서 prototypeBean을 사용할 수 있는 이유?

static class ClientBean{ ... }의 코드에서는 스프링 빈으로 prototypeBean을 등록하는 부분이 없는데 어떻게 logic()메소드에서 prototypeBean.addCount();를 사용할 수 있는 것인가요?

 

🤔 제 생각에는 void singletonClientUsePrototype()의 코드인

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class); 

으로 빈으로 등록이 되는 것 같아서 코드를 다음과 같이 수정 후 실행해 보았습니다.

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class); 

👉 그러자 실행이 안되는 것을 확인 할 수 있었습니다.

 

질문 2. singletonClientUsePrototype()메소드에서 등록된 빈이 ClientBean에 어떻게 영향을 주는 것인가요?

🤔 ClientBean 코드의 ApplicationContext applicationContext;의 의존성 주입을 통해 이루어지는 것 같은데 이 코드에서는 어떤 것이 applicationContext에 해당되는 것인가요..? 따로 해당 인스턴스타입으로 등록된 빈이 없는 것 같은데 헷갈립니다ㅜㅜ

 

public class SingletonWithPrototypeTest1 {

  @Test
  void singletonClientUsePrototype(){
    AnnotationConfigApplicationContext ac =
            new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);
    ClientBean client1 = ac.getBean(ClientBean.class);
    int count1 = client1.logic();
    assertThat(count1).isEqualTo(1);
    System.out.println("count1 = " + count1);

    ClientBean client2 = ac.getBean(ClientBean.class);
    int count2 = client2.logic();
    System.out.println("count2 = " + count2);
    assertThat(count2).isEqualTo(1);
  }

  @Scope("singleton")
  static class ClientBean {

    @Autowired
    ApplicationContext applicationContext;

    public int logic() {
      PrototypeBean prototypeBean = applicationContext.getBean(PrototypeBean.class);
      prototypeBean.addCount();
      return prototypeBean.getCount();
    }
  }



  @Scope("prototype")
  static class PrototypeBean {
    private int count = 0;

    public void addCount() {
      count++;
    }

    public int getCount() {
      return count;
    }

    @PostConstruct
    public void init() {
      System.out.println("PrototypeBean.init" + this);
    }

    @PreDestroy
    public void destroy() {
      System.out.println("PrototypeBean.destroy");
    }

  }
}

spring 객체지향 프로토타입 싱글톤타입

回答 1

0

yh

인프런 질문

안녕하세요. 진서현님

1. ClientBean의 생상자를 보시면 @Autowired가 붙어있는 것을 확인할 수 있습니다. 이렇게 하면 ClientBean을 생성할 때 스프링 빈에 등록된 PrototypeBean도 함께 생성자에 전달하게 됩니다.

 

2. 다음 코드를 통해서 ApplicationContext applicationContext에 해당하는 스프링 컨테이너가 생성됩니다. 다음 코드의 부모 인터페이스가 바로 ApplicationContext applicationContext입니다.

AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);

 

그리고 다음 코드와 같이 @Autowired를 사용하게 되면 스프링 컨테이너 자신을 주입하게 됩니다.

@Scope("singleton")

static class ClientBean {

@Autowired

ApplicationContext applicationContext;

}

감사합니다.

코드 자료

0

26

2

구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?

0

51

2

MemberService의 인터페이스를 왜 사용하는지 궁금합니다.

0

74

1

롬복 @Setter를 써야 하는 상황이 있는건가요?

0

89

1

빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?

0

81

1

테스트 속도가 나중에 영향이 있을까요?

0

77

1

gradle 설정 안떠서 질문 남깁니다!

0

121

2

build.gradle로 프로젝트를 여는 이유

0

85

1

provider 사용하는 이유

0

88

1

다음 강의 뭘 들어야 할까요

0

126

2

프로토타입 빈, 직접 destroy 호출 안 할 경우

0

64

1

beanB

0

82

2

퀴즈다시풀기

0

66

1

Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ

0

92

2

"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.

0

65

3

run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>

0

105

2

도메인의 정의?

0

59

1

ApplicationContext 질문입니다.

0

62

1

@Scope의 proxyMode를 사용할때 단위 테스트 방법

0

88

2

ai api 선정하기 관련 질문

0

118

2

생성자 자동주입 관련해서

0

64

1

생성자 직접 호출 vs 팩토리 메서드 패턴

0

96

2

Spring에서 SessionScope와 RequestScope는 함께 사용되나요?

1

65

1

12:25

0

77

2