๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/Web

[Spring] ๋นˆ(bean)์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•

by ynzu๐Ÿค 2022. 7. 1.
๋ฐ˜์‘ํ˜•

 

1. ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊ณผ ์ž๋™ ์˜์กด๊ด€๊ณ„ ์„ค์ •

์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ์›๋ฆฌ
@Component ์–ด๋…ธํ…Œ์ด์…˜์ด ์„ ์–ธ๋˜์–ด ์žˆ์œผ๋ฉด ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ์ž๋™ ๋“ฑ๋ก
  • @Controller ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ์ž๋™ ๋“ฑ๋ก๋œ ์ด์œ ๋„ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๋•Œ๋ฌธ! (@Controller ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ดํŽด๋ณด๋ฉด @Component๊ฐ€ ์„ ์–ธ๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Œ)
  • @Service, @Repository๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ @Component๊ฐ€ ์„ ์–ธ๋˜์–ด ์žˆ์–ด ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ์ž๋™ ๋“ฑ๋ก ๋œ๋‹ค.

 

 ์˜ˆ์‹œ 

  • OrderController.java
@Controller 
public class OrderController {

    private final OrderService orderService;
    
    @Autowired
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }
}
  • OrderService.java
@Service
public class OrderService {

    private final OrderRepository orderRepository;

    @Autowired
    public OrderService(OrderRepository orderRepository) {
        orderRepository = orderRepository;
    }
    
}

์ƒ์„ฑ์ž์— @Autowired๊ฐ€ ์žˆ์œผ๋ฉด ์Šคํ”„๋ง์ด ์—ฐ๊ด€๋œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ์ฐพ์•„์„œ ๊ฐ์ฒด ์ƒ์„ฑ ์‹œ์ ์— ํ•ด๋‹น ๋นˆ์„ ์ฐพ์•„์„œ ์ฃผ์ž…ํ•œ๋‹ค. ์ƒ์„ฑ์ž๊ฐ€ ํ•˜๋‚˜๋งŒ ์žˆ์œผ๋ฉด ์ƒ๋žต ๊ฐ€๋Šฅ!  -> ์ด๋ ‡๊ฒŒ ๊ฐ์ฒด์˜ ์˜์กด ๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์„ DI๋ผ๊ณ  ํ•œ๋‹ค!

์ฐธ๊ณ  : @Autowired ๋ฅผ ํ†ตํ•œ DI๋Š” @Controller , @Service ๋“ฑ๊ณผ ๊ฐ™์ด ์Šคํ”„๋ง์ด ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด์—์„œ๋งŒ ๋™์ž‘ํ•œ๋‹ค. ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜์ง€ ์•Š๊ณ  ๋‚ด๊ฐ€ ์ง์ ‘ ์ƒ์„ฑํ•œ ๊ฐ์ฒด์—์„œ๋Š” ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค.

  • OrderRepository.java
@Repository
public class OrderRepository {}

์ด๋ ‡๊ฒŒ ์„ค์ •ํ•˜๋ฉด OrderController - > OrderService -> OrderRepository๊ตฌ์กฐ๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.   

 


 

2. ์ž๋ฐ” ์ฝ”๋“œ๋กœ ์ง์ ‘ ์Šคํ”„๋ง ๋นˆ ๋“ฑ๋กํ•˜๊ธฐ

OrderService.java์™€ OrderRepository.java์˜ @Service, @Repository, @Autowired ์–ด๋…ธํ…Œ์ด์…˜์„ ์ œ๊ฑฐํ•œ๋‹ค. (OrderController.java๋Š” ์œ ์ง€)

 ์˜ˆ์‹œ 

  • OrderConfig.java
@Configuration
public class OrderConfig {
 
    @Bean
    public OrderService orderService() {
        return new OrderService(orderRepository());
    }

    @Bean
    public OrderRepository orderRepository() {
      return new OrderRepository();
    }
}

@Configuration ์–ด๋…ธํ…Œ์ด์…˜์ด ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฉ”์†Œ๋“œ์— @Bean ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜๋ฉด, ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

 

3. XML๋กœ ์„ค์ •

application.xml์— bean์„ ์ง์ ‘ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ ์ตœ๊ทผ์—๋Š” ๊ฑฐ์˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

ํ•˜์ง€๋งŒ ์•„์ง ๋งŽ์€ ๋ ˆ๊ฑฐ์‹œ ํ”„๋กœ์ ํŠธ๊ฐ€ XML๋กœ ๋˜์–ด ์žˆ๊ธฐ๋„ ํ•˜๊ณ , ์ปดํŒŒ์ผ ์—†์ด ๋นˆ ์„ค์ • ์ •๋ณด๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ๋‹ค.

<beans ...>

    <bean id="orderService" class="com.test.order.OrderService">
    	<constructor-arg name="orderRepository" ref="orderRepository"/>
    </bean>

    <bean id="orderRepository" class="com.test.order.OrderRepository">
    </bean>

</beans>

 

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€