• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

AutoAppConfig 필터링 문제

24.03.25 19:08 작성 조회수 58

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 어노테이션은 잘 설정한것 같은데 뭐가 문제일까요?

답변 1

답변을 작성해보세요.

0

OMG님의 프로필

OMG

2024.03.25

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

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

감사합니다.