본문 바로가기
728x90
반응형

스프링 Spring/스프링 핵심 원리 - 기본편61

[스프링 컨테이너와 스프링 빈] 다양한 설정 형식 지원 - 자바 코드, XML 다양한 설정 형식 지원 - 자바 코드, XML XML 설정 사용 GenericXmlApplicationContext를 사용하면서 xml 설정 파일을 넘기면 된다. public class XmlAppContext { @Test void xmlAppContext() { ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml"); MemberService memberService = ac.getBean("memberService", MemberService.class); assertThat(memberService).isInstanceOf(MemberService.class); } } 위치 : src/main/resources/appCo.. 2022. 2. 4.
[스프링 컨테이너와 스프링 빈] BeanFactory와 ApplicationContext BeanFactory와 ApplicationContext BeanFactory 스프링 컨테이너의 최상위 인터페이스 스프링 빈을 관리하고 조회하는 역할 getBean() 제공 ApplicationContext BeanFactory 기능 모두 상속 + 수 많은 부가 기능 인프런 강의 "스프링 핵심 원리 - 기본편"을 정리한 것 입니다. 스프링 핵심 원리 - 기본편 - 인프런 | 강의 스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다., - 강의 소개 | 인프런... www.inflearn.com 2022. 2. 3.
[스프링 컨테이너와 스프링 빈] 스프링 빈 조회 - 상속 관계 스프링 빈 조회 - 상속 관계 부모 타입으로 조회하면, 자식 타입도 함께 조회 public class ApplicationContextExtendsFindTest { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class); @Test @DisplayName("부모 타입으로 조회시, 자식이 둘 이상 있으면, 중복 오류가 발생한다.") void findBeanByParentTypeDuplicate() { // DiscountPolicy 타입이 두개라 에러(rateDiscountPolicy, fixDiscountPolicy) // DiscountPolicy bean = ac.getBean(Di.. 2022. 2. 3.
[스프링 컨테이너와 스프링 빈] 스프링 빈 조회 - 동일한 타입이 둘 이상 스프링 빈 조회 - 동일한 타입이 둘 이상 public class ApplicationContextSameBeanFindTest { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SameBeanConfig.class); @Test @DisplayName("타입으로 조회시 같은 타입이 둘 이상 있으면, 중복 오류가 발생한다.") void findBeanByTypeDuplicate() { MemberRepository bean = ac.getBean(MemberRepository.class); assertThrows(NoUniqueBeanDefinitionException.class, () -> ac.getBean.. 2022. 2. 2.
[스프링 컨테이너와 스프링 빈] 스프링 빈 조회 - 기본 스프링 빈 조회 - 기본 public class ApplicationContextBasicFindTest { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); @Test @DisplayName("빈 이름으로 조회") void findBeanByName() { MemberService memberService = ac.getBean("memberService", MemberService.class); System.out.println("memberService = " + memberService); System.out.println("memberService.getClass() = ".. 2022. 2. 2.
[스프링 컨테이너와 스프링 빈] 컨테이너에 등록된 모든 빈 조회 컨테이너에 등록된 모든 빈 조회 public class ApplicationContextTest { 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 = " +.. 2022. 2. 2.
[스프링 컨테이너와 스프링 빈] 스프링 컨테이너 생성 스프링 컨테이너 생성 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); ApplicationContext는 스프링 컨테이너이며 인터페이스이다. new AnnotationConfigApplicationContext(AppConfig.class);는 ApplicationContext 인터페이스의 구현체이다. 스프링 컨테이너의 생성 과정 1. 스프링 컨테이너 생성 2. 스프링 빈 등록 빈 이름 1. 메서드 이름을 사용 2. 직접 부여 ex) @Bean(name="memberService2") 3. 스프링 빈 의존관계 설정 인프런 강의 "스프링 핵심 원리 - 기본편"을 정리한 것 입니다. .. 2022. 2. 2.
[스프링 핵심 원리 이해2 - 객체 지향 원리 적용] 스프링으로 전환하기 스프링으로 전환하기 AppConfig에 @Configuration과 @Bean 추가 @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.. 2022. 2. 1.
728x90
반응형