Container Context

Spring IoC ์ปจํ…Œ์ด๋„ˆ ์†Œ๊ฐœ

Spring IoC(Inversion of Control) ์ปจํ…Œ์ด๋„ˆ๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐ์ฒด(bean)๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ApplicationContext ์ธํ„ฐํŽ˜์ด์Šค๋Š” Spring์˜ IoC ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, bean์˜ ์ธ์Šคํ„ด์Šคํ™”, ์„ค์ •, ์กฐ๋ฆฝ์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

์ปจํ…Œ์ด๋„ˆ๋Š” ๊ตฌ์„ฑ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด ์–ด๋–ค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์„ค์ •ํ•˜๋ฉฐ, ์–ด๋–ป๊ฒŒ ์กฐ๋ฆฝํ• ์ง€์— ๋Œ€ํ•œ ์ง€์‹œ๋ฅผ ๋ฐ›์Šต๋‹ˆ๋‹ค. @Component, @Configuration ์• ๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ์„ค์ • ํด๋ž˜์Šค, XML ํŒŒ์ผ, ๋˜๋Š” Groovy ์Šคํฌ๋ฆฝํŠธ ๋“ฑ์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์š” ApplicationContext ๊ตฌํ˜„์ฒด

์Šคํ”„๋ง์˜ ApplicationContext ์ธํ„ฐํŽ˜์ด์Šค์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ตฌํ˜„์ฒด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ ์ค‘์—์„œ๋„ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ AnnotationConfigApplicationContext์™€ ClassPathXmlApplicationContext์ž…๋‹ˆ๋‹ค.

  • AnnotationConfigApplicationContext: ์ž๋ฐ” ๊ธฐ๋ฐ˜ ์„ค์ •์„ ์‚ฌ์šฉํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉ.

  • ClassPathXmlApplicationContext: XML ํŒŒ์ผ ๊ธฐ๋ฐ˜ ์„ค์ •์„ ์‚ฌ์šฉํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์„ค์ •ํ•  ๋•Œ ์‚ฌ์šฉ.

Spring Boot์—์„œ ApplicationContext ์ƒ์„ฑ

Spring Boot์—์„œ๋Š” ๋ณ„๋„์˜ ์„ค์ • ์—†์ด ๊ธฐ๋ณธ์ ์ธ ์„ค์ • ๊ทœ์น™์— ๋”ฐ๋ผ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค. ๋ณ„๋„์˜ ์‚ฌ์šฉ์ž ์ฝ”๋“œ๋กœ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ƒ์„ฑํ•  ํ•„์š”๊ฐ€ ์—†์œผ๋ฉฐ, Spring Boot์˜ ์ž๋™ ์„ค์ •์ด ์ด๋ฅผ ์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค.

๊ตฌ์„ฑ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ

๊ตฌ์„ฑ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋Š” Spring IoC ์ปจํ…Œ์ด๋„ˆ์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๊ณ  ์„ค์ •ํ•˜๋ฉฐ ์กฐ๋ฆฝํ• ์ง€๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ตฌ์„ฑ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋Š” ์ž๋ฐ” ๊ธฐ๋ฐ˜, ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜, ๋˜๋Š” XML ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ํ•„์š”ํ•œ bean์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

์ž๋ฐ” ๊ธฐ๋ฐ˜ ์„ค์ •

์ž๋ฐ” ๊ธฐ๋ฐ˜ ์„ค์ •์—์„œ๋Š” @Configuration ํด๋ž˜์Šค์™€ @Bean ์• ๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์—ฌ bean์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ์„ค์ •์€ ์™ธ๋ถ€์˜ ์ž๋ฐ” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ฐ์ฒด๋ฅผ ์ •์˜ํ•˜๊ณ  ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

XML ๊ธฐ๋ฐ˜ ์„ค์ •

XML ๊ธฐ๋ฐ˜ ์„ค์ •์—์„œ๋Š” <bean> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ bean์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. XML ํŒŒ์ผ์— ๊ฐ bean์˜ id์™€ ํด๋ž˜์Šค ์ •๋ณด๋ฅผ ์ •์˜ํ•˜๊ณ , ์˜์กด ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

<beans xmlns="http://www.springframework.org/schema/beans">
    <bean id="myService" class="com.example.MyServiceImpl">
        <property name="dependency" ref="someDependency"/>
    </bean>

    <bean id="someDependency" class="com.example.SomeDependency"/>
</beans>

Groovy ๊ธฐ๋ฐ˜ ์„ค์ •

Groovy DSL์„ ์‚ฌ์šฉํ•ด๋„ bean์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฐฉ์‹์€ Grails ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ํ”ํžˆ ์‚ฌ์šฉ๋˜๋ฉฐ, XML๊ณผ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์™ธ๋ถ€ ์„ค์ •์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

beans {
    myService(MyServiceImpl) {
        dependency = someDependency
    }
    someDependency(SomeDependency)
}

ApplicationContext๋ฅผ ์‚ฌ์šฉํ•œ Bean ์ ‘๊ทผ

ApplicationContext๋Š” ๋‹ค์–‘ํ•œ bean์„ ๋“ฑ๋กํ•˜๊ณ , ํ•„์š”์— ๋”ฐ๋ผ ์ด๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, XML ๊ธฐ๋ฐ˜ ์„ค์ •์—์„œ ClassPathXmlApplicationContext๋ฅผ ์‚ฌ์šฉํ•ด ์„ค์ • ํŒŒ์ผ์„ ๋กœ๋“œํ•˜๊ณ , ๋“ฑ๋ก๋œ bean์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
PetStoreService service = context.getBean("petStore", PetStoreService.class);
List<String> userList = service.getUsernameList();

์ด ๋ฐฉ์‹์œผ๋กœ Spring IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” bean์„ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Groovy ๊ธฐ๋ฐ˜ ์„ค์ •์—์„œ๋„ ์œ ์‚ฌํ•œ ๋ฐฉ์‹์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, GenericApplicationContext๋ฅผ ํ†ตํ•ด ์—ฌ๋Ÿฌ ํ˜•์‹์˜ ์„ค์ • ํŒŒ์ผ์„ ์กฐํ•ฉํ•ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Last updated