[Spring Boot] Bean, ์๋ช ์ฃผ๊ธฐ, Bean Scope
๐ Bean์ด๋?
Bean
์ ์คํ๋ง ํ๋ ์์ํฌ์์ IoC(Inversion of Control)
์ปจํ
์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด๋ฅผ ๋งํ๋ค.
Spring Boot์ ํน์ง ์ค ํ๋๋
IoC(Inversion of Control)
์ด๋ค. IoC๋ ๊ฐ์ฒด์ ์์ฑ ๋ฐ ์ ์ด๊ถ, ์๋ช ์ฃผ๊ธฐ ๊ด๋ฆฌ ๋ฑ์ ๊ฐ๋ฐ์๊ฐ ์๋ Spring Boot Framework์๊ฒ ๋งก๊ธฐ๋ ๊ฒ์ด๋ค. ์ด๋ฅผ ํตํด ๊ฐ๋ฐ์๋ new๋ฅผ ํตํด ์์ฑํ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์๋ Spring Boot์ ์ํด ๊ด๋ฆฌ๋๋ bean์ ์ฌ์ฉํ๋ฉด ๋๋ ๊ฒ์ด๋ค.
IoC ์ปจํ
์ด๋๋ bean์ผ๋ก ๋ฑ๋กํ ํด๋์ค๋ค์ ์๋์ผ๋ก ํ์ ๋ฐ ๊ด๋ฆฌํ๋๋ฐ, ์ด๋ฅผ Component Scan
์ด๋ผ๊ณ ํ๋ค.
bean์ ์ฌ์ฉํจ์ผ๋ก์จ ์ป๋ ์ฅ์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ์ ํ๋ฆฌ์ผ์ด์ ์ ๋น์ฆ๋์ค ๋ก์ง์ ์บก์ํํ ์ ์๋ค.
Dependency Injection, DI (์์กด์ฑ ์ฃผ์ )
์ ํตํด ๊ฐ์ฒด ๊ฐ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ฌ์ฌ์ฉ์ฑ์ ๋์ผ ์ ์๋ค.
Dependency Injection
์ ๊ฐ์ฒด ๊ฐ ์์กด์ฑ์ ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ์ด๋ค. ์์กด์ฑ์ด๋ ํด๋์ค๊ฐ ๋ค๋ฅธ ํด๋์ค๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
- ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ํํ ์ ์์ผ๋ฉฐ ์ ์ง๋ณด์์ฑ์ ํฅ์์ํฌ ์ ์๋ค.
๐ Bean์ ๊ตฌ์ฑ ์์
ApplicationContext
๋ IoC ์ปจํ ์ด๋์ ์ธํฐํ์ด์ค์ด๋ค. Spring Boot ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๋ฉด ์๋์ผ๋ก ์์ฑ๋๋ค. bean์ ๋ํ ์ ๋ฐ์ ์ธ ๊ด๋ฆฌ์ ์์กด์ฑ ์ฃผ์ ์ ์ํํ๋ค.Bean
์ IoC ์ปจํ ์ด๋์ ์ํด ์์ฑ ๋ฐ ๊ด๋ฆฌ๋๋ ๊ฐ์ฒด์ด๋ค. ์ ํ๋ฆฌ์ผ์ด์ ์ ํต์ฌ ๋ก์ง์ ์บก์ํํ๋ค.Dependency Injection(DI)
๋ ํด๋์ค ๊ฐ ์์กด์ฑ์ ์ธ๋ถ์์ ์ฃผ์ ๋ฐ๋ ๋ฐฉ๋ฒ์ด๋ค. IoC ์ปจํ ์ด๋๊ฐ bean ๊ฐ์ ์์กด์ฑ์ ์๋์ผ๋ก ์ฐ๊ฒฐํ๋ค.
Bean์ ์ ์ํ๋ ๋ฐฉ๋ฒ
- XML
1
2
3
<bean id="myBean" class="com.example.MyClass">
<property name="propertyName" value="propertyValue"/>
</bean>
<bean>
ํ๊ทธ๋ฅผ ํตํด ํด๋์ค์ ์์ฑ์ ๋ช
์์ ์ผ๋ก ์ง์ ํ ์ ์๋ค. ์์ฃผ ์ฌ์ฉ๋๋ ๋ฐฉ๋ฒ์ ์๋๋ค.
- Java ๊ธฐ๋ฐ
1
2
3
4
5
6
7
8
@Configuration
public class AppConfig {
@Bean
public MyBean myBean() {
return new MyBean();
}
}
@Configuration
, @Bean
์ด๋
ธํ
์ด์
์ ํตํด Java ์ฝ๋๋ก bean์ ์ ์ํ ์ ์๋ค.
- ์ด๋ ธํ ์ด์ ๊ธฐ๋ฐ
1
2
3
4
@Component
public class MyService {
// ...
}
@Component
, @Service
, @Repository
, @Controller
๋ฑ์ ์ด๋
ธํ
์ด์
์ ํตํด ์๋์ผ๋ก bean์ ๋ฑ๋กํ ์ ์๋ค.
์ฐธ๊ณ ๋ก @Service, @Repository, @Controller์ ๊ฐ์ ์ด๋ ธํ ์ด์ ์ @Component์ ์์์ ๋ฐ๊ณ ์๋ค.
DI๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ
- ์์ฑ์ ์ฃผ์
1
2
3
4
5
6
7
8
9
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
๊ฐ์ฒด์ ์์กด ๊ด๊ณ๊ฐ ํ ๋ฒ ์ฃผ์ ๋๋ฉด ๋ค์ ๋ณ๊ฒฝ๋์ง ์์์ ๋ณด์ฅํ๋ฉฐ, ํ ์คํธ ์ Mock ๊ฐ์ฒด๋ฅผ ์ฝ๊ฒ ์ฃผ์ ํ ์ ์๋ค.
- ํ๋ ์ฃผ์
1
2
3
4
5
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
}
์์กด์ฑ์ด ๋ช ํํ๊ฒ ๋๋ฌ๋์ง ์์ผ๋ฉฐ ํ ์คํธ ์ Mock ๊ฐ์ฒด๋ฅผ ์ฃผ์ ํ๊ธฐ ์ด๋ฝ๋ค. ๊ถ์ฅํ์ง ์๋ ๋ฐฉ๋ฒ์ด๋ค.
- Setter ์ฃผ์
1
2
3
4
5
6
7
8
9
@Service
public class UserService {
private UserRepository userRepository;
@Autowired
public void setUserRepository(UserRepository userRepository) {
this.userRepository = userRepository;
}
}
์ ํ์ ์ธ ์์กด์ฑ์ด๋ ๋ฐํ์์ ๋ณ๊ฒฝ ๊ฐ๋ฅํด์ผ ํ๋ ์์กด์ฑ์ ์ ํฉํ ๋ฐฉ๋ฒ์ด๋ค.
๐ Bean์ ์๋ช ์ฃผ๊ธฐ
bean์ ์๋ช ์ฃผ๊ธฐ ๋จ๊ณ๋ ๋ค์๊ณผ ๊ฐ๋ค.
๊ฐ์ฒด ์์ฑ(Instantiation)
์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฑ๋ก๋ bean์ IoC ์ปจํ ์ด๋๊ฐ ํ์ํ๋ค. ์์ฑ์๋ ์์กด์ฑ ์ฃผ์ ๊ณผ ํจ๊ป ํธ์ถ๋๋ค. bean์ธ ๊ธฐ๋ณธ์ ์ผ๋ก singleton ์ค์ฝํ์์ ํ ๋ฒ๋ง ์์ฑ๋๋ค.
์์กด์ฑ ์ค์ (Dependency Injection)
IoC ์ปจํ ์ด๋๊ฐ ํ์ํ ์์กด์ฑ์ ์ฃผ์ ํ์ฌ bean์ ๊ตฌ์ฑํ๋ค.
์ด๊ธฐํ(Initialization)
bean์ด ์ด๊ธฐํ๋ ํ ์ถ๊ฐ์ ์ธ ์์ ์ ์ํํ ์ ์๋ ๋จ๊ณ์ด๋ค.
์ฌ์ฉ(Ready to Use)
bean์ด ์ด๊ธฐํ๋์๋ค๋ฉด ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ ์ ์๋ค.
์๋ฉธ(Destruction)
IoC ์ปจํ ์ด๋๊ฐ ์ข ๋ฃ๋ ๋ bean์ ๋ฆฌ์์ค๋ฅผ ์ ๋ฆฌํ๊ณ ์๋ฉธํ๋ค.
๐ Bean์ ์ค์ฝํ
bean์ ์ค์ฝํ์ ๋ฐ๋ผ IoC ์ปจํ ์ด๋๊ฐ bean์ ๊ด๋ฆฌํ๋ ๋ฐฉ์์ด ๋ฌ๋ผ์ง๋ค.
Singleton Scope
์ปจํ ์ด๋ ๋น ํ๋์ ์ธ์คํด์ค๋ง ์์ฑ๋๋ฉฐ, ๋ชจ๋ ์์ฒญ์์ ๋์ผํ ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค. ๋ํ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฒด์์ ๊ฐ์ state๋ฅผ ๊ณต์ ํ๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์ด ์์๋๋ฉด ์์ฑ๋๋ฉฐ ์ปจํ ์ด๋๊ฐ ์ข ๋ฃ๋๋ฉด ์๋ฉธ๋๋ค.
Prototype Scope
๋งค ์์ฒญ๋ง๋ค ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. state๋ฅผ ์ ์งํด์ผ ํ๋ ๊ฐ์ฒด์ ์ ํฉํ๋ค. ์ฃผ์ํด์ผ ํ ์ ์, Singleton bean์ Prototype bean์ ์ฃผ์ ํ๋ฉด Prototype bean์ด Singleton bean์ฒ๋ผ ๋์ํ๋ค.
bean ์์ฒญ์ด ๋ค์ด์ค๋ฉด ์์ฑ๋๋ฉฐ ์ ํ๋ฆฌ์ผ์ด์ ์ด ์๋๋ผ GC์ ์ํด ์๋ฉธ๋๋ค.
Request Scope
HTTP ์์ฒญ๋ง๋ค ์๋ก์ด ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. ๋์ผํ ์์ฒญ ๋ด์์๋ง state๋ฅผ ๊ณต์ ํ๋ค.
HTTP ์์ฒญ์ด ์์๋๋ฉด ์์ฑ๋๋ฉฐ, ์์ฒญ์ด ์ข ๋ฃ๋๋ฉด ์๋ฉธ๋๋ค.
Session Scope
์ฌ์ฉ์ ์ธ์ ๋น ํ๋์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. ๋ก๊ทธ์ธํ ์ฌ์ฉ์๋ณ๋ก state๋ฅผ ๊ด๋ฆฌํ๋ค.
์ธ์ ์ด ์์ฑ๋๋ฉด ์์ฑ๋๋ฉฐ ์ธ์ ์ด ๋ง๋ฃ๋๋ฉด ์๋ฉธํ๋ค.
Application Scope
ServiceContext
๋น ํ๋์ ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ฉฐ, ์ ํ๋ฆฌ์ผ์ด์
์ ์ญ์์ ๊ณต์ ๋๋ค.
์ ํ๋ฆฌ์ผ์ด์ ์ด ์์๋๋ฉด ์์ฑ๋๋ฉฐ ์ข ๋ฃ ์ ์๋ฉธ๋๋ค.
WebSocket Scope
WebSocket
์ธ์
๋ณ๋ก ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ค.
WebSocket ์ฐ๊ฒฐ ์ ์์ฑ๋๋ฉฐ ์ฐ๊ฒฐ์ด ์ข ๋ฃ๋๋ฉด ์๋ฉธ๋๋ค.