# Spring / @ComponentScan, @Component
Study Repository

Spring / @ComponentScan, @Component

by rlaehddnd0422

@ComponentScan์„ ์ด์šฉํ•œ Bean ๋“ฑ๋ก

 ์ง€๊ธˆ๊นŒ์ง€ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•  ๋•Œ๋Š” ์ž๋ฐ” ์ฝ”๋“œ์˜ @Bean์„ ํ†ตํ•ด์„œ๋‚˜ ๋˜๋Š” XML์˜ <bean> ์„ ํ†ตํ•ด ์„ค์ •์ •๋ณด(AppConfig)์— ์ง์ ‘ ๋“ฑ๋กํ•  ๋นˆ๋“ค์„ ๋‚˜์—ดํ–ˆ์—ˆ๋‹ค.

 

์˜ˆ์ œ์—์„œ๋Š” ๋ช‡ ๊ฐœ ์—†์—ˆ์ง€๋งŒ, ๋“ฑ๋กํ•ด์•ผ ํ•  ๋นˆ์ด ์ˆ˜์‹ญ๊ฐœ, ์ˆ˜๋ฐฑ๊ฐœ๊ฐ€ ๋˜๋ฉด ์ผ์ผ์ด ๋“ฑ๋กํ•˜๊ธฐ ๊ท€์ฐฎ๊ณ , ์„ค์ •์ •๋ณด ์ž์ฒด๊ฐ€ ์ปค์ ธ ๋ˆ„๋ฝํ•˜๋Š” ๋ฌธ์ œ๊นŒ์ง€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ทธ๋ž˜์„œ ์Šคํ”„๋ง์€ ์„ค์ •์ •๋ณด ์—†์ด ์ž๋™์œผ๋กœ ์Šคํ”„๋ง ๋นˆ์„ ๋“ฑ๋กํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด๋ผ๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณต

+ ์˜์กด๊ด€๊ณ„๋„ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•˜๋Š” @Autowired๋ผ๋Š” ๊ธฐ๋Šฅ๋„ ์ œ๊ณต

package hello.core;

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)๋„ ์ž๋™์œผ๋กœ ๋“ฑ๋ก๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ธฐ์กด์— ์žˆ๋˜ AppConfig์— ์˜ํ–ฅ์ด ๊ฐ€์ง€ ์•Š๋„๋ก excludeFilter๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ธฐ์กด์˜ ์„ค์ •์ •๋ณด๋Š” ์Šค์บ” ๋Œ€์ƒ์—์„œ ์ œ์™ธ์‹œ์ผฐ๋‹ค.

(๋ณดํ†ต ์„ค์ • ์ •๋ณด๋ฅผ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•˜์ง€๋Š” ์•Š์ง€๋งŒ, ๊ธฐ์กด ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ตœ๋Œ€ํ•œ ๋‚จ๊ธฐ๊ณ  ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ)

@ComponentScan์„ ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ @Component ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค๋ฅผ ์Šค์บ”ํ•ด์„œ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

 

์ด์ œ ๋“ฑ๋กํ•  ๋นˆ๋“ค์— @Component๋ฅผ ๋ถ™ํ˜€ ๋นˆ์— ๋“ฑ๋กํ•˜๊ณ  ํ…Œ์ŠคํŠธ ํ•ด๋ณด์ž.

 

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

 

+ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…ํ•  ๋•Œ์—๋Š” @Autowired๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.

@Component
public class MemberServiceImp implements MemberService{
    private final MemberRepository memberRepository;

    @Autowired // ac.getBean(MemberRepository.class)
    public MemberServiceImp(MemberRepository memberRepository) {
        this.memberRepository = memberRepository;
    }
@Component
public class OrderServiceImp implements OrderService{
    private final MemberRepository memberRepository;
    private final DiscountPolicy discountPolicy;

    @Autowired
    public OrderServiceImp(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
        this.memberRepository = memberRepository;
        this.discountPolicy = discountPolicy;
    }

 

์ด ์ „ AppConfig์—์„œ๋Š” @Bean์œผ๋กœ ์ง์ ‘ ์„ค์ •์ •๋ณด๋ฅผ ์ž‘์„ฑํ–ˆ๊ณ , ์˜์กด๊ด€๊ณ„๋„ ์ง์ ‘ ๋ช…์‹œํ–ˆ๋‹ค.

์ด์ œ๋Š” ์ด๋Ÿฐ ์„ค์ • ์ •๋ณด ์ž์ฒด๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์—, ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…๋„ ์ด ํด๋ž˜์Šค ์•ˆ์—์„œ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค.

@Autowired๋Š” ์˜์กด๊ด€๊ณ„๋ฅผ ์ž๋™์œผ๋กœ ์ฃผ์ž…ํ•ด์ค€๋‹ค.

 

Testcode

public class AutoAppConfigTest {

