Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

XML로 설정해보자아 본문

인프런/[인프런] Spring 핵심원리 이해

XML로 설정해보자아

Seung__ 2021. 7. 26. 22:48

 

 

1. Xml사용


  • 스프링 부트 사용하면서 잘 사용안함...
  • 레거시 프로젝트들이 xml로 되어있음.
  • xml을 사용하면 컴파일 없이 빈 설정 정볼르 변경할 수 있다는 장점도 있음!!
  • GenericXmlApplicationContext를 사용하고 xml을 넘기면됨!!!

 

 

 

2.Xml 작성


  • resources하위에 appConfig.xml 생성
  • 기존 AppConfig.class와 다른 것 같지만 구조는 똑같다!
<?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>

 

 

 

3.Test


  • GenericXmlApplicationContext를 사용하고 xml을 넘기면됨!!!
public class XmlAppContext {

    @Test
    void xmlContext(){
        ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");
        MemberService memberService = ac.getBean("memberService", MemberService.class);
        Assertions.assertThat(memberService).isInstanceOf(MemberService.class);
    }
}

 

* 중요한 점은 별다른 코드 변경 없이 설정 방식을 변경할 수 있었다. ㅗㅜㅑ

 

4.GitHub : 210726 XML Config


 

 

GitHub - bsh6463/SpringCoreFunction

Contribute to bsh6463/SpringCoreFunction development by creating an account on GitHub.

github.com

 

Comments