๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
TIL

01/30(ํ™”) ๋‚ด์ผ๋ฐฐ์›€์บ ํ”„ Java 27์ผ์ฐจ TIL - ๊ฐ™์€ Bean์ด 2๊ฐœ์ผ ๋•Œ

by ๊ฐ์ž๊ฐœ๋ฐœ๊พผ 2024. 1. 30.

 

๐Ÿ“–  ์˜ค๋Š˜์˜ ํ•™์Šต ํ‚ค์›Œ๋“œ

  • Bean

 

๐Ÿฅ”  ๊ฐ™์€ ํƒ€์ž… Bean ๋“ฑ๋ก

Food Interface
public interface Food {
    void eat();
}

 

Chicken
import org.springframework.stereotype.Component;

@Component
public class Chicken implements Food {
    @Override
    public void eat() {
        System.out.println("์น˜ํ‚จ์„ ๋จน์Šต๋‹ˆ๋‹ค.");
    }
}

 

Pizza
import org.springframework.stereotype.Component;

@Component
public class Pizza implements Food {
    @Override
    public void eat() {
        System.out.println("ํ”ผ์ž๋ฅผ ๋จน์Šต๋‹ˆ๋‹ค.");
    }
}

 

 

โ—๏ธ ํ…Œ์ŠคํŠธ์ฝ”๋“œ ์ž‘์„ฑ

@SpringBootTest
public class BeanTest {

    @Autowired
    Food food;
    
}

 

- Food food; ํ•„๋“œ์— @Autowired๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Bean์„ ์ฃผ์ž…ํ•˜๋ ค๊ณ  ์‹œ๋„ํ•  ๋•Œ, ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

- Food ํƒ€์ž…์˜ Bean ๊ฐ์ฒด๊ฐ€ ํ•œ ๊ฐœ ์ด์ƒ ์žˆ๋‹ค๋Š” ์˜ค๋ฅ˜๋กœ ์ธํ•˜์—ฌ ์ฃผ์ž…์ด ์•ˆ๋ฉ๋‹ˆ๋‹ค.

- ์–ด๋–ค Bean์„ ๋“ฑ๋กํ•ด์•ผ ํ• ์ง€ ๋ชฐ๋ผ ๋ฐœ์ƒํ•˜๋Š” ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค.

 

๐Ÿฅ”  ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๋“ฑ๋ก๋œ Bean ์ด๋ฆ„ ๋ช…์‹œ
@SpringBootTest
public class BeanTest {

    @Autowired
    Food pizza;
    
    @Autowired
    Food chicken;
    
}

 

- Bean ์ด๋ฆ„ pizza, chicken์„ ์ •ํ™•ํ•˜๊ฒŒ ๋ช…์‹œํ•ด ์ฃผ๋ฉด ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

- ์ด๋กœ์จ ์•Œ ์ˆ˜ ์žˆ๋Š” ์ ์€ @Autowired๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ๋Š” Bean Type(Food)์œผ๋กœ DI๋ฅผ ์ง€์›ํ•˜๋ฉฐ ์—ฐ๊ฒฐ์ด ๋˜์ง€ ์•Š์„ ๊ฒฝ์šฐ,

- Bean Name(Pizza, Chicken)์œผ๋กœ ์ฐพ๋Š” ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Primary ์‚ฌ์šฉ

 

@Component
@Primary
public class Chicken implements Food {
    @Override
    public void eat() {
        System.out.println("์น˜ํ‚จ์„ ๋จน์Šต๋‹ˆ๋‹ค.");
    }
}

//=========================================

@SpringBootTest
public class BeanTest {
    @Autowired
    Food food;
}

 

- @Primary๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ๊ฐ™์€ ํƒ€์ž…์˜ Bean์ด ์—ฌ๋Ÿฌ ๊ฐœ๊ฐ€ ์žˆ๋”๋ผ๊ณ  ์šฐ์„ ์ ์œผ๋กœ @Primary๊ฐ€ ์„ค์ •๋œ Bean ๊ฐ์ฒด๋ฅผ ์ฃผ์ž…ํ•ฉ๋‹ˆ๋‹ค.

 

@Qualifier ์‚ฌ์šฉํ•˜๊ธฐ
@Component
@Qualifier("pizza")
public class Pizza implements Food {
    @Override
    public void eat() {
        System.out.println("ํ”ผ์ž๋ฅผ ๋จน์Šต๋‹ˆ๋‹ค.");
    }
}

//=========================================

@SpringBootTest
public class BeanTest {

    @Autowired
    @Qualifier("pizza")
    Food food;
}

 

- ๊ฐ™์€ ํƒ€์ž…์˜ Bean๋“ค์— Qualifier์™€ Primary๊ฐ€ ๋™์‹œ์— ์ ์šฉ๋˜์–ด์žˆ๋‹ค๋ฉด Qualifier์˜ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋” ๋†’์Šต๋‹ˆ๋‹ค.

- ํ•˜์ง€๋งŒ Qualifier๋Š” ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฃผ์ž… ๋ฐ›๊ณ ์žํ•˜๋Š” ๊ณณ์— ํ•ด๋‹น Qualifier๋ฅผ ๋ฐ˜๋“œ์‹œ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

- ๋”ฐ๋ผ์„œ ๊ฐ™์€ ํƒ€์ž…์˜ Bean์ด ์—ฌ๋Ÿฌ ๊ฐœ ์žˆ์„ ๋•Œ๋Š” ๋ฒ”์šฉ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Bean ๊ฐ์ฒด์—๋Š” Primary๋ฅผ ์„ค์ •ํ•˜๊ณ 

- ์ง€์—ฝ์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” Bean ๊ฐ์ฒด์—๋Š” Qualifier๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“š  ์˜ค๋Š˜์˜ ํšŒ๊ณ 

์ ์  ์•Œ์•„์•ผ ํ•  ๊ฒƒ๋“ค์ด ๋งŽ์•„์ง€๊ณ  ์–ด๋ ต์Šต๋‹ˆ๋‹ค!

ํ•˜์ง€๋งŒ ์ฐจ๊ทผ์ฐจ๊ทผ ์Œ“์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.