๐ Spring Container์ Spring์ผ๋ก ๊ตฌ์ฑ๋ ์ ํ๋ฆฌ์ผ์ด์ ์์ ๊ฐ์ฒด(Bean)๋ฅผ ์์ฑ, ๊ด๋ฆฌ, ์๋ฉธํ๋ ์ญํ ์ ๋ด๋นํ๋ค. ์ ํ๋ฆฌ์ผ์ด์ ์์ ์, ์ค์ ํ์ผ์ด๋ Annotation์ ์ฝ์ด Bean์ ์์ฑํ๊ณ ์ฃผ์ ํ๋ ๋ชจ๋ ๊ณผ์ ์ ์ปจํธ๋กคํ๋ค.
โ ์ฌ์ฉํ๋ ํด๋์ค์์ ์ง์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค.
โ ๊ฐ์ฒด ๊ฐ์ ์์กด์ฑ์ด ๋์์ง๊ณ ๊ฒฐํฉ๋๊ฐ ์ฆ๊ฐ โ ์ ์ง๋ณด์ ์ด๋ ค์
public class OrderService {
private final PaymentService paymentService = new PaymentService(); // ์ง์ ์์ฑ
}
โ Spring Container๊ฐ ๊ฐ์ฒด๋ฅผ ๋์ ์์ฑํ๊ณ ๊ด๋ฆฌ
โ ๋์จํ ๊ฒฐํฉ(OCP, DIP ์์น ์ค์)์ผ๋ก ์ ์ฐํ ์ค๊ณ ๊ฐ๋ฅ
@Component
public class OrderService {
private final PaymentService paymentService;
@Autowired
public OrderService(PaymentService paymentService) { // ์์กด์ฑ ์ฃผ์
this.paymentService = paymentService;
}
}
โ
@Autowired
๋ฅผ ํตํด Spring Container๊ฐ ์๋์ผ๋ก ์์กด์ฑ ์ฃผ์
๐ ๊ฐ์ฒด(Bean)๋ฅผ ์์ฑ ๋ฐ ๊ด๋ฆฌํ๊ณ ์์กด์ฑ์ ์ฃผ์ ํ๋ ์ญํ ์ ๋ด๋นํ๋ค.
โ ์ฌ๋ฃ(Bean) ์ ํ โ ์๋ฆฌ(๊ฐ์ฒด ์์ฑ ๋ฐ ๊ด๋ฆฌ) โ ์์ฑ(์์กด์ฑ ์ฃผ์ ๋ฐ ์ ๊ณต)๊น์ง ๋ชจ๋ ๊ฒ์ ๊ด๋ฆฌํ๋ค.
โ ๊ฐ์ฒด๋ฅผ ์ง์ ์์ฑํ๋ ๊ฒฝ์ฐ, ๊ฐ์ฒด ๊ฐ์ ์์กด์ฑ ๋ฐ ๊ฒฐํฉ๋๊ฐ ๋์์ง๊ณ OCP, DIP์ ์๋ฐํ๊ฒ ๋๋ค.
โ Spring Container๋ฅผ ์ฌ์ฉํ๋ฉด ์ธํฐํ์ด์ค์๋ง ์์กดํ๋ ์ค๊ณ๊ฐ ๊ฐ๋ฅํด์ ธ OCP, DIP ์ค์ํ ์ ์๊ฒ ๋๋ค.
Spring Container์ ์ต์์ ์ธํฐํ์ด์ค
Spring Bean์ ๊ด๋ฆฌํ๊ณ ์กฐํํ๋ค.
BeanFactory์ ํ์ฅ๋ ํํ(implements) => BeanFactory์ ๊ธฐ๋ฅ์ ๊ฐ์ง๊ณ ์์
Application ๊ฐ๋ฐ์ ํ์ํ ๋ค์ํ ๊ธฐ๋ฅ์ ์ถ๊ฐ์ ์ผ๋ก ์ ๊ณตํ๋ค.
๐ก ์ผ๋ฐ์ ์ผ๋ก ApplicationContext๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ApplicationContext๋ฅผ Spring Container๋ผ ํํํ๋ค.
๐ Spring Bean์ Spring ์ปจํ ์ด๋๊ฐ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด๋ฅผ ์๋ฏธํ๋ค. ์๋ฐ ๊ฐ์ฒด ์์ฒด๋ ํน๋ณํ์ง ์์ง๋ง, Spring์ด ์ด ๊ฐ์ฒด๋ฅผ ๊ด๋ฆฌํ๋ ์๊ฐ๋ถํฐ Bean์ด ๋๋ค. Spring์ Bean์ ์์ฑ, ์ด๊ธฐํ, ์์กด์ฑ ์ฃผ์ ๋ฑ์ ํตํด ๊ด๋ฆฌํ๋ค.
Bean์ new ํค์๋ ๋์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ค.
Spring Container๊ฐ ์ ์ดํ๋ค.
Chef์ธ Spring Container๊ฐ ์๋ฆฌํ ์์์ ์ฌ์ฉ๋ ์ฌ๋ฃ(Bean)
โ Spring ์ปจํ ์ด๋์ ์ํด ๊ด๋ฆฌ๋จ
โ ๊ธฐ๋ณธ์ ์ผ๋ก Singleton(๋จ์ผ ๊ฐ์ฒด๋ก ๊ด๋ฆฌ)์ผ๋ก ์ค์ ๋๋ค.
โ DI(์์กด์ฑ ์ฃผ์ )๋ฅผ ํตํด ๋ค๋ฅธ ๊ฐ์ฒด์ ์ฐ๊ฒฐ ๊ฐ๋ฅ
โ ์์ฑ โ ์ด๊ธฐํ โ ์ฌ์ฉ โ ์๋ฉธ์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง
<beans>
<!-- myBean์ด๋ผ๋ ์ด๋ฆ์ Bean ์ ์ -->
<bean id="myBean" class="com.example.MyBean" />
</beans>
public class MyApp {
public static void main(String[] args) {
// Spring ์ปจํ
์ด๋์์ Bean์ ๊ฐ์ ธ์ด
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
MyService myService = context.getBean("myService", MyService.class);
myService.doSomething();
}
}
๐ @Component
์๋ ๋ฑ๋ก + @Autowired
์์กด์ฑ ์ฃผ์
// ์ด ํด๋์ค๋ฅผ Bean์ผ๋ก ๋ฑ๋ก
// @Controller, @Service, @Repository
@Component
public class MyService {
public void doSomething() {
System.out.println("Spring Bean ์ผ๋ก ๋์");
}
}
@Component
public class MyApp {
private final MyService myService;
@Autowired // ์์กด์ฑ ์๋ ์ฃผ์
public MyApp(MyService myService) {
this.myService = myService;
}
public void run() {
myService.doSomething();
}
}
// com.example ํจํค์ง๋ฅผ ์ค์บํ์ฌ Bean ๋ฑ๋ก
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyApp app = context.getBean(MyApp.class);
app.run();
}
}
๐น @Bean
์ฌ์ฉํ์ฌ ์๋ ๋ฑ๋ก
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
public class MyApp {
public static void main(String[] args) {
// Spring ์ปจํ
์ด๋์์ Bean์ ๊ฐ์ ธ์ด
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MyService myService = context.getBean(MyService.class);
myService.doSomething();
}
}