스프링/1. 스프링 핵심 원리

25강. XML로 스프링

sdafdq 2023. 7. 17. 23:45

다양한 걸로 Bean 가능

 

 

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

비슷함. 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="com.example.core.member.MemberServiceImpl">
        <constructor-arg name = "memberRepository" ref="memberRepository"/>
    </bean>
    <bean id="discountPolicy" class="com.example.core.discount.RateDiscountPolicy"></bean>

    <bean id="orderService" class="com.example.core.order.OrderServiceImpl">
        <constructor-arg name="memberRepository" ref="memberRepository"></constructor-arg>
        <constructor-arg name="discountPolicy" ref="discountPolicy"></constructor-arg>
    </bean>

    <bean id="memberRepository" class="com.example.core.member.MemoryMemberRepository"></bean>


</beans>

new file 할 때 xml 보면 spring xml 있고, 

bean만 내가 써줌.

id는 bean 이름, class는 실제 구현체

<constructor-arg 는 생성자에 주입되는 인자