ํฌ์ŠคํŠธ

Spring - IOC, DI, Bean

๐Ÿ˜DI(Dependency Injection)

์˜์กด๊ด€๊ณ„ ์ฃผ์ž…

  • ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด ๊ด€๊ณ„๋ฅผ ์™ธ๋ถ€์—์„œ ๊ฒฐ์ •ํ•˜๊ณ  ์ฃผ์ž…ํ•˜๋Š” ๋””์ž์ธํŒจํ„ด์ค‘ ํ•˜๋‚˜์ด๋‹ค. DI๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋Š” ์ง์ ‘ ์ž์‹ ์ด ํ•„์š”๋กœ ํ•˜๋Š” ์˜์กด์„ฑ์„ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ฐธ์กฐํ•˜๋Š”๊ฒƒ์ด ์•„๋‹Œ ์™ธ๋ถ€์—์„œ ์ฃผ์ž…๋œ ์˜์กด์„ฑ์„ ์‚ฌ์šฉํ•จ. ์ด๋ฅผํ†ตํ•ด ๊ฒฐํ•ฉ๋„๋ฅผ ๋‚ฎ์ถ”๊ณ  ์œ ์—ฐ์„ฑ ํ–ฅ์ƒ์ด ๋ชฉํ‘œ

  • ํ•„๋ฆ„์นด๋ฉ”๋ผ์™€ ํ•„๋ฆ„์ด ํ•œ๊ตฐ๋ฐ์—์„œ ์˜์กดํ•˜๊ณ ์žˆ๋‹ค๋ฉด ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์€ ์ผํšŒ์šฉ์นด๋ฉ”๋ผ (X) - ๋ณ„๋กœ์ž„
  • ํ•„๋ฆ„์นด๋ฉ”๋ผ์— ํ•„๋ฆ„์„ ์ฃผ์ž…ํ•˜์—ฌ ์˜์กดํ•˜๊ณ ์žˆ๋‹ค๋ฉด ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์€ ํ•„๋ฆ„๊ต์ฒด์šฉ์นด๋ฉ”๋ผ (O) - ๊ตฟ

1.์ƒ์„ฑ์ž ์ฃผ์ž… (๐Ÿคž์ฃผ๋กœ์‚ฌ์šฉ )

  • ์ƒ์„ฑ์ž ํ˜ธ์ถœ์‹œ์ ์— ๋”ฑ 1๋ฒˆ๋งŒ ํ˜ธ์ถœ๋˜๋Š” ๊ฒƒ์ด ๋ณด์žฅ๋ฉ๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž ์ฃผ์ž…์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๋”ฑ 1๋ฒˆ๋งŒ ํ˜ธ์ถœ๋˜๋ฏ€๋กœ ์ดํ›„์— ํ˜ธ์ถœ๋˜๋Š” ์ผ์ด ์—†์Œ ๋”ฐ๋ผ์„œ ๋ถˆ๋ณ€ํ•˜๊ฒŒ ์„ค๊ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ํ™œ์šฉํ•  ๊ฒฝ์šฐ ๊ฐ’์˜ ๋ถˆ๋ณ€์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ final์„ ํ™œ์šฉ๊ฐ€๋Šฅ ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•ด ์˜์กด์„ฑ์„ ์„ค์ •ํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ผ์ด ์—†์œผ๋ฏ€๋กœ final์œผ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ๋ถˆ๋ณ€์„ ๋ณด์žฅ
1
2
3
4
5
@Service
@RequiredArgsConstructor   <-- Lombok์œผ๋กœ ์Šคํ”„๋ง์—์„œ DI(์˜์กด์„ฑ ์ฃผ์ž…)์˜ ๋ฐฉ๋ฒ• ์ค‘์— ์ƒ์„ฑ์ž ์ฃผ์ž…์„ ์ž๋™์œผ๋กœ ์„ค์ •ํ•ด์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜
public class CommonServiceImpl implements ICommonService {
	private final SpeakerInfoRepository speakerInfoRepository;
}

