Bean Scope - ProtoType Scope
Bean Scope ๋ ๋ง๊ทธ๋๋ก ๋น์ด ์กด์ฌํ๋ ๋ฒ์๋ฅผ ๋ปํฉ๋๋ค.
Bean Life Cycle ์์ ์ดํด๋ดค๋ฏ์ด ๋น์ ์ปจํ ์ด๋์ ์์๊ณผ ํจ๊ป ์์ฑ๋์ด์ Container๊ฐ ์ข ๋ฃ๋ ๋๊น์ง ์ ์ง๋ฉ๋๋ค.
๋ํ Spring Container๋ ๋น์ ๋ฑ๋กํ ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ฒด ์ธ์คํด์ค(Bean)๋ฅผ ์ฑ๊ธํค์ผ๋ก ๋ฑ๋กํ๊ณ ์ฑ๊ธํค ์ค์ฝํ๋ก ์์ฑ๋ฉ๋๋ค. ( ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ ๋น์ ์ค์ฝํ๊ฐ ๋ช ์์ ์ผ๋ก ์ง์ ๋์ง ์์ผ๋ฉด ์ฑ๊ธํค์ด๋ค )
๋์ ํด๋์ค์ @Scope( ) ์ด๋ ธํ ์ด์ ์ ํตํด ์ค์ฝํ ์ง์
//์ปดํฌ๋ํธ ์ค์บ ์๋ ๋ฑ๋ก
@Scope("prototype") // @Scope(”singletone”)
@Component
public class HelloBean {}
//์๋๋ฑ๋ก
@Scope("prototype") // @Scope(”singletone”)
@Bean
PrototypeBean HelloBean() {
return new HelloBean();
}
Singleton Scope
• ์คํ๋ง ์ปจํ ์ด๋์ ์์๊ณผ ๋๊น์ง ํจ๊ปํ๋ ๋งค์ฐ ๊ธด ์ค์ฝํ
• ๊ธฐ๋ณธ ์ค์ฝํ, ์คํ๋ง ์ปจํ ์ด๋์ ์์๊ณผ ์ข ๋ฃ๊น์ง ์ ์ง๋๋ ๊ฐ์ฅ ๋์ ๋ฒ์์ ์ค์ฝํ
• ํญ์ ๊ฐ์ ๊ฐ์ฒด ์ธ์คํด์ค์ ์คํ๋ง ๋น์ ๋ฐํํ๊ณ ์คํ๋ง ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๊ธฐ ๋๋ฌธ์ ์์ฑ ์์ ์ ์ด๊ธฐํ ๋ฉ์๋๊ฐ ์คํ๋๋ฉฐ ์ข ๋ฃ๋ ๋๋ ๋น์ ์ข ๋ฃ๋ฉ์๋ ์คํ ๋จ
• ์ฑ๊ธํค ํ์ ์ผ๋ก ์ ํฉํ ๊ฐ์ฒด
1) ์ํ๊ฐ ์๋ ๊ณต์ ๊ฐ์ฒด
2) ์ฝ๊ธฐ ์ ์ฉ์ผ๋ก๋ง ์ํ๋ฅผ ๊ฐ์ง ๊ฐ์ฒด
3) ์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅํ ์ํ๋ฅผ ์ง๋๋ฉด์๋ ์ฌ์ฉ๋น๋๊ฐ ๋งค์ฐ ๋์ ๊ฐ์ฒด ( ๋จ, ์ด๋๋ ๋๊ธฐํ ์ ๋ต์ด ํ์ )
์คํ๋ง์ ์ฑ๊ธํค ์ค์ฝํ ์ธ์๋ ํ๋กํ ํ์ ์ค์ฝํ, ์น ๊ด๋ จ ์ค์ฝํ๋ฅผ ์ง์ํฉ๋๋ค.
์ค๋์ ํ๋กํ ํ์ ์ค์ฝํ์ ๋ํด ์์๋ด ์๋ค..
ProtoType Scope
ํ๋กํ ํ์ ์ค์ฝํ๋ ์์ฑ๊ณผ ์์กด๊ด๊ณ ์ฃผ์ , ๊ทธ๋ฆฌ๊ณ ์ด๊ธฐํ๊น์ง๋ง ์งํํ๋ ํน๋ณํ ์ค์ฝํ์ ๋๋ค.
์ฆ, Spring Container๊ฐ ํ๋กํ ํ์ ๋น์ ์์ฑ, ์์กด๊ด๊ณ ์ฃผ์ ๊น์ง๋ง ๊ด์ฌํ๋ ์งง์ ๋ฒ์์ ์ค์ฝํ์ ๋๋ค.
ProtoType Scope ๋น ์์ฒญ
• ํ๋กํ ํ์ ์ค์ฝํ๋ ์คํ๋ง ์ปจํ ์ด๋์ ์์ฒญํ ๋๋ง๋ค ํญ์ ์๋ก์ด ํ๋กํ ํ์ ์ธ์คํด์ค๋ฅผ ์์ฑํด์ ๋ฐํ
• ์คํ๋ง ์ปจํ ์ด๋๋ ํ๋กํ ํ์ ๋น์ ์์ฑํ๊ณ , ์์กด๊ด๊ณ ์ฃผ์ , ์ด๊ธฐํ ๊น์ง๋ง ์ฒ๋ฆฌ
• ๋น ์กฐํ ์ ๋น์ด ์์ฑ๋๊ณ , ์ด๊ธฐํ ๋ฉ์๋๋ ์กฐํ ์ ์งํ๋จ
• ์คํ๋ง ์ปจํ ์ด๋๊ฐ ํด๋ผ์ด์ธํธ์ ๋น์ ๋ฐํํ ์ดํ ์คํ๋ง ์ปจํ ์ด๋๋ ์์ฑ๋ ํ๋กํ ํ์ ๋น์ ๊ด๋ฆฌํ์ง ์๋๋ค.
• ๋น์ ๋ฐ์ ํด๋ผ์ด์ธํธ์ ๋น์ ๊ด๋ฆฌํ ์ฑ ์์ด ์์ด์ @PreDestory ๊ฐ์ ์ข ๋ฃ ๋ฉ์๋๊ฐ ํธ์ถ๋์ง ์๋๋ค.
• ์ข ๋ฃ ๋ฉ์๋์ ๋ํ ํธ์ถ๋ ํด๋ผ์ด์ธํธ๊ฐ ์ง์ ํด์ผํ๋ค. ( GC ์ ์ํด ๋น์ด ์ ๊ฑฐ )
• ํ๋กํ ํ์ ์ผ๋ก ์ ํฉํ ๊ฐ์ฒด
1) ์ฌ์ฉํ ๋๋ง๋ค ์ํ๊ฐ ๋ฌ๋ผ์ ธ์ผํ๋ ๊ฐ์ฒด
2) ์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅํ ์ํ๊ฐ ์๋ ๊ฐ์ฒด
ProtoType, Singleton ํจ๊ป ์ฌ์ฉ ์ ๋ฌธ์ ์
์คํ๋ง์ ์ผ๋ฐ์ ์ผ๋ก ์ฑ๊ธํค ๋น์ ์ฌ์ฉํ๋ฏ๋ก, ์ฑ๊ธํค ๋น์ด ํ๋กํ ํ์ ๋น์ ์ฌ์ฉํ๊ฒ ๋ฉ๋๋ค. ํ์ง๋ง ์ด์ ๊ฐ์ด ์ฑ๊ธํค ๊ฐ์ฒด๊ฐ ํ๋กํ ํ์ ๊ฐ์ฒด๋ฅผ ๊ฐ๊ณ ์๋ ๊ฒฝ์ฐ์๋ ์๋์ ๋ค๋ฅธ ๊ฒฐ๊ณผ๊ฐ ๋์ฌ ์ ์์ต๋๋ค.
์ฑ๊ธํค ๋น์ ์์ฑ ์์ ์ ์์กด๊ด๊ณ ์ฃผ์ ์ ๋ฐ๊ธฐ ๋๋ฌธ์ ์ด๋ฏธ ์ฑ๊ธํค ๋น์ด ์์ฑ๋๋ ์์ ์ ํ๋กํ ํ์ ๋น์ด ์์ฑ๋์ด ๋ค์ด์ต๋๋ค. ์ด๋ฐ ์ฑ๊ธํค ๋น ๋ด๋ถ์ ํ๋กํ ํ์ ๋น์ ํธ์ถํ๊ฒ ๋๋ฉด ์ฑ๊ธํค ๋น๊ณผ ํ๋กํ ํ์ ๋น์ด ํจ๊ป ์ ์ง๋๊ธฐ ๋๋ฌธ์ ์ฑ๊ธํค ๋น ๋ด๋ถ์ ํ๋กํ ํ์ ๋น์ ์ฌ์ฉํ ๋๋ง๋ค ๋งค๋ฒ ๊ฐ์ ๊ฐ์ ๊ฐ์ ธ์ค๊ฒ ๋ฉ๋๋ค.
์ฑ๊ธํค ๋น๊ณผ ํ๋กํ ํ์ ๋น์ ํจ๊ป ์ฌ์ฉํ ๋, ์ด๋ป๊ฒ ํ๋ฉด ์ฌ์ฉํ ๋๋ง๋ค ํญ์ ์๋ก์ด ํ๋กํ ํ์ ๋น(๋ค๋ฅธ ์ธ์คํด์ค)์ ์์ฑํ ์ ์์์ง์ ๋ํด ์์๋ด ์๋ค.
1 ) Provider
@Component
class ClientBean {
@Autowired
private Provider<PrototypeBean> provider; //javax.inject ํ์ ํด๋์ค๋ก importํด์ผํจ
public int logic() {
PrototypeBean prototypeBean = provider.get(); // ์ปจํ
์ด๋์ ๋น ์์ฒญ
prototypeBean.addCount();
return prototypeBean.getCount();
}
}
- logic() ๋ฉ์๋๋ฅผ ํธ์ถํ ๋๋ง๋ค ๋ค๋ฅธ PrototypeBean ์ธ์คํด์ค๊ฐ ํธ์ถ๋ฉ๋๋ค.
- Provider๋ ์๋ฐ ํ์ค์ด๋ผ์ ์คํ๋ง์ ๋ ๋ฆฝ์ ์ด๋ผ๋ ์ฅ์ ์ด ์์ต๋๋ค.
2 ) @Scope์ proxyMode ์ค์
@Component
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class ProtoProxy {}
@Component
@AllArgsConstructor
public class ScopeWrapper {
...
@Getter
ProtoProxy protoProxy;
}
- ์์ ๊ฐ์ด Protytpe์ proxyMode ์ค์ ์ ์ถ๊ฐํฉ๋๋ค.
- ํ๋ก์ ์ ์ฉ ๋์์ด ํด๋์ค๋ฉด TARGET_CLASS, ์ธํฐํ์ด์ค๋ฉด INTERFACE๋ฅผ ์ ํํฉ๋๋ค.
ํ์ต์ ๋ง์ ๋์์ด ๋ ๋งํฌ๋ฅผ ์ฐธ์กฐํ์ต๋๋ค. ๐
์ฐธ์กฐ ๋ฐ ์ถ์ฒ
[ Spring ] Spring Bean ์ด ์ ๋ฆฌ