반응형

1. 의존 관계 자동 주입

- 자동 빈 등록

이전 예제에서 스프링 빈을 등록할 때 자바 코드의 @Bean이나 XML의 등을 통해서 설정 정보에 직접 등록할 스프링 빈을 수동으로 등록했지만, 스프링에선 자동으로 스프링 빈을 등록하는 @ComponentScan이라는 기능을 제공한다.

 

자동 구성을 해주는 새로운 기획자

package hello.core;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import static org.springframework.context.annotation.ComponentScan.*;

@Configuration
@ComponentScan
public class AutoAppConfig {
 
}

@ComponentScan 어노테이션을 붙여주고, @Bean 어노테이션과 내용은 하나도 없다.

Bean으로 등록하고 싶은 클래스에 직접 @Component어노테이션을 붙여주면 자동으로 Bean에 등록된다.

MemberRepository에 의존관계를 주입하고자 하는 MemoryMemberRepository만 Bean에 등록하기 위해 @Component어노테이션을 붙인다.

DiscountPolicy도 마찬가지로 RateDiscountPolicy에만 @Component어노테이션을 붙인다.

 

예시)

@Component
public class MemoryMemberRepository implements MemberRepository {
	...
}
@Component
public class RateDiscountPolicy implements DiscountPolicy {
	...
}

 

컴포넌트 스캔 기본 대상

- @Component : 컴포넌트 스캔에서 사용

- @Controlller : 스프링 MVC 컨트롤러에서 사용

- @Service : 스프링 비즈니스 로직에서 사용

- @Repository : 스프링 데이터 접근 계층에서 사용

- @Configuration : 스프링 설정 정보에서 사용

 

 

스프링 컨테이너를 인터스턴스화 할때 해당 config파일을 인자로 넘겨주면 된다.

 

- 자동 의존 관계 주입 등록

@Autowire어노테이션을 통해 자동 의존 관계를 주입.

 

예시)

@Component
public class MemberServiceImpl implements MemberService {
	private final MemberRepository memberRepository;
	
    @Autowired
	public MemberServiceImpl(MemberRepository memberRepository) {
		this.memberRepository = memberRepository;
	}
}
@Component
public class OrderServiceImpl implements OrderService {
	private final MemberRepository memberRepository;
	private final DiscountPolicy discountPolicy;
	
    // 여러 의존관계 한번에 주입 가능
    @Autowired
	public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
		this.memberRepository = memberRepository;
		this.discountPolicy = discountPolicy;
	}
}

@Autowire의 기본 조회 전략은 타입이 같은 빈을 찾아서 주입한다. getBean(MemberRepository.class), getBean(DiscountPolicy.class)와 동일.

 

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

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

@ComponentScan( basePackages = "hello.core" )

 

basePackages : 탐색할 패키지의 시작 위치를 지정한다. 이 패키지를 포함해서 하위 패키지를 모두 탐색한다. basePackages = {"hello.core", "hello.service"} 이렇게 여러 시작 위치를 지정할 수도 있다.

basePackageClasses : 지정한 클래스의 패키지를 탐색 시작 위치로 지정한다.

 

만약 지정하지 않으면 @ComponentScan 이 붙은 설정 정보 클래스의 패키지가 시작 위치가 된다.

 

※ 권장하는 방법

패키지 위치를 지정하지 않고, 설정 정보 클래스의 위치를 프로젝트 최상단에 두는 것이다. 최근 스프링 부트도 이 방법을 기본으로 제공한다.

 

3. 컴포넌트 스캔 대상 필터

includeFilters : 컴포넌트 스캔 대상을 추가로 지정한다.

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

 

- 어노테이션 생성

컴포넌트 스캔 대상에 추가할 애노테이션

package hello.core.scan.filter;
import java.lang.annotation.*;

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

컴포넌트 스캔 대상에서 제외할 애노테이션

package hello.core.scan.filter;
import java.lang.annotation.*;

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

 

- 어노테이션 적용

컴포넌트 스캔 대상에 추가할 클래스

package hello.core.scan.filter;

@MyIncludeComponent
public class BeanA { }

 

컴포넌트 스캔 대상에 제외할 클래스

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

 

- 테스트

package hello.core.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;
import static org.springframework.context.annotation.ComponentScan.Filter;

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 = @Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
		excludeFilters = @Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
	)
	static class ComponentFilterAppConfig { }
}

 

FilterType의 5가지 옵션

ANNOTATION: 기본값, 애노테이션을 인식해서 동작한다.

ASSIGNABLE_TYPE: 지정한 클래스의 타입과 자식 클래스 타입을 인식해서 동작한다. 

ASPECTJ: AspectJ 패턴 사용

REGEX: 정규 표현식

CUSTOM: TypeFilter 이라는 인터페이스를 구현해서 처리

 

 

4. 중복 등록과 충돌

- 자동 Bean 등록 vs 자동 Bean  등록

컴포넌트 스캔에 의해 자동으로 스프링 Bean이 등록될때 이름이 같은 경우 스프링은 ConflictingBeanDefinitionException 예외를 발생시킨다. 

 

- 수동 Bean  등록 vs 자동 Bean  등록

수동 Bean 등록이 우선권을 가지며, 오버라이드 됐다는 로그를 출력 

Overriding bean definition for bean 'XXXX' with a different definition: replacing

 

반응형

+ Recent posts