inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

스프링 핵심 원리 - 기본편

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

256

벼리

작성한 질문수 1

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 패키지에 있습니다.

 

 

 

spring 객체지향

답변 2

0

y2gcoder

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

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

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

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

https://bit.ly/3fX6ygx


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


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

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

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


링크: 공식 서포터즈

링크: 자주하는 질문

감사합니다.

0

인프런 AI 인턴

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

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

감사합니다.

코드 자료

0

48

2

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

0

55

2

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

0

76

1

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

0

91

1

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

0

81

1

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

0

77

1

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

0

121

2

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

0

86

1

provider 사용하는 이유

0

90

1

다음 강의 뭘 들어야 할까요

0

126

2

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

0

66

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

66

1

12:25

0

79

2