μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
- νν΄ λ°±μλ
- νν΄+
- νν΄ νλ¬μ€ νκΈ°
- νν΄νλ¬μ€λ°±μλ
- λΉ μ€μ½ν
- νν νκΈ°
- λμμΈ ν¨ν΄
- κΈ°λ₯ ν μ€νΈ
- νν
- νν΄νλ¬μ€ νκ³
- μ±κΈν€ λΉ
- μμ°¨μ§ν₯
- push model
- νν΄νλ¬μ€
- νν λ°±μλ
- νν΄νλ¬μ€ λ°±μλ
- νν΄
- νν΄νλ¬μ€νκΈ°
- API Aggregation
- λ°μ΄ν° 쿼리
- TDD
- νν΄ νκΈ°
- fanout on write
- OOP
- νλ‘ν νμ λΉ
- νν΄νλ¬μ€ νκΈ°
- fanout on read
- pull model
- μμ± ν¨ν΄
- νΌλ ꡬν
- Today
- Total
deVlog
[Spring] Bean Scope(λΉ μ€μ½ν) μ’ λ₯ λ³Έλ¬Έ
λͺ©μ°¨
βΊοΈ Bean Scope λ?
λλΆλΆμ μ€νλ§ λΉμ΄ μ€νλ§ μ»¨ν μ΄λμ μμκ³Ό ν¨κ» μμ±λμ΄μ μ€νλ§ μ»¨ν μ΄λκ° μ’ λ£λ λκΉμ§ μ μ§λλ€κ³ μκ³ μμ κ²μ΄λ€.
μ΄κ²μ μ€νλ§ λΉμ΄ κΈ°λ³Έμ μΌλ‘ μ±κΈν€ μ€μ½νλ‘ μμ±λκΈ° λλ¬Έμ΄λ€.
κ·Έ μ΄μ λ νλλ§ λ§λ€μ΄λκ³ κ³΅μ ν΄μ μ¬μ©νκΈ° μν¨μΈλ°, μ΄ λλ¬Έμ Beanμ μνλ₯Ό μ μ₯νλ μ½λλ₯Ό μμ±νλ κ²μ λμμ± λ¬Έμ λ₯Ό μ λ°ν μ μλ€. λν μꡬμ¬νκ³Ό ꡬν κΈ°λ₯ λ±μ νμμ λ°λΌμ λΉμ±κΈν€μ΄ νμν κ²½μ°λ μλ€. μ΄λ₯Ό λͺ μμ μΌλ‘ ꡬλΆνκΈ° μν΄μ scope λΌλ ν€μλλ₯Ό μ 곡νλ€.
Spring μλ λ€μκ³Ό κ°μ Bean Scope λ€μ΄ μ‘΄μ¬νλ€.
π Bean Scope μ μ’ λ₯
μ±κΈν€(Singleton)
- Spring μμ κΈ°λ³Έ
- μ€νλ§ μ»¨ν μ΄λμ μμκ³Ό μ’ λ£κΉμ§ μ μ§λλ κ°μ₯ λμ λ²μμ μ€μ½ν
νλ‘ν νμ (Prototype)
- μ€νλ§ μ»¨ν μ΄λλ νλ‘ν νμ λΉμ μμ±κ³Ό μμ‘΄κ΄κ³ μ£Όμ κΉμ§λ§ κ΄μ¬νκ³ λλ κ΄λ¦¬νμ§ μλ λ§€μ° μ§§μ λ²μμ μ€μ½ν
- μμ²μ΄ μ€λ©΄ νμ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό μμ±νμ¬ λ°ννκ³ μ΄νμ κ΄λ¦¬νμ§ μλλ€.
- μ΄κΈ°ν λ©μλκΉμ§λ λΆλ₯΄μ§λ§ μ’ λ£ λ©μμ§ νΈμΆμ΄ λμ§ μλλ€.
- νλ‘ν νμ μ λ°μ ν΄λΌμ΄μΈνΈκ° κ°μ²΄λ₯Ό κ΄λ¦¬ν΄μΌ νλ€.
μΉ κ΄λ ¨ μ€μ½ν
- request
- μΉ μμ²μ΄ λ€μ΄μ€κ³ λκ° λκΉμ§ μ μ§λλ μ€μ½ν
- session
- μΉ μΈμ μ΄ μμ±λκ³ μ’ λ£λ λκΉμ§ μ μ§λλ μ€μ½ν
- application
- μΉμ μλΈλ¦Ώ 컨ν μ€νΈμ κ°μ λ²μλ‘ μ μ§λλ μ€μ½ν
π² Bean Scope μ§μ μμ
λΉ μ€μ½νλ λ€μκ³Ό κ°μ΄ μ§μ ν μ μλ€.
1. μ»΄ν¬λνΈ μ€μΊ μλ λ±λ‘
@Scope("prototype")
@Component
public class HelloBean {}
2. μλ λ±λ‘
@Scope("prototype")
@Bean
ProtoTypeBean HelloBean() {
return new HelloBean();
}
βΌοΈ νλ‘ν νμ
μ€μ½ν?
μ±κΈν€ μ€μ½νμ λΉμ μ‘°ννλ©΄ μ€νλ§ μ»¨ν μ΄λλ νμ κ°μ μΈμ€ν΄μ€μ μ€νλ§ λΉμ λ°ννλ€.
λ°λ©΄μ νλ‘ν νμ μ€μ½νλ₯Ό μ€νλ§ μ»¨ν μ΄λμμ μ‘°ννλ©΄ μ€νλ§ μ»¨ν μ΄λλ νμ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό μμ±ν΄μ λ°ννλ€.
1. μ±κΈν€ μ€μ½νμ λΉμ μ€νλ§ μ»¨ν μ΄λμ μμ²νλ€.
2. μ€νλ§ μ»¨ν μ΄λλ λ³ΈμΈμ΄ κ΄λ¦¬νλ μ€νλ§ λΉμ λ°ννλ€.
3. μ΄νμ μ€νλ§ μ»¨ν μ΄λμ κ°μ μμ²μ΄ μλ κ°μ κ°μ²΄ μΈμ€ν΄μ€μ μ€νλ§ λΉμ λ°ννλ€.
1. νλ‘ν νμ μ€μ½νμ λΉμ μ€νλ§ μ»¨ν μ΄λμ μμ²νλ€.
2. μ€νλ§ μ»¨ν μ΄λλ μμ² μμ μ νλ‘ν νμ λΉμ μμ±νκ³ , νμν μμ‘΄κ΄κ³λ₯Ό μ£Όμ νλ€.
3. μ€νλ§ μ»¨ν μ΄λλ μμ±ν νλ‘ν νμ λΉμ ν΄λΌμ΄μΈνΈμ λ°ννλ€.
4. μ΄νμ μ€νλ§ μ»¨ν μ΄λμ κ°μ μμ²μ΄ μ€λ©΄ νμ μλ‘μ΄ νλ‘ν νμ λΉμ μμ±ν΄μ λ°ννλ€.
μ¬κΈ°μ ν΅μ¬μ μ€νλ§ μ»¨ν μ΄λλ νλ‘ν νμ λΉμ μμ±νκ³ , μμ‘΄κ΄κ³ μ£Όμ , μ΄κΈ°νκΉμ§λ§ μ²λ¦¬νλ€λ κ²μ΄λ€. ν΄λΌμ΄μΈνΈμ λΉμ λ°ννκ³ , μ΄ν μ€νλ§ μ»¨ν μ΄λλ μμ±λ νλ‘ν νμ λΉμ κ΄λ¦¬νμ§ μλλ€. νλ‘ν νμ λΉμ κ΄λ¦¬ν μ± μμ νλ‘ν νμ λΉμ λ°μ ν΄λΌμ΄μΈνΈμ μλ€. κ·Έλμ @PreDestroy μ κ°μ μ’ λ£ λ©μλκ° νΈμΆλμ§ μλλ€.
π§ͺ λΉ μ€μ½ν ν μ€νΈ
π« μ±κΈν€ μ€μ½ν λΉ
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import static org.assertj.core.api.Assertions.assertThat;
class BeanTest {
@Test
void singletonBeanFind() {
AnnotationConfigApplicationContext ac = new
AnnotationConfigApplicationContext(SingletonBean.class);
SingletonBean singletonBean1 = ac.getBean(SingletonBean.class);
SingletonBean singletonBean2 = ac.getBean(SingletonBean.class);
System.out.println("singletonBean1 = " + singletonBean1);
System.out.println("singletonBean2 = " + singletonBean2);
assertThat(singletonBean1).isSameAs(singletonBean2);
ac.close(); //μ’
λ£
}
@Scope("singleton")
static class SingletonBean {
@PostConstruct
public void init() {
System.out.println("SingletonBean.init");
}
@PreDestroy
public void destroy() {
System.out.println("SingletonBean.destroy");
}
}
}
μ€ν κ²°κ³Ό
SingletonBean.init
singletonBean1 = ν¨ν€μ§λͺ .BeanTest$SingletonBean@134d26af
singletonBean2 = ν¨ν€μ§λͺ .BeanTest$SingletonBean@134d26af
SingletonBean.destroy
- @PostConstruct(λΉ μ΄κΈ°ν) λ©μλ μ€ν
- λμΌν μΈμ€ν΄μ€ λΉ μ‘°ν
- @PreDestory(μ’ λ£) λ©μλ μ€ν
π« νλ‘ν νμ μ€μ½ν λΉ
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Scope;
import static org.assertj.core.api.Assertions.assertThat;
class PrototypeTest {
@Test
void prototypeBeanFind() {
AnnotationConfigApplicationContext ac = new
AnnotationConfigApplicationContext(PrototypeBean.class);
PrototypeBean prototypeBean1 = ac.getBean(PrototypeBean.class);
PrototypeBean prototypeBean2 = ac.getBean(PrototypeBean.class);
System.out.println("prototypeBean1 = " + prototypeBean1);
System.out.println("prototypeBean2 = " + prototypeBean2);
assertThat(prototypeBean1).isNotSameAs(prototypeBean2);
ac.close(); //μ’
λ£
}
@Scope("prototype")
static class PrototypeBean {
@PostConstruct
public void init() {
System.out.println("PrototypeBean.init");
}
@PreDestroy
public void destroy() {
System.out.println("PrototypeBean.destroy");
}
}
}
μ€ν κ²°κ³Ό
PrototypeBean.init
PrototypeBean.init
prototypeBean1 = ν¨ν€μ§.PrototypeTest$PrototypeBean@663411de
prototypeBean2 = ν¨ν€μ§.PrototypeTest$PrototypeBean@63dd899
- μ±κΈν€ λΉμ μ€νλ§ μ»¨ν μ΄λ μμ± μμ μ μ΄κΈ°ν λ©μλκ° μ€ν λμ§λ§, νλ‘ν νμ μ€μ½νμ λΉμ μ€νλ§ μ»¨ν μ΄λμμ λΉμ μ‘°νν λ μμ±λκ³ , μ΄κΈ°ν λ©μλλ μ€νλλ€.
- νλ‘ν νμ λΉμ 2λ² μ‘°ννμΌλ―λ‘ μμ ν λ€λ₯Έ μ€νλ§ λΉμ΄ μμ±λκ³ , μ΄κΈ°νλ 2λ² μ€νλ κ²μ νμΈν μ μλ€.
- μ±κΈν€ λΉμ μ€νλ§ μ»¨ν μ΄λκ° κ΄λ¦¬νκΈ° λλ¬Έμ μ€νλ§ μ»¨ν μ΄λκ° μ’ λ£λ λ λΉμ μ’ λ£ λ©μλκ° μ€νλμ§λ§, νλ‘ν νμ λΉμ μ€νλ§ μ»¨ν μ΄λκ° μμ±κ³Ό μμ‘΄κ΄κ³ μ£Όμ κ·Έλ¦¬κ³ μ΄κΈ°ν κΉμ§λ§ κ΄μ¬νκ³ , λλ κ΄λ¦¬νμ§ μλλ€. λ°λΌμ νλ‘ν νμ λΉμ μ€νλ§ μ»¨ν μ΄λκ° μ’ λ£λ λ @PreDestroy μ κ°μ μ’ λ£ λ©μλκ° μ€νλμ§ μλλ€.
νλ‘ν νμ λΉμ νΉμ§
- μ€νλ§ μ»¨ν μ΄λμ μμ²ν λλ§λ€ μλ‘ μμ±λλ€.
- μ€νλ§ μ»¨ν μ΄λλ νλ‘ν νμ λΉμ μμ±κ³Ό μμ‘΄κ΄κ³ μ£Όμ κ·Έλ¦¬κ³ μ΄κΈ°νκΉμ§λ§ κ΄μ¬νλ€.
- μ’ λ£ λ©μλκ° νΈμΆλμ§ μλλ€.
- νλ‘ν νμ λΉμ νλ‘ν νμ λΉμ μ‘°νν ν΄λΌμ΄μΈνΈκ° κ΄λ¦¬ν΄μΌ νλ€. μ’ λ£ λ©μλμ λν νΈμΆλ ν΄λΌμ΄μΈνΈκ° μ§μ ν΄μΌνλ€.
π€ νλ‘ν νμ λΉμ μΈμ μ¬μ©ν΄μΌ ν κΉ?
1. κ°μ²΄λ₯Ό λ§€λ² μλ‘ μμ±ν΄μΌ ν λ
- μλ‘ λ 립μ μΈ μνλ₯Ό κ°μ§ κ°μ²΄κ° νμν λ μ μ©ν©λλ€.
- e.g. μμ²λ§λ€ λ€λ₯Έ μνλ₯Ό μ μ§ν΄μΌ νλ λΉμ¦λμ€ λ‘μ§ ν΄λμ€λ μμ λ°μ΄ν° μ μ₯μ
2. μνλ₯Ό κ°μ§ Beanμ λ€λ£° λ
- singleton μ€μ½νμ λ¬λ¦¬, prototype Beanμ μνλ₯Ό 곡μ νμ§ μλλ€.
- μνκ° λ 립μ μ΄μ΄μΌ νκ±°λ νΉμ λ‘μ§μμ κ°μ²΄ μνκ° λ³νλ©΄ μ λλ κ²½μ°μ μ ν©νλ€.
- e.g. μ¬λ¬ μ°λ λκ° λμμ μ κ·Όνμ¬ λ°μ΄ν° μΆ©λμ΄ λ°μν μ μλ κ²½μ°.
3. λ¨κΈ°κ° μ¬μ© ν μλ©Ένλ κ°μ²΄
- prototype μ€μ½νλ Spring 컨ν μ΄λκ° μμ±ν μ΄νμ Beanμ λΌμ΄νμ¬μ΄ν΄μ κ΄λ¦¬νμ§ μλλ€.
- λ°λΌμ λ¨κΈ°κ° μ¬μ©λκ³ , μ ν리μΌμ΄μ μ 체μμ κ³μ μ μ§λ νμκ° μλ κ²½μ° μ ν©νλ€.
- e.g. μΌνμ± μμ μ μννλ μλΉμ€λ μ νΈλ¦¬ν° ν΄λμ€
4. DI (Dependency Injection)μμ νμ©
- νΉμ singleton Beanμ΄ prototype Beanμ μ£Όμ λ°μ λ§€λ² μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό μ¬μ©ν΄μΌ ν λ μ μ©νλ€.
- Springμ @Scope("prototype")κ³Ό ν¨κ» ObjectProviderλ @Lookupμ μ¬μ©ν΄ μλ‘μ΄ μΈμ€ν΄μ€λ₯Ό μμ²ν μ μλ€.
π μ°Έκ³
'π Framework > Spring' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Spring] μ€νλ§ λΉ μ€μ½ν μλ²½ κ°μ΄λ (0) | 2025.04.04 |
---|