spring core : component scan
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package com.example.basic;
|
||||||
|
|
||||||
|
import com.example.basic.member.MemberRepository;
|
||||||
|
import com.example.basic.member.MemoryMemberRepository;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.FilterType;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(
|
||||||
|
basePackages = "com.example.basic", // default : @ComponentScan 의 위치 패키지
|
||||||
|
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
|
||||||
|
)
|
||||||
|
public class AutoAppConfig {
|
||||||
|
|
||||||
|
@Bean(name = "memoryMemberRepository") // 수동 빈, 자동 빈 이름 같을 경우 수동 빈이 우선순위를 가진다.( 최근 스프링 부트는 에러 )
|
||||||
|
MemberRepository memberRepository() {
|
||||||
|
return new MemoryMemberRepository();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@ package com.example.basic.discount;
|
|||||||
|
|
||||||
import com.example.basic.member.Grade;
|
import com.example.basic.member.Grade;
|
||||||
import com.example.basic.member.Member;
|
import com.example.basic.member.Member;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class RateDiscountPolicy implements DiscountPolicy {
|
public class RateDiscountPolicy implements DiscountPolicy {
|
||||||
|
|
||||||
private final int discountPercent = 10;
|
private final int discountPercent = 10;
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
package com.example.basic.member;
|
package com.example.basic.member;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class MemberServiceImpl implements MemberService {
|
public class MemberServiceImpl implements MemberService {
|
||||||
|
|
||||||
private final MemberRepository memberRepository;
|
private final MemberRepository memberRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
public MemberServiceImpl(MemberRepository memberRepository) {
|
public MemberServiceImpl(MemberRepository memberRepository) {
|
||||||
this.memberRepository = memberRepository;
|
this.memberRepository = memberRepository;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package com.example.basic.member;
|
package com.example.basic.member;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class MemoryMemberRepository implements MemberRepository {
|
public class MemoryMemberRepository implements MemberRepository {
|
||||||
|
|
||||||
private static final Map<Long, Member> store = new HashMap<>();
|
private static final Map<Long, Member> store = new HashMap<>();
|
||||||
|
|||||||
@@ -3,12 +3,16 @@ package com.example.basic.order;
|
|||||||
import com.example.basic.discount.DiscountPolicy;
|
import com.example.basic.discount.DiscountPolicy;
|
||||||
import com.example.basic.member.Member;
|
import com.example.basic.member.Member;
|
||||||
import com.example.basic.member.MemberRepository;
|
import com.example.basic.member.MemberRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
public class OrderServiceImpl implements OrderService {
|
public class OrderServiceImpl implements OrderService {
|
||||||
|
|
||||||
private final MemberRepository memberRepository;
|
private final MemberRepository memberRepository;
|
||||||
private final DiscountPolicy discountPolicy;
|
private final DiscountPolicy discountPolicy;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
|
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
|
||||||
this.memberRepository = memberRepository;
|
this.memberRepository = memberRepository;
|
||||||
this.discountPolicy = discountPolicy;
|
this.discountPolicy = discountPolicy;
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.example.basic.scan;
|
||||||
|
|
||||||
|
import com.example.basic.AutoAppConfig;
|
||||||
|
import com.example.basic.member.MemberService;
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
|
||||||
|
class AutoAppConfigTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void basicScan() {
|
||||||
|
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);
|
||||||
|
|
||||||
|
MemberService memberService = ac.getBean(MemberService.class);
|
||||||
|
assertThat(memberService).isInstanceOf(MemberService.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.basic.scan.filter;
|
||||||
|
|
||||||
|
@MyIncludeComponent
|
||||||
|
public class BeanA {
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.basic.scan.filter;
|
||||||
|
|
||||||
|
@MyExcludeComponent
|
||||||
|
public class BeanB {
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.example.basic.scan.filter;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
|
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.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ComponentFilterAppConfigTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void filterScan() {
|
||||||
|
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
|
||||||
|
BeanA beanA = ac.getBean("beanA", BeanA.class);
|
||||||
|
assertThat(beanA).isNotNull();
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.example.basic.scan.filter;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface MyExcludeComponent {
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.example.basic.scan.filter;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Target(ElementType.TYPE)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Documented
|
||||||
|
public @interface MyIncludeComponent {
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user