    @Test
    void basicBean()
    {
        ApplicationContext ac = new AnnotationConfigApplicationContext(AutoAppConfig.class);
        MemberService bean = ac.getBean(MemberService.class);
        Assertions.assertThat(bean).isInstanceOf(MemberServiceImp.class);
    }
}

ํ†ต๊ณผ

์„ค์ • ์ •๋ณด๋กœ AutoAppConfig ํด๋ž˜์Šค๋ฅผ ๋„˜๊ฒจ์ค€๋‹ค.

์‹คํ–‰ํ•ด๋ณด๋ฉด ๊ธฐ์กด๊ณผ ๊ฐ™์ด ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋กœ๊ทธ๋ฅผ ์ž˜๋ณด๋ฉด ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค

 

๋™์ž‘์›๋ฆฌ๋ฅผ ์•Œ์•„๋ณด์ž.

 

1. @ComponentScan

์šฐ์„  @ComponentScan์€ @Component ๊ฐ€ ๋ถ™์€ ๋ชจ๋“  ํด๋ž˜์Šค๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

์ด ๋•Œ ๋นˆ์˜ ๊ธฐ๋ณธ์ด๋ฆ„์€ ํด๋ž˜์Šค๋ช…์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์•ž๊ธ€์ž๊ฐ€ ์†Œ๋ฌธ์ž๋กœ ๋ฐ”๋€œ

- ๋นˆ ์ด๋ฆ„ ๊ธฐ๋ณธ ์ „๋žต : MemberServiceImp -> memberServiceImp

๋งŒ์•ฝ ๋นˆ์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด @Component("์ง€์ •์ด๋ฆ„")์œผ๋กœ ๋ถ€์—ฌํ•˜๋ฉด ๋œ๋‹ค.

 

2. @Autowired ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…

์ƒ์„ฑ์ž์— @Autowired๋ฅผ ์ง€์ •ํ•˜๋ฉด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ž๋™์œผ๋กœ ๋นˆ์„ ์ฐพ์•„ ์˜์กด๊ด€๊ณ„๋ฅผ ์ฃผ์ž…ํ•ด์ค€๋‹ค.

ac.getBean(MemberRepository.class)์™€ ๋™์ผํ•˜๋‹ค๊ณ  ๋ณด๋ฉด ๋จ.

์ด๋•Œ ๊ธฐ๋ณธ ์กฐํšŒ ์ „๋žต์€ ํƒ€์ž…์„ ๋ณด๊ณ  ๋นˆ์„ ์ฐพ์•„์„œ ์ฃผ์ž…ํ•˜๋Š”๋ฐ ๋งŒ์•ฝ ๊ฐ™์€ ํƒ€์ž…์ด ์ปดํฌ๋„ŒํŠธ๋กœ ์—ฌ๋Ÿฌ ๊ฐœ ์ง€์ • ๋˜์–ด ์žˆ์œผ๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ??

๋’ค์—์„œ ๋‹ค๋ฃจ๋„๋ก ํ•˜๊ฒ ๋‹ค. 

 

 


@ComponentScan ํƒ์ƒ‰ ์œ„์น˜

๋ชจ๋“  ์ž๋ฐ” ํด๋ž˜์Šค๋ฅผ ๋‹ค ์ปดํฌ๋„ŒํŠธ ์Šค์บ”ํ•˜๋ฉด ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์—, ํƒ์ƒ‰ํ•  ํŒจํ‚ค์ง€์˜ ์‹œ์ž‘ ์œ„์น˜๋ฅผ @ComponentScan์— basePackages ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ง€์ •ํ•ด์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

@ComponentScan(
          basePackages = {"hello.core"} )

basePackages : ์‹œ์ž‘์œ„์น˜๋ฅผ ์ง€์ •ํ•ด์„œ ํ•ด๋‹น์œ„์น˜์˜ ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ ๋ชจ๋‘ ํƒ์ƒ‰ํ•œ๋‹ค.

basePackages = {"hello.core", "hello.service"} ์ด๋ ‡๊ฒŒ ์—ฌ๋Ÿฌ ์‹œ์ž‘ ์œ„์น˜๋ฅผ ์ง€์ •ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

๋งŒ์•ฝ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด @ComponentScan์ด ๋ถ™์€ ์„ค์ • ์ •๋ณด ํด๋ž˜์Šค์˜ ํŒจํ‚ค์ง€๊ฐ€ ์‹œ์ž‘ ์œ„์น˜๊ฐ€ ๋œ๋‹ค.

 

๊ถŒ์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•

๊ทธ๋ƒฅ @ComponentScan์ด ๋ถ™์€ ์„ค์ •์ •๋ณด ํด๋ž˜์Šค์˜ ์œ„์น˜๋ฅผ ํ”„๋กœ์ ํŠธ ์ตœ์ƒ๋‹จ์— ๋‘์ž.

์ตœ๊ทผ ์Šคํ”„๋ง ๋ถ€ํŠธ๋„ ์ด ๋ฐฉ๋ฒ•์„ default๋กœ ์ œ๊ณตํ•œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

์˜ˆ๋ฅผ๋“ค์–ด

kdo6301.core.hello

kdo6301.core.hello.service

kdo6301.core.hello.repository ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜๋ฉด

 

kdo6301.core.hello์— ์„ค์ •์ •๋ณด ํด๋ž˜์ŠคํŒŒ์ผ์„ ๋‘์ž๋Š” ๊ฒƒ์ด๋‹ค.

ํด๋ž˜์ŠคํŒŒ์ผ์„ ์ตœ์ƒ๋‹จ์— ๋‘๊ณ  @ComponentScan๋งŒ ๋ถ™ํ˜€์ฃผ๊ณ  basePackage ์ง€์ • ์ƒ๋žต.

์ด๊ฒŒ ์ œ์ผ ๊น”๋”ํ•˜๋‹ค.

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด kdo6301.core.hello ๋ฅผ ํฌํ•จํ•œ ํ•˜์œ„๋Š” ๋ชจ๋‘ ์ž๋™์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ๋Œ€์ƒ์ด ๋œ๋‹ค.

โ—๏ธ ์ฐธ๊ณ ๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์Šคํ”„๋ง ๋ถ€ํŠธ์˜ ๋Œ€ํ‘œ ์‹œ์ž‘ ์ •๋ณด์ธ @SpringBootApplication ๋ฅผ ์ด ํ”„๋กœ์ ํŠธ ์‹œ์ž‘ ๋ฃจํŠธ ์œ„์น˜์— ๋‘๋Š” ๊ฒƒ์ด ๊ด€๋ก€์ด๋‹ค. (๊ทธ๋ฆฌ๊ณ  ์ด ์„ค์ •์•ˆ์— ๋ฐ”๋กœ @ComponentScan ์ด ๋“ค์–ด์žˆ๋‹ค!)

 

@ComponentScan ๊ธฐ๋ณธ ๋Œ€์ƒ

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์€ ๊ธฐ๋ณธ์ ์œผ๋กœ @Component ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ 

@Controller, @Service, @Repository, @Configuration๋„ ์ถ”๊ฐ€๋กœ ๋Œ€์ƒ์— ํฌํ•จํ•œ๋‹ค.

 

1. @Controller : Spring MVC Controller์—์„œ ์‚ฌ์šฉ

2. @Service : Spring Buisness ๋กœ์ง์—์„œ ์‚ฌ์šฉ

3. @Repository : Spring ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต์—์„œ ์‚ฌ์šฉ

4. @Configuration : Spring ์„ค์ • ์ •๋ณด์—์„œ ์‚ฌ์šฉ (AppConfig)

 

@Repository

@Repository๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด ๋นˆ ๋“ฑ๋ก ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์Šคํ”„๋ง ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต์œผ๋กœ ์ธ์‹ํ•˜๊ณ , ๋ฐ์ดํ„ฐ ๊ณ„์ธต์˜ ์˜ˆ์™ธ๋ฅผ ์Šคํ”„๋ง ์˜ˆ์™ธ๋กœ ๋ณ€ํ™˜ํ•ด์ค€๋‹ค. 

 

@Controller

@Controller๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด ๋นˆ ๋“ฑ๋ก ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์Šคํ”„๋ง MVC Controller๋กœ ์ธ์‹ํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

 

@Service

@Service๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์–ด ๋นˆ ๋“ฑ๋ก์„ ํ•ด์ฃผ์ง€๋งŒ ๊ทธ ์™ธ์˜ ๋ณ„๋‹ค๋ฅธ ๊ธฐ๋Šฅ์€ ์ œ๊ณตํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋‹จ์ˆœํ•˜๊ฒŒ ํ•ต์‹ฌ ๋น„์ฆˆ๋‹ˆ์Šค ๊ณ„์ธต์„ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ธ์‹ํ•˜๊ธฐ ์‰ฝ๊ฒŒ ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ

 

@Configuration

@Configuration์€ ๋นˆ๋“ฑ๋ก ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์•ž์„œ ๋ณด์•˜๋“ฏ์ด ์Šคํ”„๋ง ์„ค์ • ์ •๋ณด๋กœ ์ธ์‹ํ•˜๊ณ , ์Šคํ”„๋ง ๋นˆ์ด ์‹ฑ๊ธ€ํ†ค์„ ์œ ์ง€ํ•˜๋„๋ก ์ถ”๊ฐ€ ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.

 

โ—๏ธ ์ฐธ๊ณ : useDefaultFilters ์˜ต์…˜์€ ๊ธฐ๋ณธ์œผ๋กœ ์ผœ์ ธ์žˆ๋Š”๋ฐ, ์ด ์˜ต์…˜์„ ๋„๋ฉด ๊ธฐ๋ณธ ์Šค์บ” ๋Œ€์ƒ๋“ค์ด ์ œ์™ธ๋œ๋‹ค. ๊ทธ๋ƒฅ ์ด๋Ÿฐ ์˜ต์…˜์ด ์žˆ๊ตฌ๋‚˜ ์ •๋„ ์•Œ๊ณ  ๋„˜์–ด๊ฐ€์ž.

 


@ComponentScan Filter

@ComponentScan์—๋Š” ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋Œ€์ƒ์„ ์ถ”๊ฐ€๋กœ ์ง€์ •ํ•˜๊ฑฐ๋‚˜ ์ œ์™ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

@MyIncludeComponent
public class BeanA {
}
@MyExcludeComponent
public class BeanB {
}

 

 

package hello.core.scan;


import hello.core.scan.filter.BeanA;
import hello.core.scan.filter.BeanB;
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 ComponentFilterTest {
    
    @Configuration
    @ComponentScan(
            includeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyIncludeComponent.class),
            excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = MyExcludeComponent.class)
    )
    static class ComponentFilterAppConfig{
    }

