Spring Bean
๋น ๋น ๋์๊ฐ๋ ํ์ ๋ชฉ๋ง์ฒ๋ผ

์ ์
Spring IoC Container
Container๋ ์๋์ ๋ฉํ๋ฐ์ดํฐ๋ก ์์ฑํ ์ ์์
XML
Java Config
Componenet Scan
XML
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="demoService" class="com.example.demo.BookService">
<property name="repository" ref="bookRepository"/>
<property name="count" value="42"/>
</bean>
<bean id="bookRepository" class="com.example.demo.BookRepository"/>
</beans>
Java Config
@Configuration
public class AppConfig {
@Bean
public BookServiceImpl BookService() {
return new BookServiceImpl();
}
}
Componenet Scan
@Component()
public class BookRepository {}
@Component("bookService")
public class BookService {}
@Configuration
@ComponentScan(basePackages="book") // ํด๋น ํจํค์ง์ ์ํ ์ปดํฌ๋ํธ ์ค์บ. type-safe X
@ComponentScan(basePackagesClasses=BookService.class) // ํด๋น ํด๋์ค๊ฐ ์ํ ํจํค์ง๋ฅผ ์ค์บ
public class JavaBasicConfig{}
Bean Scope
๊ฐ์ฒด์ ์ธ์คํด์ค๊ฐ ์ค์ง 1๊ฐ๋ง ์์ฑ๋๋ ๊ฒ์ ๋ณด์ฅํ๋ ๋์์ธ ํจํด
์ข ๋ฅ
request
- HTTP ์์ฒญ์ด ๋ค์ด์ค๊ณ ๋๊ฐ ๋๊น์ง ์ ์ง๋๋ ์ค์ฝํ๋ก ๊ฐ๊ฐ์ HTTP ์์ฒญ๋ง๋ค ๋ณ๋์ ๋น ์ธ์คํด์ค๊ฐ ์์ฑ๋๊ณ ๊ด๋ฆฌ๋จ
session
- HTTP ์ธ์ ๊ณผ ๋์ผํ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง๋ ์ค์ฝํ
application
- ์๋ธ๋ฆฟ ์ปจํ ์คํธ์ ๋์ผํ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง๋ ์ค์ฝํ
websocket
- ์น ์์ผ๊ณผ ๋์ผํ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง๋ ์ค์ฝํ
Bean Scope
๋ฐ๋ก ์ค์ ํด ๋ ๊ฒ์ด ์์ผ๋ฉด ๋น์ด ์ฑ๊ธํค์ ๋ฒ์๋ก ๋ฑ๋ก

์ฃผ์์
๊ฐ์ฒด์ ์ํ๋ฅผ ์ ์งํ๊ฒ ์ค๊ณํ๋ฉด ์๋จ. โ stateless ํด์ผ ํจ
ํน์ ํด๋ผ์ด์ธํธ์ ์์กด์ ์ด๊ฑฐ๋ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ ํ๋๊ฐ ์์ผ๋ฉด ์๋จ
๊ฐ๊ธ์ ์ฝ๊ธฐ๋ง ํ์ฉ
Prototype Scope
statefulํ ๋น์ ์์ฑํ ์ ์์
ํด๋ผ์ด์ธํธ๊ฐ ์คํ๋ง ๋น์ ์๊ตฌํ ๋๋ง๋ค ๋น ์์ฑ, ์์กด์ฑ ์ฃผ์ , ๋ฐํ์ ์ํํจ

Life-Cycle
์ฅ๋จ์
์ฅ์
์์กด์ฑ ์ฃผ์ ๊ฐ๋ฅํจ:
์คํ๋ง์ ์์กด์ฑ ์ฃผ์ ์ ์ง์ํ์ฌ ๊ฐ์ฒด ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ๊ณผ ํ ์คํธ ์ฉ์ด์ฑ์ ๋์ผ ์ ์์.๊ฐ์ฒด ์ฌ์ฌ์ฉ ๊ฐ๋ฅํจ:
์คํ๋ง ๋น์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฑ๊ธํค ๋ฒ์๋ฅผ ๊ฐ์ง๋ฏ๋ก, ํ ๋ฒ ์์ฑ๋ ๋น ๊ฐ์ฒด๋ ์ฌ๋ฌ ๊ณณ์์ ์ฌ์ฌ์ฉํ ์ ์์.ํ ์คํธ ์ฉ์ดํจ:
์คํ๋ง์ ์์กด์ฑ ์ฃผ์ ๊ณผ AOP๋ฅผ ํตํด ํ ์คํธํ๊ธฐ ์ด๋ ค์ด ์ฝ๋๋ฅผ ๋ถ๋ฆฌํ๊ณ , ํ ์คํธ ์ฉ์ด์ฑ์ ๋์ผ ์ ์์.
๋จ์
์คํ๋ง ์ปจํ ์ด๋์ ์ข ์์ ์:
์คํ๋ง ๋น์ ์คํ๋ง ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋๋ฏ๋ก, ์คํ๋ง ์์ด๋ ์ฌ์ฉํ๊ธฐ ์ด๋ ค์.์ค์ ๋ณต์กํจ:
์คํ๋ง์ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง๋ง, ๊ทธ๋งํผ ์ค์ ์ด ๋ณต์กํ๊ณ ํ์ต ๊ณก์ ์ด ๊ฐํ๋ฆ.


