[Spring] Bean Scope

giggleยท2023๋…„ 9์›” 6์ผ
0
post-custom-banner

๐Ÿ“Œ Spring Bean์ด๋ž€?

Bean

A bean is an object that is instantiated, assembled, and managed by a Spring IoC container.

๊ณต์‹๋ฌธ์„œ์—์„œ Spring Bean์ด๋ž€ Spring IoC container์— ์˜ํ•ด ๊ด€๋ฆฌ๋˜๋Š” ๊ฐ์ฒด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

  • Spring Bean์€ Spring IoC ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด๋กœ์„œ ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ๊ด€๋ฆฌ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
  • IoC ์ปจํ…Œ์ด๋„ˆ ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ๊ฐ์ฒด๋กœ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค IoC ์ปจํ…Œ์ด๋„ˆ์—์„œ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์–ด๋…ธํ…Œ์ด์…˜์ธ @Bean์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ xml ์„ค์ •์„ ํ†ตํ•ด ์ผ๋ฐ˜ ๊ฐ์ฒด๋ฅผ Bean์œผ๋กœ ๋“ฑ๋ก์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
  • ์ฆ‰, Spring ์—์„œ๋Š” Bean์€ ApplicationContext๊ฐ€ ์•Œ๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์ด๋ฉฐ ApplicationContext๊ฐ€ ์ƒ์„ฑํ•˜๊ณ  ์ง์ ‘ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

XML ๊ธฐ๋ฐ˜์˜ Bean ์ •์˜ ๋ฐฉ๋ฒ•๋“ค

<!-- ๊ฐ„๋‹จํ•œ ๋นˆ ์ •์˜ -->
<bean id="..." class="..."></bean>

<!-- scope์™€ ํ•จ๊ป˜ ๋นˆ ์ •์˜ -->
<bean id="..." class="..." scope="singleton"></bean>

<!-- property์™€ ํ•จ๊ป˜ ๋นˆ ์ •์˜ -->
<bean id="..." class="...">
	<property name="beaninit" value="Hello World!"/>
</bean>

<!-- ์ดˆ๊ธฐ์™€ ๋ฉ”์†Œ๋“œ์™€ ํ•จ๊ป˜ ๋นˆ ์ •์˜ -->
<bean id="..." class="..." init-method="..."></bean>

Spring Bean์˜ ์ƒ๋ช… ์ฃผ๊ธฐ

  • ๊ฐ์ฒด ์ƒ์„ฑ -> ์˜์กด ์„ค์ • -> ์ดˆ๊ธฐํ™” -> ์‚ฌ์šฉ -> ์†Œ๋ฉธ ๊ณผ์ •์˜ ์ƒ๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
  • Bean์€ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
  • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ดˆ๊ธฐํ™”๋  ๋•Œ ๋จผ์ € ๋นˆ ๊ฐ์ฒด๋ฅผ ์„ค์ •์— ๋งž์ถฐ ์ƒ์„ฑํ•˜๋ฉฐ ์˜์กด ๊ด€๊ณ„๋ฅผ ์„ค์ •ํ•œ ๋’ค ํ•ด๋‹น ํ”„๋กœ์„ธ์Šค๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ๋นˆ ๊ฐ์ฒด๊ฐ€ ์ง€์ •ํ•œ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ดˆ๊ธฐํ™”๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์ข…๋ฃŒ๋  ๋•Œ ๋นˆ์ด ์ง€์ •ํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ์†Œ๋ฉธ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์นฉ๋‹ˆ๋‹ค.
  • ์Šคํ”„๋ง์€ InitializingBean ์ธํ„ฐํŽ˜์ด์Šค์™€ DisposableBean์„ ์ œ๊ณตํ•˜๋ฉฐ ๋นˆ ๊ฐ์ฒด์˜ ํด๋ž˜์Šค๊ฐ€ InitializingBean Interface ๋˜๋Š” DisposableBean์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ํ•ด๋‹น ์ธํ„ฐํŽ˜์ด์Šค์—์„œ ์ •์˜๋œ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋นˆ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™” ๋˜๋Š” ์ข…๋ฃŒ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  • ๋˜ํ•œ, ์–ด๋…ธํ…Œ์ด์…˜์„ ์ด์šฉํ•œ ๋นˆ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•์—๋Š” @PostConstruct์™€ ๋นˆ ์†Œ๋ฉธ์—์„œ๋Š” @PreDestory๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