    @Test
    void filterScan()
    {
        ApplicationContext ac = new AnnotationConfigApplicationContext(ComponentFilterAppConfig.class);
        BeanA beanA = ac.getBean(BeanA.class);
        System.out.println("beanA = " + beanA);
        assertThat(beanA).isNotNull();

        Assertions.assertThrows(
                NoSuchBeanDefinitionException.class,
                () -> ac.getBean(BeanB.class)
        );


    }
    
}
  • includeFilters์— MyIncludeComponent ์• ๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์„œ BeanA๊ฐ€ ์Šคํ”„๋ง ๋นˆ์— ๋“ฑ๋ก๋œ๋‹ค.
  • excludeFilters์— MyExcludeComponent ์• ๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์„œ BeanB๋Š” ์Šคํ”„๋ง ๋นˆ์— ๋“ฑ๋ก๋˜์ง€ ์•Š๋Š”๋‹ค.

 

 

์ฐธ๊ณ : @Component ๋ฉด ์ถฉ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, includeFilters ๋ฅผ ์‚ฌ์šฉํ•  ์ผ์€ ๊ฑฐ์˜ ์—†๋‹ค. excludeFilters ๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ด์œ ๋กœ ๊ฐ„ํ˜น ์‚ฌ์šฉํ•  ๋•Œ๊ฐ€ ์žˆ์ง€๋งŒ ๋งŽ์ง€๋Š” ์•Š๋‹ค.

