• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

필터 - 'No bean named 'beanA' available' 에러

22.12.10 18:47 작성 22.12.10 19:07 수정 조회수 1.13k

1

package hello.core.scan.filter;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
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.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.context.annotation.ComponentScan.*;

public class ComponentFilterAppConfigTest {

    @Test
    void filterScan() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfigTest.class);
        BeanA beanA = ac.getBean("beanA", BeanA.class);
        //여기서 계속 문제 발생

        assertThat(beanA).isNotNull();

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

    }

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

 

안녕하세요, '필터' 강의에서 filterScan 테스트 하는 부분에서 계속해서 아래와 같은 에러가 발생하네요

(강의에서는 5분 3초에서 실행하는 부분입니다.)

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'beanA' available

처음부터 다시 써보기도하고 이전의 질문들을 찾아보기도 했지만 이 테스트 코드는 계속해서 통과를 할 수 없네요ㅠ

어떤 문제인지 도와주실 수 있다면 정말 감사드리겠습니다!

 

답변 2

·

답변을 작성해보세요.

2

넥스링크님의 프로필

넥스링크

2022.12.11

ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfigTest.class);
ComponentFilterAppConfigTest.class 를 ComponentFilterAppConfig.class로 바꿔주세요. 자동완성 이슈인듯 합니다
devace님의 프로필

devace

질문자

2022.12.11

image

image

드디어 해결되었습니다 정말 감사합니다 ! 계속 이상한걸 불러오고 있었군요....

 

 

0

David님의 프로필

David

2022.12.10

안녕하세요. lab님, 공식 서포터즈 David입니다.

빈 이름이 beanA 인 빈을 못 찾고 있습니다.

BeanA 클래스에 @MyIncludeComponent 애노테이션을 붙이셨을까요?

감사합니다.

devace님의 프로필

devace

질문자

2022.12.10

package hello.core.scan.filter;

@MyIncludeComponent
public class BeanA {
}

네 붙인 상태였습니다!

프로젝트 링크 공유드립니다 한번 확인해주시면 정말 감사드리겠습니다!

https://drive.google.com/drive/folders/1K4llnm02ONPUGts_6FCHOdN122v3wN7a?usp=share_link