2.setter(์ˆ˜์ •์ž) ์ฃผ์ž…

  • ๋Œ€๋ถ€๋ถ„์˜ ์˜์กด๊ด€๊ณ„ ์ฃผ์ž…์€ ํ•œ๋ฒˆ ์ผ์–ด๋‚˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ์‹œ์ ๊นŒ์ง€ ์˜์กด๊ด€๊ณ„๋ฅผ ๋ณ€๊ฒฝํ•  ์ผ์ด ๊ฑฐ์˜ ์—†๋‹ค. ํ•˜์ง€๋งŒ setter๋Š” ์–ธ์ œ๋“  ๋ณ€๊ฒฝ๋˜๊ฒŒ ํ•  ์œ„ํ—˜์ด ์žˆ์Œ -์ˆ˜์ •์ž ์ฃผ์ž…์„ ์‚ฌ์šฉํ•˜๋ฉด, setXxx ๋ฉ”์„œ๋“œ๋ฅผ public์œผ๋กœ ์—ด์–ด๋‘์–ด์•ผ ํ•จ. ์ฆ‰ ์–ธ์ œ ์–ด๋””์„œ๋“  ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋ง์ž„
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public  class  ExampleCase{
    	private  ChocolateService  chocolateService;
    	private  DrinkService  drinkService;

        @Autowired
    	public void  setChocolateService(ChocolateService  chocolateService){
    		this.chocolateService = chocolateService;
    	}

    	@Autowired
    	public void  setDrinkService(DrinkService  drinkService){
    		this.drinkService = drinkService;
    	}

    }

3.ํ•„๋“œ ์ฃผ์ž…

  • ์ฝ”๋“œ๊ฐ€ ๊ฐ„๊ฒฐํ•˜์ง€๋งŒ, ์™ธ๋ถ€์—์„œ ๋ณ€๊ฒฝํ•˜๊ธฐ ํž˜๋“ค๋‹ค. ํ”„๋ ˆ์ž„์›Œํฌ์— ์˜์กด์ ์ด๊ณ  ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์ข‹์ง€ ์•Š๋‹ค.
1
2
3
4
5
6
7
8
9
10
@Component
public class OrderServiceImpl implements OrderService {

    @Autowired
    private final MemberRepository memberRepository;

    @Autowired
    private final DiscountPolicy discountPolicy;

}

4.method ์ฃผ์ž…

1
2
3
4
5
6
7
8
9
10
11
@Component
public class OrderServiceImpl implements OrderService {

    private MemberRepository memberRepository;
    private DiscountPolicy discountPolicy;

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

๐Ÿ˜IOC(Inversion of Control)

์ œ์–ด์˜ ์—ญ์ „

  • ๋ณดํ†ต ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐœ๋ฐœ์ž๊ฐ€ ์›ํ•˜๋Š”๋Œ€๋กœ ์ง์ ‘ํ˜ธ์ถœํ•˜๊ณ  ์ œ์–ดํ•˜๋Š” ์Šคํƒ€์ผ๋กœ ๊ฐœ๋ฐœ์„ ํ•˜๋Š”๋ฐ ์ œ์–ด์˜ ์—ญ์ „์˜ ๊ฐœ๋…์€ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ™์€๊ฒŒ ๋‚ด์ฝ”๋“œ๋ฅผ ๋Œ€์‹ ํ˜ธ์ถœํ•ด์ฃผ๋Š”๊ฒƒ์ž„

  • ex) ์Šคํ”„๋งํ”„๋ ˆ์ž„์›Œํฌ ์—์„œ ๋นˆ์„ ๋“ฑ๋กํ•˜๋ฉด IOC ์ปจํ…Œ์ด๋„ˆ์— ์ €์žฅํ•˜๊ณ  ๋Œ€์‹ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ ๊ฐœ๋ฐœ์ž๊ฐ€ ํ•˜๋Š”๊ฒŒ ์•„๋‹Œ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ๋Œ€์‹ ํ•ด์ฃผ๋ฏ€๋กœ ์ œ์–ด์˜ ์—ญ์ „์ด ์ผ์–ด๋‚ฌ๋‹ค๊ณ  ๋ณผ์ˆ˜์žˆ์Œ.


์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜์ง€์•Š์€ ์ผ๋ฐ˜ ์ž๋ฐ”์ฝ”๋“œ์—์„œ์˜ ๊ฐ์ฒด ์ƒ์„ฑ๊ณผ ์˜์กด์„ฑ ์ฃผ์ž… ์˜ˆ์‹œ

1
2
3
4
5
6
public class OrderService {
    private OrderServiceImPl orderServiceImpl;
    public OrderService() {
        this.orderServiceImpl = new OrderServiceImPl();
    }
 }