ํŠนํžˆ ์ตœ๊ทผ ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์„ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•˜๋Š”๋ฐ, ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ์˜ต์…˜์„ ๋ณ€๊ฒฝํ•˜๋ฉด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ๋ณด๋‹ค๋Š” ์Šคํ”„๋ง์˜ ๊ธฐ๋ณธ ์„ค์ •์— ์ตœ๋Œ€ํ•œ ๋งž์ถ”์–ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•˜๊ณ , ์„ ํ˜ธํ•˜๋Š” ํŽธ์ด๋‹ค.

 

 


์ค‘๋ณต๋“ฑ๋ก๊ณผ ์ถฉ๋Œ

 

์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก vs ์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก

@Component("myService")
public class MemberServiceImp implements MemberService{
    private final MemberRepository memberRepository;
@Component("myService")
public class OrderServiceImp implements OrderService{
    private final MemberRepository memberRepository;
    private final DiscountPolicy discountPolicy;

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜๋™์œผ๋กœ @Component๋ฅผ ์ง€์ •ํ•ด์„œ ์ด๋ฆ„์ด ๊ฐ™์€ ๋นˆ์„ ๋“ฑ๋กํ•˜์—ฌ ํ…Œ์ŠคํŠธ๋ฅผ ๋Œ๋ ค๋ณด๋ฉด

 

ConflictBeanDefinitionException ์˜ˆ์™ธ ๋ฐœ์ƒ

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜์—ฌ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

 

์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก vs ์ž๋™ ๋นˆ ๋“ฑ๋ก

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

