인프런 커뮤니티 질문&답변

김아영님의 프로필 이미지
김아영

작성한 질문수

스프링 핵심 원리 - 기본편

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

에러관련 질문드려요~

작성

·

1.3K

0

안녕하세요. 강사님. 이번 강의에서 

XmlAppContext.java는

package hello.beanfind;

import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.test.context.web.GenericXmlWebContextLoader;

public class XmlAppContext {

@Test
@DisplayName("xml config 테스트해보기")
void xmlAppContext(){
ApplicationContext ac = new GenericXmlApplicationContext("appConfig.xml");
MemberService ms = ac.getBean("memberService", MemberService.class);
Assertions.assertThat(ac).isInstanceOf(MemberService.class);
}

}

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>

이렇게 작성하였는데 아래와 같은 에러가 나는데요. 

18:22:00.364 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'memberService'

18:22:00.397 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'memberRepository'

18:22:00.420 [main] WARN org.springframework.context.support.GenericXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memberService' defined in class path resource [appConfig.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [hello.core.member.MemberRepository] - did you specify the correct bean references as arguments?

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memberService' defined in class path resource [appConfig.xml]: Unsatisfied dependency expressed through constructor parameter 0: Ambiguous argument values for parameter of type [hello.core.member.MemberRepository] - did you specify the correct bean references as arguments?

구글링을 해도 원인을 모르겠어서요..ㅜ.ㅜ

답변 2

6

김아영님의 프로필 이미지
김아영
질문자

알려주신 대로 생성자부분을 보니 생성자부분의 이름이 달라서 그랬던 거였네요..ㅜ.ㅜ 오히려 이렇게 에러가 나니 당연하게 여겼던 부분까지 다시보게 되네요~ 답변 감사드립니다^-^

3

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. 아영님

MemberService 객체의 생성자 부분에 문제가 있는 것 같아요. 해당 객체 전체를 보여주시겠어요?

김아영님의 프로필 이미지
김아영

작성한 질문수

질문하기