inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 핵심 원리 - 기본편

AutoAppConfig 필터링 문제

233

정대교

작성한 질문수 1

0

안녕하세요!

컴포넌트 스캔의 필터와 관련하여 질문이 있어 올립니다!

기존에 있던 AppConfig.java로 등록한 Bean을 제외하려고 excludeFilters를 사용했는데, CoreApplication을 실행하면

Parameter 0 of constructor in hello.core.member.MemberServiceImpl required a single bean, but 2 were found:

- memoryMemberRepository: defined in file [파일경로/core/out/production/classes/hello/core/member/MemoryMemberRepository.class]

- memberRepository: defined by method 'memberRepository' in class path resource [hello/core/AppConfig.class]

이런 오류가 뜹니다. 코드는 다음과 같습니다.

AppConfig.java

package hello.core;

import hello.core.discount.DiscountPolicy;
import hello.core.discount.FixDiscountPolicy;
import hello.core.discount.RateDiscountPolicy;
import hello.core.member.MemberRepository;
import hello.core.member.MemberService;
import hello.core.member.MemberServiceImpl;
import hello.core.member.MemoryMemberRepository;
import hello.core.order.OrderService;
import hello.core.order.OrderServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class AppConfig {

    @Bean
    public MemberRepository memberRepository() {
        return new MemoryMemberRepository();
    }

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

    @Bean
    public DiscountPolicy discountPolicy() {
        //return new FixDiscountPolicy();
        return new RateDiscountPolicy();
    }

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

AutoAppConfig.java

package hello.core;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import static org.springframework.context.annotation.ComponentScan.*;

@Configuration
@ComponentScan(
        excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
)

public class AutoAppConfig {
}

위의 오류는 memberRepository뿐만 아니라 rateDiscountPolicy에서도 나옵니다. @Component와 @Autowired 어노테이션은 잘 설정한것 같은데 뭐가 문제일까요?

spring 객체지향

답변 1

0

OMG

안녕하세요. 정대교님, 공식 서포터즈 OMG입니다.

다음 링크(클릭)의 영한님 답변(원글 답변 및 추가답변)들을 보시면 원인과 @ComponentScan에서의 동작을 자세히 아실 수 있으실꺼라 생각합니다 😀

감사합니다.

코드 자료

0

42

2

구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?

0

55

2

MemberService의 인터페이스를 왜 사용하는지 궁금합니다.

0

75

1

롬복 @Setter를 써야 하는 상황이 있는건가요?

0

90

1

빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?

0

81

1

테스트 속도가 나중에 영향이 있을까요?

0

77

1

gradle 설정 안떠서 질문 남깁니다!

0

121

2

build.gradle로 프로젝트를 여는 이유

0

86

1

provider 사용하는 이유

0

89

1

다음 강의 뭘 들어야 할까요

0

126

2

프로토타입 빈, 직접 destroy 호출 안 할 경우

0

65

1

beanB

0

82

2

퀴즈다시풀기

0

68

1

Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ

0

92

2

"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.

0

67

3

run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>

0

106

2

도메인의 정의?

0

59

1

ApplicationContext 질문입니다.

0

63

1

@Scope의 proxyMode를 사용할때 단위 테스트 방법

0

89

2

ai api 선정하기 관련 질문

0

118

2

생성자 자동주입 관련해서

0

65

1

생성자 직접 호출 vs 팩토리 메서드 패턴

0

97

2

Spring에서 SessionScope와 RequestScope는 함께 사용되나요?

1

65

1

12:25

0

78

2