    @Bean(name="memoryMemberRepository")
    public MemberRepository memberRepository()
    {
        return new MemoryMemberRepository();
    }
}

์ˆ˜๋™์œผ๋กœ ๋“ฑ๋ก๋œ ๋นˆ์ด ์šฐ์„ ๊ถŒ์„ ๊ฐ€์ง„๋‹ค! ์ˆ˜๋™ ๋นˆ์ด ์ž๋™ ๋นˆ์„ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•ด๋ฒ„๋ฆผ.

17:43:11.644 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'memoryMemberRepository' with a different definition 

 

๋ฌผ๋ก  ๊ฐœ๋ฐœ์ž๊ฐ€ ์˜๋„์ ์œผ๋กœ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ๊ธฐ๋Œ€ํ–ˆ๋‹ค๋ฉด, ์ž๋™ ๋ณด๋‹ค๋Š” ์ˆ˜๋™์ด ์šฐ์„ ๊ถŒ์„ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด ์ข‹๋‹ค. ํ•˜์ง€๋งŒ ํ˜„์‹ค์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ์˜๋„์ ์œผ๋กœ ์„ค์ •ํ•ด์„œ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๊ธฐ ๋ณด๋‹ค๋Š” ์—ฌ๋Ÿฌ ์„ค์ •๋“ค์ด ๊ผฌ์—ฌ์„œ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋Œ€๋ถ€๋ถ„์ด๋ผ๊ณ  ํ•œ๋‹ค.
๊ทธ๋Ÿฌ๋ฉด ์ •๋ง ์žก๊ธฐ ์–ด๋ ค์šด ๋ฒ„๊ทธ๊ฐ€ ๋งŒ๋“ค์–ด์ง„๋‹ค. ํ•ญ์ƒ ์žก๊ธฐ ์–ด๋ ค์šด ๋ฒ„๊ทธ๋Š” ์• ๋งคํ•œ ๋ฒ„๊ทธ๋‹ค.

๊ทธ๋ž˜์„œ ์ตœ๊ทผ ์Šคํ”„๋ง ๋ถ€ํŠธ์—์„œ๋Š” ์ˆ˜๋™ ๋นˆ ๋“ฑ๋ก๊ณผ ์ž๋™ ๋นˆ ๋“ฑ๋ก์ด ์ถฉ๋Œ๋‚˜๋ฉด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋„๋ก ๊ธฐ๋ณธ ๊ฐ’์„ ๋ฐ”๊พธ์—ˆ๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ์ธ CoreApplication ์„ ์‹คํ–‰ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

<์ฐธ๊ณ  ์ž๋ฃŒ>

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8/dashboard

 

์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ - ์ธํ”„๋Ÿฐ | ๊ฐ•์˜

์Šคํ”„๋ง ์ž…๋ฌธ์ž๊ฐ€ ์˜ˆ์ œ๋ฅผ ๋งŒ๋“ค์–ด๊ฐ€๋ฉด์„œ ์Šคํ”„๋ง์˜ ํ•ต์‹ฌ ์›๋ฆฌ๋ฅผ ์ดํ•ดํ•˜๊ณ , ์Šคํ”„๋ง ๊ธฐ๋ณธ๊ธฐ๋ฅผ ํ™•์‹คํžˆ ๋‹ค์งˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค., - ๊ฐ•์˜ ์†Œ๊ฐœ | ์ธํ”„๋Ÿฐ...

www.inflearn.com

 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