섹션6. 필터에서 beanA를 찾을 수 없음
258
작성한 질문수 1
@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입니다.
도움을 드리고 싶지만 질문 내용만으로는 답변을 드리기 어렵습니다.
실제 동작하는 전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.
구글 드라이브 업로드 방법은 다음을 참고해주세요.
주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요
추가로 다음 내용도 코멘트 부탁드립니다.
1. 문제 영역을 실행할 수 있는 방법
2. 문제가 어떻게 나타나는지에 대한 상세한 설명
링크: 공식 서포터즈
링크: 자주하는 질문
감사합니다.
0
안녕하세요, 인프런 AI 인턴입니다.
해당 질문은 No bean named 'beanA' available 에 대한 문제로 보이며, 같은 문제로 다른 학습자분들도 질문을 하셨던 내용입니다. 아래 제공된 링크 중 하나를 참조하시면 도움이 될 것 같습니다. 제목을 클릭하시면 해당 질문 내용과 답변을 보실 수 있습니다.
감사합니다.
빈 스코프 개념의 중요성
0
23
1
SingletonService가 JVM이 뜰 때 생성되는게 맞나요?
0
68
2
섹션3. 11 회원객체 다이어그램
0
38
1
OCP, DIP과 @Qualifier 어노테이션에 대해서 질문합니다.
0
40
1
코드 자료
0
86
2
구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?
0
83
2
MemberService의 인터페이스를 왜 사용하는지 궁금합니다.
0
100
1
롬복 @Setter를 써야 하는 상황이 있는건가요?
0
104
1
빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?
0
89
1
테스트 속도가 나중에 영향이 있을까요?
0
91
1
gradle 설정 안떠서 질문 남깁니다!
0
148
2
build.gradle로 프로젝트를 여는 이유
0
101
1
provider 사용하는 이유
0
99
1
다음 강의 뭘 들어야 할까요
0
140
2
프로토타입 빈, 직접 destroy 호출 안 할 경우
0
70
1
beanB
0
94
2
퀴즈다시풀기
0
77
1
Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ
0
105
2
"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.
0
72
3
run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>
0
116
2
도메인의 정의?
0
64
1
ApplicationContext 질문입니다.
0
68
1
@Scope의 proxyMode를 사용할때 단위 테스트 방법
0
98
2
ai api 선정하기 관련 질문
0
141
2





