본문 바로가기

Spring106

[스프링 핵심 원리 - 기본편] 스프링 빈 조회 - 기본 스프링 컨테이너에서 스프링 빈을 찾는 가장 기본적인 방법 ac.getBean(빈이름, 타입) ac.getBean(타입) 조회 대상 스프링 빈이 없으면 예외 발생 NoSuchBeanDefinitionException: No bean named 'xxxxx' available 예제 코드 class ApplicationContextBasicFindTest { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); @Test @DisplayName("빈 이름으로 조회") void findBeanByName() { MemberService memberService = ac.getBean("memb.. 2021. 9. 12.
[스프링 핵심 원리 - 기본편] 컨테이너에 등록된 모든 빈 조회 class ApplicationContextInfoTest { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); @Test @DisplayName("모든 빈 출력하기") void findAllBean() { String[] beanDefinitionNames = ac.getBeanDefinitionNames(); for (String beanDefinitionName : beanDefinitionNames) { Object bean = ac.getBean(beanDefinitionName); System.out.println("name = " + beanDefinitionName +.. 2021. 9. 12.
[스프링 핵심 원리 - 기본편] 스프링 컨테이너 생성 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); ApplicationContext를 스프링 컨테이너라 합니다. ApplicationContext는 인터페이스입니다. 스프링 컨테이너는 XML을 기반으로 만들 수 있고, 애노테이션 기반의 자바 설정 클래스로 만들 수 있습니다. 직전에 AppConfig를 사용했던 방식이 애노테이션 기반의 자바 설정 클래스로 스프링 컨테이너를 만든 것입니다. 자바 설정 클래스 기반 스프링 컨테이너(ApplicationContext) new AnnotationConfigApplicationContext(AppConfig.clss); 이 클래스는 Applic.. 2021. 9. 11.
[스프링 핵심 원리 - 기본편] 스프링으로 전환하기 AppConfig 스프링 기반으로 변경 @Configuration public class AppConfig { @Bean public MemberService memberService() { return new MemberServiceImpl(memberRepository()); } @Bean public MemberRepository memberRepository() { return new MemoryMemberRepository(); } @Bean public OrderService orderService() { return new OrderServiceImpl(memberRepository(), discountPolicy()); } @Bean public DiscountPolicy discountP.. 2021. 9. 11.