๐ก Spring ํต์ฌ ๊ฐ๋
์ธํฐ๋ทฐ Q&A
Q. Bean์ด๋
- SpringBean์ ๋จ์ํ ๊ฐ์ฒด๊ฐ ์๋๋ผ SpringContainer(ApplicationContext)์ ์ํด ๊ด๋ฆฌ๋๊ณ lifeCylcle์ด ์ ์ด๋๋ ์ธ์คํด์ค
- Spring์ด
IoC ์์น์ ๋ฐ๋ผ ์ง์ ์์ฑํ๊ณ DI๋ก ์ฃผ์
ํด์ฃผ๋ ๊ฐ์ฒด์
๋๋ค.
@Component, @Service, @Repository, @Controller ๋๋ @Bean์ผ๋ก ๋ฑ๋ก๋ฉ๋๋ค.
- Spring์์ Bean์Spring Container๊ฐ ๊ด๋ฆฌํ๋ ํต์ฌ ๋จ์ ๊ฐ์ฒด์ด๋ฉฐ,
- ์๋ช
์ฃผ๊ธฐ์ Scope, ์ฃผ์
๋ฐฉ์์ ๋ฐ๋ผ ์ค๊ณ์ ์ฑ๋ฅ์ ํฐ ์ํฅ์ ์ค ์ ์์ต๋๋ค.
ํน์ง
- IoC์ ์ํด ๊ฐ์ฒด ์์ฑ, DI, ์๋ช
์ฃผ๊ธฐ ๊ด๋ฆฌ๋ฅผ Spring์ด ๋ด๋น
- AOP, ํธ๋์ญ์
, Validator ๋ฑ Spring ๊ธฐ๋ฅ์ ์ ์ฉ ๋์
- ์ฑ๊ธํค ์บ์๋ก ๊ด๋ฆฌ๋๋ฏ๋ก ์ํ๋ฅผ ๊ฐ๋ ํ๋๋ฅผ ์ฃผ์ํด์ ์ฌ์ฉํด์ผํ๋ค.
์์ฑ
- ์ ๋
ธํ
์ด์
๊ธฐ๋ฐ
- ํด๋์ค์
@Component, @Service, @Controller ๋ฑ ๋ถ์ฌ
- Java Config ๊ธฐ๋ฐ
- @Configuration ํด๋์ค์์ @Bean ๋ฉ์๋
Q. ๋น ๋ฑ๋ก ์์ฑ
- ์ฃผ๋ก ์ฌ์ฉ๋๋ ์ ๋
ธํ
์ด์
๊ธฐ๋ฐ, Config๊ธฐ๋ฐ ๋ฐฉ์์ด ์๋ค.
@Component vs @Bean
- @Component(@Service, @Repository, @Controller)๋ ์คํ๋ง์ปจํ
์ด๋๊ฐ ์ปดํฌ๋ํธ ์ค์บํ์ฌ ๋น์ ์๋ ๋ฑ๋ก, ํด๋์ค๋จ์ ์ ์ธ
- @Bean์ ์ง์ ์ ์ธํด์ผ ๋น ๋ฑ๋ก๋๋ค. ๋ฉ์๋ ๋จ์ ์ ์ธ
Q.Bean ์๋ช
์ฃผ๊ธฐ๋
- ์ปดํฌ๋ํธ ์ค์บ๋์ด BeanDefinition ์์ฑ โ ๋ฑ๋ก
- ์์ฑ์ํธ์ถ๋๊ณ ๊ฐ์ฒด ์์ฑ(Bean ๊ฐ์ฒด ์์ฑ (instantiateBean))
- ์์กด์ฑ ์ฃผ์
- ์ด๊ธฐํ ์ฝ๋ฐฑ @PostConstruct
- ์์ฉ
- ์๋ฉธ ์ฝ๋ฐฑ @PreDestroy
Q. Bean Scope
- ๋น์ด ์ด๋ป๊ฒ ์ธ์ ์์ฑ๋๊ณ ๊ด๋ฆฌ๋๋์ง๋ฅผ ๊ฒฐ์ ํ๋ ์ค์ ์
๋๋ค.
- ์คํ๋ง์ ๊ธฐ๋ณธ ๋น ์ค์ฝํ๋ ์ฑ๊ธํค์ด๋ค
- ์ฆ ์คํ๋ง์ปจํ
์ด๋์์๋ ํ๋์ ๋น(์ธ์คํด์ค)๋ง ์กด์ฌํ๋ค.
- prototype์ ์์ฒญ๋ง๋ค ์๋ก์ด ๋น์ ์์ฑํ๋ฏ๋ก singleton๊ณผ ๋ฐ๋๋๋ ๊ฐ๋
์ด๋ผ๊ณ ๋ณผ ์๋ ์๋ค.
| Scope | ์ค๋ช
| ์ฌ์ฉ ํ๊ฒฝ |
|---|
| singleton (๊ธฐ๋ณธ๊ฐ) | ์ปจํ
์ด๋๋น ํ ๊ฐ์ ์ธ์คํด์ค | ์ผ๋ฐ ์๋น์ค ๋ก์ง |
| prototype | ์์ฒญ๋ง๋ค ์ ๊ฐ์ฒด ์์ฑ (DI๋ง ํ๊ณ ์ดํ ๊ด๋ฆฌ X) | ์ํ๋ฅผ ๊ฐ์ง ๊ฐ์ฒด ๋ฑ |
| request | HTTP ์์ฒญ๋ง๋ค ํ ๊ฐ | Web Application |
| session | ์ฌ์ฉ์ ์ธ์
๋ง๋ค ํ ๊ฐ | Web Application |
| application | ServletContext ๋ฒ์ | Web Application |
| websocket | WebSocket ์ฐ๊ฒฐ๋ง๋ค ํ ๊ฐ | ์ค์๊ฐ ํต์ ๋ฑ |
Q. BeanScope์์ ์ฑ๊ธํค๊ณผ ํ๋กํ ํ์
singleton
- ์ปจํ
์ด๋ ์์ฑ ์ ๋ฑ๋ก๋ ๋ชจ๋ Bean์ ์ฆ์ ์ด๊ธฐํ
- ํ๋์ Bean์ ์ฌ๋ฌ ๊ฐ์ฒด์์ ๊ณต์
- ์ํ(state)๊ฐ ์๋ ํ๋๊ฐ ์๋ค๋ฉด ๋์์ฑ ์ฃผ์ ํ์
prototype
- ์์ฒญ ์๋ง๋ค ์๋ก์ด Bean ์์ฑ
- ์์ฑ๊ณผ ์์กด์ฑ ์ฃผ์
๊น์ง๋ง Spring์ด ๊ด๋ฆฌ โ ์ดํ๋ ์ง์ ๊ด๋ฆฌํด์ผ ํจ
@PreDestroy ๋ฑ ์๋ช
์ฃผ๊ธฐ ์ฝ๋ฐฑ ๋์ํ์ง ์์
Q. Singleton ๋ด๋ถ์์ Prototype ์ฃผ์
์ ์ฃผ์ํ ์
- Spring์ ์์ฑ๊ณผ Di๊น์ง๋ง ์ฒ๋ฆฌํ๊ณ ์ดํ ์๋ช
์ฃผ๊ธฐ๋ฅผ ๊ด๋ฆฌํ์ง ์์
- ์คํ๋ง์ ์ฑ๊ธํค์ด ๊ธฐ๋ณธ์ธํ
์ด๋ฏ๋ก ์ฑ๊ธํค ์ค์ฝํ๊ฐ ์ฐ์ ๋๋ค.
- ๋ฐ๋ผ์ ํ๋กํ ํ์
๋น์ ๊ฒฝ์ฐ์, ์ฑ๊ธํค์์ ํ๋กํ ํ์
๋น์ ์ฃผ์
ํ๋ฉด ํญ์ ๊ฐ์ ๊ฐ์ฒด๊ฐ ์ฃผ์
๋๋ค.
์๋์ฒ๋ผ ์ฐ๋ฉด ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค
@Component
public class SingletonService {
@Autowired
private PrototypeBean prototypeBean;
public void logic() {
prototypeBean.doSomething();
}
}
ํด๊ฒฐ๋ฐฉ๋ฒ1 : ObjectProvider
@Autowired
private ObjectProvider<PrototypeBean> prototypeProvider;
public void logic() {
PrototypeBean bean = prototypeProvider.getObject();
}
ํด๊ฒฐ๋ฐฉ๋ฒ2 : @Lookup
@Lookup
public PrototypeBean getPrototypeBean() {
return null;
}
Q. Bean ๋ฑ๋ก/์ค์บ ์ ์ฃผ์ํ ์
- ์ค๋ณต ๋ฑ๋ก ์ โ
NoUniqueBeanDefinitionException ๋ฐ์ํ๋ฉฐ ํด๊ฒฐ : @Qualifier ๋๋ @Primary๋ก ํด๊ฒฐ
- ์์กด์ฑ ์ํ ๋ฐ์ ์ โ
BeanCurrentlyInCreationException ๋ฐ์ํ๋ฉฐ ํด๊ฒฐ : ์์ฑ์ ์ฃผ์
+ @Lazy ๋๋ ์ธํฐ ์ฃผ์
๊ณ ๋ ค
- ๋น์ด ์๋ ๊ฐ์ฒด์ AOP ์ ์ฉ ๋ถ๊ฐ
Q. Spring Bean์ด ์๋ ๊ฐ์ฒด์๋ AOP๊ฐ ์ ์ฉ๋์ง ์๋
- Spring AOP๋ ํ๋ก์ ๊ธฐ๋ฐ์ด๊ธฐ๋๋ฌธ์ spring Container๊ฐ ๊ด๋ฆฌํ๋ Bean๋ง ๊ฐ์ ์ ์๊ธฐ ๋๋ฌธ์
- Spring AOP๋ ์คํ๋ง ์ปจํ
์ด๋๊ฐ Bean ๋ฑ๋ก ์ ํด๋น ๊ฐ์ฒด๋ฅผ ํ๋ก์๋ก ๊ฐ์ธ๋ ๋ฐฉ์์ผ๋ก ๋์ํ๋ค.
- Spring Container๊ฐ ๊ด๋ฆฌํ์ง ์๋ ๊ฐ์ฒด๋ ํ๋ก์๋ ์์ฑ๋์ง ์์ AOP๊ฐ ์ ์ฉ๋์ง ์๋๋ค.
- ๊ทธ๋์ ์ง์ ์์ฑํ ๊ฐ์ฒด์๋ ํ๋ก์๋ ์๊ณ AOP๋ ์ ์ฉ์๋๋ค.