현재까지의 등록 방법 -> @Bean 등 설정 정보에 직접 스프링 빈 나열
-> 귀찮다 ....
=> @Autowired !!
@Component와 @Autowired를 각 필요한 위치에 넣은 코드 작성
클래스 명 사용 주의 (앞 두글자만 바꾸고 !)
의존관계 주입 시 스프링 컨테이너에서 자동으로 찾아서 주입함 !
골뱅이모음
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyExcludeComponent { //혹은 MyIncludeComponent 형식
}
package hello.core.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 {
}
}
같은 빈 이름을 등록한다면 ??
1. 자동 vs 자동
재 등록 -> ConflictingBeanDefinitionException 예외 발생
2. 수동 vs 자동
수동 빈 등록이 우선권
Overriding bean definition for bean 'memoryMemberRepository' with a different
definition: replacing
ㄴ 로그 내용
로그의 내용을 보고 어떤 오류인지 확인할 수 있음.
의존관계 주입 방법
수정자 주입(setter 주입)
setter라 불리는 필드의 값을 변경하는 수정자 메서드를 통해서 의존관계를 주입하는 방법
필드 주입
필드에 바로 주입
일반 메서드 주입
일반 메서드를 통해서 주입 받기