• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    미해결

섹션6. 필터에서 beanA를 찾을 수 없음

24.01.02 21:37 작성 조회수 115

0

 

@MyIncludeComponent
public class BeanA {
}
@MyExcludeComponent
public class BeanB {
}
package hello.core.scan.filter;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

import static org.assertj.core.api.Assertions.*;
import static org.springframework.context.annotation.ComponentScan.*;

public class ComponentFilterAppConfigTest {

    @Test
    void filterScan() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
        BeanA beanA = ac.getBean("beanA", BeanA.class);
        assertThat(beanA).isNotNull();

    }

    @Configuration
    @ComponentScan(
            includeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
            excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
    )
    static class ComponentFilterAppConfig {

    }
}

강의와 똑같이 따라친 해당 코드에서 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'beanA' available 에러가 계속 발생합니다.

 

그래서 아래 코드처럼 BeanA 클래스와 BeanB클래스에 @Component 애노테이션을 추가하였더니 잘 작동합니다.

 

@MyIncludeComponent
@Component
public class BeanA {
}
@MyExcludeComponent
@Component
public class BeanB {
}

 

그런데 이 경우에는 또 하단의 Assertions.thorws 검증 코드가 제대로 작동하지 않습니다. ㅠ ....

 

   assertThrows(
                NoSuchBeanDefinitionException.class,
                () -> ac.getBean("beanB", BeanB.class));

 

그래서 하단 코드처럼 BeanA에만 @Component를 붙이고 BeanB에는 @Component를 붙이지 않으면 테스트 코드가 올바르게 작동합니다... 원인을 알 수 있을까요?

 

@MyIncludeComponent
@Component
public class BeanA {
}
@MyExcludeComponent
public class BeanB {
}

 

BeanA, BeanB, MyExcludeComponent, MyIncludeComponent, ComponentFilterAppConfigTest 모두 테스트의 filter 패키지에 있습니다.

 

 

 

답변 2

·

답변을 작성해보세요.

0

y2gcoder님의 프로필

y2gcoder

2024.01.03

안녕하세요. 벼리님, 공식 서포터즈 y2gcoder입니다.

도움을 드리고 싶지만 질문 내용만으로는 답변을 드리기 어렵습니다.

실제 동작하는 전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx


주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요


추가로 다음 내용도 코멘트 부탁드립니다.

1. 문제 영역을 실행할 수 있는 방법

2. 문제가 어떻게 나타나는지에 대한 상세한 설명


링크: 공식 서포터즈

링크: 자주하는 질문

감사합니다.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.01.02

안녕하세요, 인프런 AI 인턴입니다.

해당 질문은 No bean named 'beanA' available 에 대한 문제로 보이며, 같은 문제로 다른 학습자분들도 질문을 하셨던 내용입니다. 아래 제공된 링크 중 하나를 참조하시면 도움이 될 것 같습니다. 제목을 클릭하시면 해당 질문 내용과 답변을 보실 수 있습니다.

감사합니다.