Spring(기본) - 컴포넌트 스캔

Kwon Yongho·2023년 4월 7일
0

Spring

목록 보기
14/37
post-thumbnail

컴포넌트 스캔

  1. 컴포넌트 스캔과 의존관계 자동 주입 시작하기
  2. 탐색 위치와 기본 스캔 대상
  3. 필터
  4. 중복 등록과 충돌

1. 컴포넌트 스캔과 의존관계 자동 주입 시작하기

-지금까지 스프링 빈을 등록할 때는 자바 코드의 @Bean이나 XML의 bean 등을 통해서 설정 정보에 직접 등록할 스프링 빈을 나열했다.
-스프링은 설정 정보가 없어도 자동으로 스프링 빈을 등록하는 컴포넌트 스캔이라는 기능을 제공한다.
-의존관계도 자동으로 주입하는 @Autowired 라는 기능도 제공한다.

java/com/example/springex1/AutoAppConfig.java

package com.example.springex1;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Configuration
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class))
public class AutoAppConfig {

}

컴포넌트 스캔을 사용하면 @Configuration 이 붙은 설정 정보도 자동으로 등록되기 때문에, AppConfig, TestConfig 등 앞서 만들어두었던 설정 정보도 함께 등록되고, 실행되어 버린다. 그래서 excludeFilters 를 이용해서 설정정보는 컴포넌트 스캔 대상에서 제외했다.

이제 각각 클래스가 컴포넌트 스캔의 대상이 되도록 @Conponent 애노테이션을 붙혀준다.

  • MemoryMemberRepository

  • RateDiscountPolicy

  • MemberServiceImpl

  • OrderServiceImpl

  • 컴포넌트 스캔을 사용하려면 먼저 @ComponentScan 을 설정 정보에 붙여주면 된다.

  • 기존의 AppConfig와는 다르게 @Bean으로 등록한 클래스가 하나도 없다.

  • 이전에 AppConfig에서는 @Bean 으로 직접 설정 정보를 작성했고, 의존관계도 직접 명시했다. 이제는 이런 설정 정보 자체가 없기 때문에, 의존관계 주입도 이 클래스 안에서 해결해야 한다.

test
java/com/example/springex1/scan/AutoAppConfigTest.java

package com.example.springex1.scan;

import com.example.springex1.AutoAppConfig;
import com.example.springex1.member.MemberService;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;

public class AutoAppConfigTest {

    @Test
    void basicScan() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);
        MemberService memberService = ac.getBean(MemberService.class);
        assertThat(memberService).isInstanceOf(MemberService.class);
    }
}

결과

ClassPathBeanDefinitionScanner - Identified candidate component class:
.. RateDiscountPolicy.class
.. MemberServiceImpl.class
.. MemoryMemberRepository.class
.. OrderServiceImpl.class

로그를 잘 보면 컴포넌트 스캔이 잘 동작하는 것을 확인할 수 있다.

  1. @ComponentScan
  • @ComponentScan은 @Component가 붙은 모든 클래스를 스프링 빈으로 등록한다.
  1. @Autowired 의존관계 자동 주입
  • 생성자에 @Autowired 를 지정하면, 스프링 컨테이너가 자동으로 해당 스프링 빈을 찾아서 주입한다.

2. 탐색 위치와 기본 스캔 대상

모든 자바 클래스를 다 컴포넌트 스캔하면 시간이 오래 걸린다. 그래서 꼭 필요한 위치부터 탐색하도록 시작위치를 지정할 수 있다.

@ComponentScan(
 basePackages = "hello.core",
}
  • basePackages : 탐색할 패키지의 시작 위치를 지정한다. 이 패키지를 포함해서 하위 패키지를 모두 탐색한다.

권장 방법

  • 설정 정보 클래스의 위치를 프로젝트 최상단에 두는 것이다. 최근 스프링 부트도 이 방법을 기본으로 제공한다.
  • 스프링 부트를 사용하면 스프링 부트의 대표 시작 정보인 @SpringBootApplication를 이 프로젝트 시작 루트 위치에 두는 것이 관례이다.

2-1.컴포넌트 스캔 기본 대상

  • @Component: 컴포넌트 스캔에서 사용
  • @Controller: 스프링 MVC 컨트롤러에서 사용
  • @Service: 스프링 비지니스 로직에서 사용
  • @Repository: 스프링 데이터 접근 계층에서 사용
  • @Configuration: 스프링 설정 정보에서 사용

부가기능

  • @Component: 스프링 MVC 컨트롤러로 인식
  • @Repository: 스프링 데이터 접근 계층으로 인식하고, 데이터 계층의 예외를 스프링 예외로 변환해준다.
  • @Configuration: 스프링 설정 정보로 인식하고, 스프링 빈이 싱글톤을 유지하도록 추가 처리를 한다.

참고: 사실 애노테이션에는 상속관계라는 것이 없다. 그래서 이렇게 애노테이션이 특정 애노테이션을 들고 있는 것을 인식할 수 있는 것은 자바 언어가 지원하는 기능은 아니고, 스프링이 지원하는 기능이다.

3. 필터

  • includeFilters: 컴포넌트 스캔 대상을 추가로 지정한다.
  • excludeFilters: 컴포넌트 스캔에서 제외할 대상을 지정한다.

test 코드
scan/filter/BeanA.java

package com.example.springex1.scan.filter;

@MyIncludeComponent
public class BeanA {
}

scan/filter/BeanB.java

package com.example.springex1.scan.filter;

@MyExcludeComponent
public class BeanB {
}

scan/filter/MyExcludeComponent.interface

package com.example.springex1.scan.filter;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent {
}

scan/filter/MyIncludeComponent.interface

package com.example.springex1.scan.filter;

import java.lang.annotation.*;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyIncludeComponent {
}

scan/filter/ComponentFilterAppConfigTest

package com.example.springex1.scan.filter;

import org.junit.jupiter.api.Assertions;
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;

public class ComponentFilterAppConfigTest {
    @Test
    void filterScan() {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
        BeanA beanA = ac.getBean("beanA", BeanA.class);
        assertThat(beanA).isNotNull();
        Assertions.assertThrows(NoSuchBeanDefinitionException.class,
                () -> ac.getBean("beanB", BeanB.class));
    }
    @Configuration
    @ComponentScan(includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
            excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
    )
    static class ComponentFilterAppConfig {
    }
}

4. 중복 등록과 충돌

컴포넌트 스캔에서 같은 빈 이름을 등록하면??

  1. 자동 빈 등록 vs 자동 빈 등록
  2. 수동 빈 등록 vs 수동 빈 등록

4-1. 자동 빈 등록 vs 자동 빈 등록

  • 컴포넌트 스캔에 의해 자동으로 스프링 빈이 등록되는데, 그 이름이 같은 경우 스프링은 오류를 발생시킨다.
  • ConflictingBeanDefinitionException 예외 발생

4-2. 수동 빈 등록 vs 자동 빈 등록

  • 이 경우 수동 빈 등록이 우선권을 가진다.
  • 수동 빈이 자동 빈을 오버라이딩 해버린다.

    수동 빈 등록시 남는 로그
    Overriding bean definition for bean 'memoryMemberRepository' with a different definition: replacing
    수동 빈 등록, 자동 빈 등록 오류시 스프링 부트 에러
    Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

참고
김영한: 스프링 핵심 원리 - 기본편(인프런)
Github - https://github.com/b2b2004/Spring_ex

0개의 댓글