본문으로 바로가기

[스프링] 18. 자바 코드 또는 XML 설정

category SPRING/스프링 2021. 5. 3. 14:40
728x90
반응형
SMALL

다양한 설정 형식 지원 - 자바 코드, XML

 

아래는 같은 설정이다.

*자바 bean 설정

 

    @Bean
    public MemberService memberService(){
        return new MemberServiceImpl(memberRepository());
    }
    @Bean
    public MemoryMemberRepository memberRepository() {
        return new MemoryMemberRepository();
    }

*xml 설정

  <bean id="memberService" class="hello.core.member.MemberServiceImpl">
        <constructor-arg name="memberRepository" ref="memberRepository" />
    </bean>

    <bean id="memberRepository" class="hello.core.member.MemoryMemberRepository"/

 

*자바 bean 설

@Bean
    public OrderService orderService(){
        return new OrderServiceImpl(memberRepository(), discountPolicy());
    }
    @Bean
    public DiscountPolicy discountPolicy(){
        return new RateDiscountPolicy();
        // return new FixDiscountPolicy()
    }

 

*xml 설정

<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"/>

 

728x90
반응형
LIST