TIL(Today I Learned)
TIL(2021.11.16)(26일차)
keepgoing
2021. 11. 16. 14:50
TIL(2021.11.16)(26th day) ✔
- spring container
- find spring container all of bean
- Find bean using role and definition.
- Find same type bean(getBeansOfType)
- 부모 타입 조회 = 자식 타입도 함께 조회.
spring container 🎯
ApplicationContext
= 스프링 컨테이너 or interfacenew AnnotationConfigApplicationContext(AppConfig.class)
는 구현체- 빈 이름 + 빈 객체로 구성
- 빈 이름은 서로 다르게 구성
- 스프링 빈 의존관계 설정
spring container BeanName, Bean Object 꺼내기 Test 🎯
- bean과 spring 내부 모두 콘솔에 출력
- 콘솔에 bean 이름과 객체만 출력
//ApplicationContextInfoTest
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
- bean에 대한 메타데이터 정보를 꺼냄.
//ApplicationContextInfoTest
if(beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION){
Object bean = ac.getBean(beanDefinitionName);
System.out.println("name = " + beanDefinitionName + "object = " + bean);
}
ROLE 역할
- 역할 = Application 즉, application을 위한 라이브러리 일때 콘솔에 출력
- Role ROLE_APPLICATION : 직접 등록한 애플리케이션 빈
- Role ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 빈
//ApplicationContextBasicFindTest assertThrows(NoSuchBeanDefinitionException.class, () -> ac.getBean("xxxx", MemberService.class)); // bean name이 다르면 error가 터져야한다.
//ApplicationContextSameBeanFindText
assertThat(memberRepository).isInstanceOf(MemberRepository.class); // isinstanceOf : 객체 타입 확인할 때 사용
BeanFactory🎯
- spring container 최상위 인터페이스
- 스프링 빈을 관리하고 조회하는 역할
- getBean()을 제공한다.
ApplicationContext🎯
- BeanFactory의 기능을 모두 상속받아서 제공
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
- ApplicationContext는 여러가지 부가 기능을 갖고 있음.
- EnvironmentCapable : 환경변수
- 로컬, 개발, 운영, 스테이지 환경 등을 구분해서 관리
- MessageSource : 메시지소스를 활용한 국제화 기능
- 예로 한국에선 한국어로, 미국에선 영어로
- ApplicationEventPublisher : 애플리케이션 이벤트
- 이벤트를 발행하고 구독하는 모델을 편리하게 지원
- ResourceLoader : 편리한 리소스 조회
- 파일, 클래스패스, 외부 등에서 리소스를 편리하게 조회
- EnvironmentCapable : 환경변수
- 결론 : BeanFactory나 ApplicationContext나 모두 스프링 컨테이너라고 불림
- BeanFactory는 잘 안쓰임
- ApplicationContext는 Bean을 관리하고 부가기능을 제공함.
- SpringContainer는 자바코드, XML, Groovy 등 다양한 형식의 설정 정보를 받아드릴 수 있게 유연하게 설계되어 있다.
//appConfig.xml
<bean id="memberService" class="hello.core.member.MemberServiceImpl">
<constructor-arg name = "memberRepository" ref="memberRepository"/>
</bean>
<bean id = "memberRepository" class="hello.core.member.MemoryMemberRepository"/>
</beans>
- 이전에 만든 AppConfig.java와 형식이 같음(id = interface, class = 구현체)
- package 경로까지 기입해줘야함.