-์œ„ ์ฝ”๋“œ์—์„œ OrderService ํด๋ž˜์Šค๋Š” OrderServiceImPl ํด๋ž˜์Šค์— ์˜์กดํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  OrderService์˜ ์ƒ์„ฑ์ž์—์„œ ์ง์ ‘ OrderServiceImPl ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ, OrderService๋Š” OrderServiceImPl ๊ฐ•ํ•˜๊ฒŒ ๊ฒฐํ•ฉ๋˜์–ด ์žˆ์–ด ์œ ์—ฐ์„ฑ์ด ๋–จ์–ด์ง€๊ฒŒ๋œ๋‹ค.

์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•˜์—ฌ ์ œ์–ด์˜ ์—ญ์ „์ด ์ผ์–ด๋‚œ ์˜ˆ์‹œ

1
2
3
4
5
6
7
public class OrderService {
    private final OrderDAO orderDAO;
    public OrderService(OrderDAO orderDAO) {
        this.orderDAO = orderDAO;
    }
}
  • ์œ„ ์ฝ”๋“œ์—์„œ๋Š” OrderServiceํด๋ž˜์Šค๊ฐ€ OrderDAO๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ์‚ฌ์šฉํ•˜๊ณ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์˜์กด์„ฑ์ด ์•ฝํ•˜๋ฉฐ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋‚ฎ์•„ ์œ ์—ฐํ•ด์ง„๋‹ค.

๐Ÿ˜Bean

-Spring IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด

์Šคํ”„๋ง Bean์€ ์™œ์‚ฌ์šฉํ•˜๋Š”๊ฐ€โ“

  • ๊ฐ€์žฅ ํฐ ์ด์œ ๋Š” ์Šคํ”„๋ง ๊ฐ„ ๊ฐ์ฒด๊ฐ€ ์˜์กด๊ด€๊ณ„๋ฅผ ๊ด€๋ฆฌํ•˜๋„๋ก ํ•˜๋Š” ๊ฒƒ์— ๊ฐ€์žฅ ํฐ ๋ชฉ์ ์ด ์žˆ๋‹ค. ๊ฐ์ฒด๊ฐ€ ์˜์กด๊ด€๊ณ„๋ฅผ ๋“ฑ๋กํ•  ๋•Œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์—์„œ ํ•ด๋‹นํ•˜๋Š” ๋นˆ์„ ์ฐพ๊ณ , ๊ทธ ๋นˆ๊ณผ ์˜์กด์„ฑ์„ ๋งŒ๋“ ๋‹ค.

Bean์„ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•

  • xml์— ์ง์ ‘ ๋“ฑ๋กํ•˜๊ธฐ
  • @Bean ์–ด๋…ธํ…Œ์ด์…˜ ์ด์šฉํ•˜๊ธฐ
  • @Component, @Controller, @Service, @Repository ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•˜๊ธฐ

1.xml์— ์ง์ ‘ ๋“ฑ๋กํ•˜๊ธฐ

1
2
<bean id="helloService" class="com.example.myapp.di.HelloService"/>
<bean id="helloController" class="com.example.myapp.di.HelloController" p:helloService-ref="helloService"</bean>

2.@Bean ์–ด๋…ธํ…Œ์ด์…˜ ์‚ฌ์šฉํ•˜๊ธฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Configurable
@ComponentScan(basePackages= {"com.example.myapp"})
@ImportResource(value= {"classpath:application-config.xml"})
public class AppConfig {
		@Bean
		public IHelloService helloService() {
			return new HelloService();
		}

		@Bean
		public HelloController helloController() {
			HelloController controller = new HelloController();
			controller.setHelloService(helloService());
			return controller;
		}
}

3. @Component, @Controller, @Service, @Repository ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
public class EmpController {
	private IEmpService empService;

	@Autowired
	public EmpController(IEmpService empService) {
		this.empService = empService;
	}

	void printInfo() {
		int count = empService.getEmpCount(50);
		System.out.println("์‚ฌ์›์˜ ์ˆ˜ : " + count);
	}
}
์ด ๊ธฐ์‚ฌ๋Š” ์ €์ž‘๊ถŒ์ž์˜ CC BY 4.0 ๋ผ์ด์„ผ์Šค๋ฅผ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.