/* ์ด ๊ธ์ ๊น์ํ๋์ ๊ฐ์๋ฅผ ๋ณด๊ณ ์ ๋ฆฌํ๋ ค๊ณ ์์ฑํ ๊ธ์ ๋๋ค.
๊ฐ์ธ์ ์ธ ๊ณต๋ถ๋ฅผ ์ํด ์ฌ๋ฆฌ๋ ๊ธ์ด๋ฏ๋ก ์ค๊ฐ ์ค๊ฐ ์ฝ๋๋ ์๋ต๋์์ต๋๋ค. */
์คํ๋ง ํต์ฌ ์๋ฆฌ - ๊ธฐ๋ณธํธ - ์ธํ๋ฐ | ๊ฐ์
์คํ๋ง ์ ๋ฌธ์๊ฐ ์์ ๋ฅผ ๋ง๋ค์ด๊ฐ๋ฉด์ ์คํ๋ง์ ํต์ฌ ์๋ฆฌ๋ฅผ ์ดํดํ๊ณ , ์คํ๋ง ๊ธฐ๋ณธ๊ธฐ๋ฅผ ํ์คํ ๋ค์ง ์ ์์ต๋๋ค., ์คํ๋ง ํต์ฌ ์๋ฆฌ๋ฅผ ์ดํดํ๊ณ , ์ฑ์ฅํ๋ ๋ฐฑ์๋ ๊ฐ๋ฐ์๊ฐ ๋์ด๋ณด์ธ์! ๐ข
์ปดํฌ๋ํธ ์ค์บ๊ณผ ์์กด๊ด๊ณ ์๋ ์ฃผ์ ์์ํ๊ธฐ
1. ์ฝ๋ ์์ฑ
AutoAppConfig
@Configuration
@ComponentScan(
excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
)
public class AutoAppConfig {
}
โ ์ปดํฌ๋ํธ ์ค์บ์ ์ฌ์ฉํ๋ ค๋ฉด ๋จผ์ @ComponentScan์ ์ค์ ์ ๋ณด์ ๋ถ์ฌ์ฃผ๋ฉด ๋๋ค.
โ ๊ธฐ์กด AppConfig์๋ ๋ค๋ฅด๊ฒ @Bean์ผ๋ก ๋ฑ๋กํ ํด๋์ค๊ฐ ํ๋๋ ์๋ค.
โ ์ปดํฌ๋ํธ ์ค์บ์ ์ฌ์ฉํ๋ฉด @Configuration์ด ๋ถ์ ์ค์ ์ ๋ณด๋ ์๋๋ฑ๋ก ๋๋๋ฐ ์์ ๋ง๋ค์๋ AppConfig, TestConfig ๋ค์ด ํจ๊ป ๋ฑ๋ก๋ผ์ ์คํ๋๋ค. ๊ทธ๋์ excludeFilter๋ฅผ ์ด์ฉํ์ฌ ์ค์ ์ ๋ณด๋ ์ปดํฌ๋ํธ ์ค์บ ๋์์์ ์ ์ธํ๋ค.
(๋ณดํต ์ค์ ์ ๋ณด๋ฅผ ์ปดํฌ๋ํธ ์ค์บ ๋์์์ ์ ์ธํ์ง ์์ง๋ง, ๊ธฐ์กด ์์ ์ฝ๋๋ฅผ ์ต๋ํ ์ ์งํ๊ณ ์ ์ด ๋ฐฉ๋ฒ์ ์ ํํ๋ค๊ณ ํ์ฌ)
๐ก ๊ถ๊ธ์ฆ
: excludeFilters ์ ๋ํด์
โพ excludeFilters๋ @ComponentScan ์ด๋ ธํ ์ด์ ๋ด์์ ์ฌ์ฉ๋๋ ํํฐ๋ฅผ ์ ์ํ๋ ์ญํ ์ ํ๋ฉฐ, ์ด ํํฐ๋ฅผ ํตํด ์ค์บ ๊ณผ์ ์์ ํน์ ํ์ ์ด๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋ ํด๋์ค๋ฅผ ์ ์ธ ํ ์ ์๋ค.
โพ FilterType ์ข ๋ฅ
- ANNOTATION : ํน์ ์ด๋ ธํ ์ด์ ์ ๊ฐ์ง๊ณ ์๋ ํด๋์ค ํํฐ๋ง
- ASSIGNABLE_TPYE : ์ง์ ๋ ํ์ ๊ณผ ํ ๋น ํธํ์ฑ์ด ์๋ ํด๋์ค ํํฐ๋ง
- ASPECTJ : AspectJํจํด์ ์ฌ์ฉํ์ฌ ํด๋์ค ํํฐ๋ง
- REGEX : ์ ๊ท ํํ์์ ์ฌ์ฉํ์ฌ ํด๋์ค ์ด๋ฆ ํํฐ๋ง
- CUSTOM : ์ฌ์ฉ์ ์ ์ ํํฐ๋ฅผ ์ ์ํ ๋ ์ฌ์ฉ
MemoryMemberRepository
@Component
public class MemoryMemberRepository implements MemberRepository{
RateDiscountPolicy
@Component
public class RateDiscountPolicy implements DiscountPolicy{
MemberServiceImpl
@Component
public class MemberServiceImpl implements MemberService{
private final MemberRepository memberRepository;
@Autowired
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
OrderServiceImpl
@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;
}
โ ComponentScan์ @Component ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๋ฅผ ์ค์บํด์ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํ๋ฏ๋ก ์ปดํฌ๋ํธ ์ค์บ์ ๋์์ด ๋๋๋ก ์ด๋ ธํ ์ด์ ์ ๋ถ์ฌ์ฃผ์๋ค.
โ AppConfig์์๋ @Bean์ผ๋ก ์ง์ ์ค์ ์ ๋ณด๋ฅผ ์์ฑํ๊ณ ์์กด๊ด๊ณ๋ฅผ ์ง์ ๋ช ์ํ๋๋ฐ ์ด์ ๋ ์ด๋ฐ ์ค์ ์ ๋ณด ์์ฒด๊ฐ ์๊ธฐ ๋๋ฌธ์ ์์กด๊ด๊ณ ์ฃผ์ ๋ ๊ฐ ํด๋์ค ์์์ ํด๊ฒฐํด์ผ ํ๋ค.
โ @Autowired๋ ์์กด๊ด๊ณ๋ฅผ ์๋์ผ๋ก ์ฃผ์ ํด์ฃผ๋ฉฐ, ์ฌ๋ฌ ์์กด๊ด๊ณ๋ ํ๋ฒ์ ์ฃผ์ ๋ฐ์ ์ ์๋ค. ์์ธํ ๋ฃฐ์ ์กฐ๊ธ ๋ค์ ์ค๋ช !
AutoAppConfigTest
public class AutoAppConfigTest {
@Test
void basicScan() {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);
MemberService memberService = ac.getBean(MemberService.class);
assertThat(memberService).isInstanceOf(MemberService.class);
}
}
โ ํ ์คํธ ์ฝ๋ ์์ฑ์ ๋ฌ๋ผ์ง ์ ์ ์ค์ ์ ๋ณด๋ก AutoConfig.class๋ฅผ ๋๊ฒจ์ค๋ค๋ ๊ฒ. ์คํํด๋ณด๋ฉด ๊ธฐ์กด๊ณผ ๊ฐ์ด ์ ๋์ ํจ.
โ ๋ก๊ทธ๋ฅผ ๋ณด๋ฉด @Component ์ด๋ ธํ ์ด์ ์ด ๋ถ์ ํด๋์ค๊ฐ ์ฑ๊ธํค ๋น์ผ๋ก ์์ฑ์ด ๋๊ณ , @Autowired๊ฐ ๋ถ์ ์ด๋ ธํ ์ด์ ์ ์๋์ผ๋ก ์์กด๊ด๊ณ๊ฐ ์ฃผ์ ๋๊ฑธ ๋ณผ ์ ์๋ค.
2. ๋์ ์๋ ๊ทธ๋ฆผ์ผ๋ก ์์๋ณด๊ธฐ
@ComponentScan, @Component
โ @ConmponentScan์ @Component๊ฐ ๋ถ์ ๋ชจ๋ ํด๋์ค๋ฅผ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํ๋ค.
โ ์ด๋ ์คํ๋ง ๋น์ ๊ธฐ๋ณธ ์ด๋ฆ์ ํด๋์ค ๋ช ์ ์ฌ์ฉํ๋ ๋งจ ์๊ธ์๋ง ์๋ฌธ์๋ก ์ฌ์ฉํ๋ค.
(๋น ์ด๋ฆ์ ์ง์ ์ง์ ํ๊ณ ์ถ์ผ๋ฉด @Component("memberService") ๋ก ์ง์ ํ๋ฉด ๋๋ค)
@Autowired
โ ์์ฑ์์ @Autowired๋ฅผ ์ง์ ํ๋ฉด ์คํ๋ง ์ปจํ ์ด๋๊ฐ ์๋์ผ๋ก ํด๋น ์คํ๋ง ๋น์ ์ฐพ์์ ์ฃผ์ ํ๋ค.
โ ์ด๋ ๊ธฐ๋ณธ ์ ๋ต์ ํ์ ์ด ๊ฐ์ ๋น์ ์ฐพ์์ ์ฃผ์ ํ๋ฉฐ, ์์ฑ์๊ฐ ํ๋ผ๋ฏธํฐ๊ฐ ๋ง์๋ ์๋์ผ๋ก ์ฃผ์ ํด์ค.
(getBean(MemberRepository.class)์ ๋์ผํ๋ค๊ณ ์ดํดํ๋ฉด ๋จ)
โ ํ์ ์ด ์ฌ๋ฌ๊ฐ์ง์ธ ๊ฒฝ์ฐ๋ ๋ค์์ ์ค๋ช ํด์ฃผ์ ๋ค๊ณ ํจ