Spring IoC Container๊ฐ ๊ด๋ฆฌํ๋ ์๋ฐ ๊ฐ์ฒด
Spring์์ ์ฌ์ฉํ๋ POJO ๊ธฐ๋ฐ์ ๊ฐ์ฒด
โ๏ธ IOC(Inversion Of Control) : ์ ์ด์ ์ญ์
์ํฉ๊ณผ ํ์์ ๋ฐ๋ผ Bean์ ์ฌ์ฉํ ๋ ํ๋๋ง ๋ง๋ค์ด์ผ ํ ์๋ ์๊ณ ์ฌ๋ฌ๊ฐ๊ฐ ํ์ํ ์๋ ์๊ณ ์ด๋ค ํ ์์ ์๋ง ์ฌ์ฉํด์ผํ ๋๊ฐ ์์ ์ ์์
โก๏ธ scope๋ฅผ ์ค์ ํด์ Bean์ ์ฌ์ฉ ๋ฒ์๋ฅผ ๊ฐ๋ฐ์๊ฐ ์ค์ ํ ์ ์์
@Component๋ฅผ ๋ช ์ํ์ฌ bean์ ์ถ๊ฐ
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Indexed
public @interface Component {
@Configuration ์ด๋ ธํ ์ด์ ํ์ฉ
@Configuration
public class AppConfig {
@Bean
public MemberRepository memberRepository() {
return new MemoryMemberRepository();
}
@Bean
public MemberService memberService() {
return new MemberServiceImpl(memberRepository());
}
}
@Component
ํด๋น Bean์ ๋ํด IoC ์ปจํ
์ด๋์์ ๋จ ํ๋์ ๊ฐ์ฒด๋ก๋ง ์กด์ฌ
(spring์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ชจ๋ bean์ singleton์ผ๋ก ์์ฑํ์ฌ ๊ด๋ฆฌ)
ํด๋น Bean์ ๋ํด ๋ค์์ ๊ฐ์ฒด๊ฐ ์กด์ฌํ ์ ์์
ํด๋น Bean์ ๋ํด ํ๋์ HTTP Request์ ๋ผ์ดํ์ฌ์ดํด์์ ๋จ ํ๋์ ๊ฐ์ฒด๋ก๋ง ์กด์ฌ
ํด๋น Bean์ ๋ํด ํ๋์ HTTP Session์ ๋ผ์ดํ์ฌ์ดํด์์ ๋จ ํ๋์ ๊ฐ์ฒด๋ก๋ง ์กด์ฌ
ํด๋น Bean์ ๋ํด ํ๋์ Global HTTP Session์ ๋ผ์ดํ ์ฌ์ดํด์์ ๋จ ํ๋์ ๊ฐ์ฒด๋ก๋ง ์กด์ฌ
โ๏ธ Scope๋ค์ Bean์ผ๋ก ๋ฑ๋กํ๋ ํด๋์ค์ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ค์ ๊ฐ๋ฅ
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
@Scope("prototype")
@Component
public class UserController {
}
[์ฐธ๊ณ ์๋ฃ]