🧐강의🧐
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8/dashboard
✅ 스프링 컨테이너는 다양한 형식의 설정 정보를 받아들일 수 있다
》자바 코드
》XML
》Groovy
》...
✨ 애노테이션 기반 자바 코드 설정 사용
· AnnotationConfigApplicationContext 클래스를 사용해 자바 코드로 된 설정 정보 전달
》new AnnotationConfigApplicationContext(AppConfig.class)
✨ XML 설정 사용
· GenericXmlApplicationContext 클래스를 사용해 xml 설정 정보 전달
》new GenericXmlApplicationContext("appConfig.xml")
· 컴파일 없이 빈 설정 정보를 변경할 수 있는 장점
· 스프링 부트 영향으로 최근에는 잘 사용하지 않음
1. appConfig.xml 파일 생성
(main/resources/appConfig.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="memberService" class="hello.core.member.MemberServiceImpl">
<constructor-arg name="memberRepository" ref="memberRepository" />
</bean>
<bean id="memberRepository" class="hello.core.member.MemoryMemberRepository" />
<bean id="orderService" class="hello.core.order.OrderServiceImpl">
<constructor-arg name="memberRepository" ref="memberRepository" />
<constructor-arg name="discountPolicy" ref="discountPolicy" />
</bean>
<bean id="discountPolicy" class="hello.core.discount.RateDiscountPolicy" />
</beans>
➡️ xml 기반 appConfig.xml 스프링 설정 정보 ≒ 자바 코드 AppConfig.java 설정 정보
2. test/java/hello.core/xml Package 생성
3. test/java/hello.core/xml/XmlAppContext Class 생성
public class XmlAppContext {
@Test
void xmlAppContext() {
// 1. 스프링 컨테이너 설정 정보 불러오기 (XML)
ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");
// 2. 빈 정보 조회하기
MemberService memberService = ac.getBean("memberService", MemberService.class);
// 3. 검증하기
assertThat(memberService).isInstanceOf(MemberService.class);
}
}
'Spring > 스프링 핵심 원리 - 기본편' 카테고리의 다른 글
[싱글톤 컨테이너] 싱글톤 패턴의 문제를 해결하는 스프링 컨테이너 (0) | 2023.11.29 |
---|---|
[스프링 컨테이너와 스프링 빈] BeanDefinition - 스프링 빈 설정 메타 정보 (0) | 2023.11.25 |
[스프링 컨테이너와 스프링 빈] BeanFactory 와 ApplicationContext (0) | 2023.11.23 |
[스프링 컨테이너와 스프링 빈] 스프링 빈 조회 - 기본, 동일한 타입이 둘 이상, 상속 관계 (0) | 2023.11.22 |
[스프링 컨테이너와 스프링 빈] 컨테이너에 등록된 모든 빈 조회 (0) | 2023.11.21 |