// ์ƒ์„ฑ ๊ด€๋ จํ•œ ์ธํ„ฐํŽ˜์ด์Šค
public interface InitializingBean { 
	void afterPropertiesSet() throws Exception;
}

// ์†Œ๋ฉธ ๊ด€๋ จํ•œ ์ธํ„ฐํŽ˜์ด์Šค
public interface DispoasableBean { 
	void destroy() throws Exception;
}

public class Test implements InitializingBean, DisposableBean {

    @Override
    public void afterPropertiesSet() throws Exception {
    	//๋นˆ ์ƒ์„ฑํ›„ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
        System.out.println("afterPropertiesSet() ์‹คํ–‰");
    }
    
    @Override
    public void destroy() throws Exception {
    	// ์†Œ๋ฉธ์„ ์ง„ํ–‰ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
        System.out.println("destroy() ์‹คํ–‰");
    }   
}

๐Ÿ“Œ Spring Bean Scope๋ž€?

Bean Scope

์Šค์ฝ”ํ”„(Scope) ์ข…๋ฅ˜

singleton : ํ•ด๋‹น Bean์— ๋Œ€ํ•ด IoC ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

prototype : ํ•ด๋‹น Bean์— ๋Œ€ํ•ด ๋‹ค์ˆ˜์˜ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. prototype bean์€ ๋ชจ๋“  ์š”์ฒญ์—์„œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์ด๋ฉฐ, ์˜์กด์„ฑ ๊ด€๊ณ„์˜ bean์ด ์ฃผ์ž…๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์–ด ์ฃผ์ž…๋œ๋‹ค. GC์— ์˜ํ•ด bean์ด ์ž๋™์œผ๋กœ ์ œ๊ฑฐ๋œ๋‹ค.

request : ํ•ด๋‹น Bean์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ HTTP Request์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

session : ํ•ด๋‹น Bean์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ HTTP Session์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

global session : ํ•ด๋‹น Bean์— ๋Œ€ํ•ด ํ•˜๋‚˜์˜ Global HTTP Session์˜ ๋ผ์ดํ”„์‚ฌ์ดํด์—์„œ ๋‹จ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋กœ๋งŒ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

request, session, global session์€ MVC ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์‹ฑ๊ธ€ํ†ค ๋ฐ ํ”„๋กœํ† ํƒ€์ž… ์Šค์ฝ”ํ”„ ๋“ฑ๋ก ๋ฐฉ๋ฒ•

@Component
public class TestBean{
	...
}

์‹ฑ๊ธ€ํ†ค ์Šค์ฝ”ํ”„์˜ ๊ฒฝ์šฐ ์œ„์˜ ์˜ˆ์‹œ ์ฝ”๋“œ์™€ ๊ฐ™์ด @Component ๋กœ ๋“ฑ๋ก์„ ํ•ด๋†“์œผ๋ฉด ๋””ํดํŠธ ๊ฐ’ ์ž๋™์œผ๋กœ ๋“ฑ๋ก๋ฉ๋‹ˆ๋‹ค.

prototype ์Šค์ฝ”ํ”„ ์ž๋™ ๋“ฑ๋ก

@Scope("prototype")
@Component
public class TestBean{
	...
}

์ž๋™ ๋“ฑ๋ก์˜ ๊ฒฝ์šฐ @Scope๋กœ ํ•ด๋†“๊ณ  prototype์„ ๋ช…์‹œํ•ด๋†“์œผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

prototype ์Šค์ฝ”ํ”„ ์ˆ˜๋™ ๋“ฑ๋ก

@Scope("prototype")
@Bean
public class TestBean{
	
    PrototypeBean TestBean(){
    	return new TestBean();
    }
}

์ˆ˜๋™ ๋“ฑ๋ก์˜ ๊ฒฝ์šฐ์—๋Š” @Bean, @Scope๋ฅผ ๋ช…์‹œํ•œ ๋‹ค์Œ ์ƒ์„ฑ์ž๋กœ PrototypeBean์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.


์ฐธ๊ณ 


ํ”ผ๋“œ๋ฐฑ ๋ฐ ๊ฐœ์„ ์ ์€ ๋Œ“๊ธ€์„ ํ†ตํ•ด ์•Œ๋ ค์ฃผ์„ธ์š”๐Ÿ˜Š

profile
๋ฐฐ์›€์„ ๊ธ€๋กœ ๊ธฐ๋กํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€