해결된 질문
작성
·
267
0
섹션 5의 '@Configuration과 싱글톤' 강의에서
AppConfig class에 @Configuration으로 어노테이션을 하면 'ConfigurationSingletonTest'에서 memoryMemberRepository의 constructor가 세번 호출 되는 것처럼 보이지만, 실제로는 한번 호출이 되어야하고, OrderServiceImpl, MemberServiceImpl, MemoryMemberRepository 빈에서의 memoryMemberRepository가 모두 같은 인스턴스여야한다고 하셨는데,
저는 constructor도 3번이 호출되고, 모두 다른 객체입니다ㅠ
또한
AppConfig bean = ac.getBean(AppConfig.class);
System.out.println("bean = " + bean.getClass());
하면
"bean = class hello.AppConfig$$EnhancerBySpringCGLIB$$~~~"
가 아닌
"bean = class hello.core.AppConfig$$SpringCGLIB$$0"
가 출력됩니다.
강사님 코드와 동일하게 작성했는데 제가 잘못 작성한 부분이 있는 걸까요?
답변 기다리겠습니다. 감사합니다. :)
Test 결과 사진:
package hello.core;
import hello.core.discount.DiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MemberService memberService() {
System.out.println("call AppConfig.memberService");
return new MemberServiceImpl(memberRepository());
}
@Bean
public OrderService orderService() {
System.out.println("call AppConfig.orderService");
return new OrderServiceImpl(memberRepository(), discountPolicy());
}
@Bean
public static MemberRepository memberRepository() {
System.out.println("call AppConfig.memberRepository");
return new MemoryMemberRepository();
}
@Bean
public DiscountPolicy discountPolicy() {
return new RateDiscountPolicy();
}
}
ConfigurationSingletonTest.java 코드:
package hello.core.singleton;
import hello.core.AppConfig;
import hello.core.member.MemberRepository;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderServiceImpl;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ConfigurationSingletonTest {
@Test
void configurationTest() {
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
MemberServiceImpl memberService = ac.getBean("memberService", MemberServiceImpl.class);
OrderServiceImpl orderService = ac.getBean("orderService", OrderServiceImpl.class);
MemberRepository memberRepository3 = ac.getBean("memberRepository", MemberRepository.class);
System.out.println("memberService -> memberRepository = " + memberService.getMemberRepository());
System.out.println("orderService -> memberRepository2 = " + orderService.getMemberRepository());
System.out.println("memberRepository = " + memberRepository3);
}
@Test
void configurationDeep() {
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
AppConfig bean = ac.getBean(AppConfig.class);
System.out.println("bean = " + bean.getClass());
}
}
답변 2
1
안녕하세요. 김세정님, 공식 서포터즈 y2gcoder입니다.
@Bean
public static MemberRepository memberRepository() {
System.out.println("call AppConfig.memberRepository");
return new MemoryMemberRepository();
}
AppConfig에서 memberRepository()의 static 을 제거해주시기 바랍니다.
다음 링크(클릭)에서도 볼 수 있듯이, static으로 해주면 @Configuration, @Bean으로 해도 싱글톤 보장을 해주지 않습니다 :)
감사합니다.
0
답변 정말 감사드립니다! :)