Inflearn brand logo image

Inflearn Community Q&A

pien's profile image
pien

asked

Spring Core Principles - Basic Edition

Singleton Caveats

@Configuration

Written on

·

336

1

안녕하세요 선생님,

수업 pdf 75페이지 @Configuration 질문입니다.

여기서 TestConfig 클래스는 @Configuration 어노테이션을 붙이지 않아도 new AnnotationConfigApplicationContext에서 구성정보로 잘 들어가는데, 왜 안붙여도 정상작동 하나요?

@Configuration 어노테이션을 붙이지 않는 이유가 있을까요?

package hello.core.singleton;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
public class StatefulServiceTest {
    @Test
    void statefulServiceSingleton() {
    ApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
    StatefulService statefulService1 = ac.getBean("statefulService", StatefulService.class);
    StatefulService statefulService2 = ac.getBean("statefulService", StatefulService.class);
    //ThreadA: A사용자 10000원 주문
    statefulService1.order("userA", 10000);
    //ThreadB: B사용자 20000원 주문
    statefulService2.order("userB", 20000);
    //ThreadA: 사용자A 주문 금액 조회
    int price = statefulService1.getPrice();
    //ThreadA: 사용자A는 10000원을 기대했지만, 기대와 다르게 20000원 출력
    System.out.println("price = " + price);
    Assertions.assertThat(statefulService1.getPrice()).isEqualTo(20000);
    }
    static class TestConfig {
        @Bean
        public StatefulService statefulService() {
            return new StatefulService();
}
}
}
oopspring

Answer 1

0

yh님의 프로필 이미지
yh
Instructor

안녕하세요. 피엔님

다음 질문을 참고해주세요^^

https://www.inflearn.com/questions/191114

https://www.inflearn.com/questions/186890

감사합니다.

pien's profile image
pien

asked

Ask a question