๐๏ธ[SpringBoot] ์คํ๋ง ๋ถํธ์ ํต์ฌ ๊ฐ๋ : ์ ์ด์ ์ญ์ (IoC) - XML ๊ธฐ๋ฐ ๊ตฌํ
๐ ๊ฐ์
์ ์ด์ ์ญ์ (IoC, Inversion of Control) ์ ๊ฐ์ฒด์ ์์ฑ๊ณผ ์์กด์ฑ ๊ด๋ฆฌ๋ฅผ ๊ฐ๋ฐ์๊ฐ ์๋ Spring Container ๊ฐ ๋ด๋นํ๋๋ก ํ๋ ์ค๊ณ ์์น์ ๋๋ค. XML ์ค์ ํ์ผ์ ํตํด IoC๋ฅผ ๊ตฌํํ๋ ์ ํต์ ์ธ ๋ฐฉ๋ฒ์ ํ์ตํด๋ณด๊ฒ ์ต๋๋ค.
๐จ ๊ธฐ์กด ์์กด์ฑ ์ฃผ์ ์ ํ๊ณ์
์ฌ์ ํ ๋จ์ ๋ฌธ์ ์
์ด์ ์ ์์กด์ฑ ์ฃผ์ ์ ํตํด ํด๋์ค ๊ฐ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ท์ง๋ง, ์ฌ์ ํ ์์ ์ด ํ์ํ ๋ถ๋ถ์ด ์์ต๋๋ค:
public class Main {
public static void main(String[] args) {
CoffeeMaker coffeeMaker = new CoffeeMaker();
// ๐จ ์๋ก์ด ์ปคํผ ๋จธ์ ์ด ์ถ๊ฐ๋ ๋๋ง๋ค ์ด ๋ถ๋ถ์ ์์ ํด์ผ ํจ
coffeeMaker.setCoffeeMachine(new DripCoffeeMachine());
coffeeMaker.makeCoffee();
// ๋ค๋ฅธ ์ปคํผ ๋จธ์ ์ผ๋ก ๋ณ๊ฒฝํ๋ ค๋ฉด ๋ ์์ ํ์
coffeeMaker.setCoffeeMachine(new EspressoMachine());
coffeeMaker.makeCoffee();
}
}
๋ณต์กํ ์์กด์ฑ ๊ด๊ณ์์์ ๋ฌธ์ ์
// ์ค์ ์ ํ๋ฆฌ์ผ์ด์
์์๋ ์ด๋ฐ ๋ณต์กํ ์์กด์ฑ ๊ด๊ณ๊ฐ ๋ฐ์
UserService userService = new UserService();
PaymentService paymentService = new PaymentService();
EmailService emailService = new EmailService();
OrderService orderService = new OrderService();
// ์์กด์ฑ ์ฃผ์
userService.setUserRepository(new UserRepository());
paymentService.setPaymentGateway(new StripePaymentGateway());
emailService.setEmailProvider(new GmailProvider());
orderService.setUserService(userService);
orderService.setPaymentService(paymentService);
orderService.setEmailService(emailService);
// ๐จ ๋งค๋ฒ ์ด๋ฐ ๋ณต์กํ ์ค์ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํจ
๐ ์ ์ด์ ์ญ์ (IoC) ๊ฐ๋
์ ํต์ ๋ฐฉ์ vs IoC ๋ฐฉ์
| ๊ตฌ๋ถ | ์ ํต์ ๋ฐฉ์ | IoC ๋ฐฉ์ |
|---|---|---|
| ๊ฐ์ฒด ์์ฑ | ๊ฐ๋ฐ์๊ฐ new ํค์๋๋ก ์ง์ ์์ฑ |
ํ๋ ์์ํฌ๊ฐ ์๋ ์์ฑ |
| ์์กด์ฑ ์ฃผ์ | ๊ฐ๋ฐ์๊ฐ ๋ฉ์๋ ํธ์ถ๋ก ์ง์ ์ฃผ์ | ํ๋ ์์ํฌ๊ฐ ์๋ ์ฃผ์ |
| ํ๋ฆ ์ ์ด | ๊ฐ๋ฐ์๊ฐ ์ง์ ์ ์ด | ํ๋ ์์ํฌ๊ฐ ๊ฐ์ ์ ์ด |
| ์ค์ ๋ฐฉ๋ฒ | ์์ค์ฝ๋ ๋ด๋ถ์์ ํ๋์ฝ๋ฉ | ์ธ๋ถ ์ค์ ํ์ผ ๋๋ ์ ๋ ธํ ์ด์ |
IoC์ ํต์ฌ ์์ด๋์ด
graph TB
A[๊ฐ๋ฐ์] -->|์ ์ด๊ถ ์ด์| B[Spring Container]
B -->|๊ฐ์ฒด ์์ฑ| C[Bean Objects]
B -->|์์กด์ฑ ์ฃผ์
| D[Dependency Injection]
B -->|์๋ช
์ฃผ๊ธฐ ๊ด๋ฆฌ| E[Lifecycle Management]
๐ XML ๊ธฐ๋ฐ IoC ๊ตฌํ
1๋จ๊ณ: XML ์คํค๋ง ์ค๋น
Spring Framework์ ๊ณต์ XML ์คํค๋ง๋ฅผ ์ฌ์ฉํฉ๋๋ค:
- ์ฐธ์กฐ ๋งํฌ: Spring XML Schemas
2๋จ๊ณ: beans.xml ํ์ผ ์์ฑ
ํ์ผ ์์น: src/main/resources/beans.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- ๋๋ฆฝ ์ปคํผ ๋จธ์ Bean ์ ์ -->
<bean id="dripCoffeeMachine" class="com.example.demo.DripCoffeeMachine"/>
<!-- ์์คํ๋ ์ ๋จธ์ Bean ์ ์ -->
<bean id="espressoMachine" class="com.example.demo.EspressoMachine"/>
<!-- ์ปคํผ ๋ฉ์ด์ปค Bean ์ ์ ๋ฐ ์์กด์ฑ ์ฃผ์
-->
<bean id="coffeeMaker" class="com.example.demo.CoffeeMaker" init-method="makeCoffee">
<property name="coffeeMachine" ref="espressoMachine"/>
</bean>
</beans>
3๋จ๊ณ: ํด๋์ค ๊ตฌ์กฐ ์ ์
์ธํฐํ์ด์ค
public interface CoffeeMachine {
String brew();
}
๊ตฌํ์ฒด๋ค
public class EspressoMachine implements CoffeeMachine {
@Override
public String brew() {
return "Brewing coffee with Espresso Machine";
}
}
public class DripCoffeeMachine implements CoffeeMachine {
@Override
public String brew() {
return "Brewing coffee with Drip Coffee Machine";
}
}
CoffeeMaker ํด๋์ค
public class CoffeeMaker {
private CoffeeMachine coffeeMachine;
// Setter ๋ฉ์๋ (XML์์ property ์ฃผ์
์ ์ํด ํ์)
public void setCoffeeMachine(CoffeeMachine coffeeMachine) {
this.coffeeMachine = coffeeMachine;
}
// init-method๋ก ์ง์ ๋ ๋ฉ์๋
public void makeCoffee() {
System.out.println(coffeeMachine.brew());
}
}
4๋จ๊ณ: Spring Boot ์ ํ๋ฆฌ์ผ์ด์ ์ค์
@SpringBootApplication
@ImportResource("classpath:beans.xml") // XML ์ค์ ํ์ผ import
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
๐งฉ XML ์ค์ ํ์ผ ๊ตฌ์ฑ ์์
<bean> ํ๊ทธ ์์ฑ๋ค
| ์์ฑ | ์ค๋ช | ์์ |
|---|---|---|
| id | Bean์ ๊ณ ์ ์๋ณ์ | id="coffeeMaker" |
| class | Bean์ผ๋ก ์์ฑํ ํด๋์ค (์ ์ฒด ํจํค์ง ๊ฒฝ๋ก) | class="com.example.demo.CoffeeMaker" |
| init-method | ๊ฐ์ฒด ์์ฑ ํ ์๋ ์คํํ ๋ฉ์๋ | init-method="makeCoffee" |
| destroy-method | ๊ฐ์ฒด ์๋ฉธ ์ ์คํํ ๋ฉ์๋ | destroy-method="cleanup" |
<property> ํ๊ทธ - ์์กด์ฑ ์ฃผ์
<!-- Setter ์ฃผ์
-->
<bean id="coffeeMaker" class="com.example.demo.CoffeeMaker">
<property name="coffeeMachine" ref="espressoMachine"/>
<property name="maxCapacity" value="500"/>
</bean>
<!-- ์์ฑ์ ์ฃผ์
-->
<bean id="coffeeMaker" class="com.example.demo.CoffeeMaker">
<constructor-arg ref="espressoMachine"/>
<constructor-arg value="500"/>
</bean>
๐ ์คํ ํ๋ฆ ๋ถ์
1. ์ ํ๋ฆฌ์ผ์ด์ ์์
SpringApplication.run(DemoApplication.class, args);
2. XML ํ์ผ ๋ก๋ฉ
@ImportResource("classpath:beans.xml")
3. Spring Container์ ์์ ์์
- Bean ์ ์ ํ์ฑ: XML์์ Bean ์ ๋ณด๋ฅผ ์ฝ์ด๋ค์
-
๊ฐ์ฒด ์์ฑ: ๊ฐ Bean ํด๋์ค์ ์ธ์คํด์ค ์์ฑ
DripCoffeeMachine dripCoffeeMachine = new DripCoffeeMachine(); EspressoMachine espressoMachine = new EspressoMachine(); CoffeeMaker coffeeMaker = new CoffeeMaker(); -
์์กด์ฑ ์ฃผ์
:
<property>ํ๊ทธ์ ๋ฐ๋ผ ์์กด์ฑ ์ฃผ์coffeeMaker.setCoffeeMachine(espressoMachine); -
์ด๊ธฐํ ๋ฉ์๋ ์คํ:
init-method์คํcoffeeMaker.makeCoffee(); // ์๋ ์คํ
4. ์คํ ๊ฒฐ๊ณผ
Brewing coffee with Espresso Machine
โ XML ๊ธฐ๋ฐ IoC์ ์ฅ์
๐ฏ ์ฝ๋ ๋ณ๊ฒฝ ์๋ ์ค์ ๋ณ๊ฒฝ
<!-- ์์คํ๋ ์ ๋จธ์ ์์ ๋๋ฆฝ ๋จธ์ ์ผ๋ก ๋ณ๊ฒฝํ๋ ค๋ฉด XML๋ง ์์ -->
<bean id="coffeeMaker" class="com.example.demo.CoffeeMaker" init-method="makeCoffee">
<property name="coffeeMachine" ref="dripCoffeeMachine"/> <!-- ์ฌ๊ธฐ๋ง ๋ณ๊ฒฝ! -->
</bean>
๐๏ธ ์ค์์ง์ค์ ์ค์ ๊ด๋ฆฌ
- ๋ชจ๋ Bean ์ค์ ์ ํ ๊ณณ์์ ๊ด๋ฆฌ
- ์์กด์ฑ ๊ด๊ณ๋ฅผ ํ๋์ ํ์ ๊ฐ๋ฅ
- ํ๊ฒฝ๋ณ ์ค์ ๋ถ๋ฆฌ ์ฉ์ด
๐ง ์ ์ฐํ ๊ฐ์ฒด ์๋ช ์ฃผ๊ธฐ ์ ์ด
<bean id="databaseConnection"
class="com.example.DatabaseConnection"
init-method="connect"
destroy-method="disconnect"
scope="singleton">
<property name="url" value="jdbc:mysql://localhost:3306/mydb"/>
</bean>
โ ๏ธ XML ๊ธฐ๋ฐ IoC์ ํ๊ณ์
1. ์ปดํ์ผ ํ์ ๊ฒ์ฆ ๋ถ๊ฐ
<!-- ์คํ๊ฐ ์์ด๋ ์คํ ์์ ์๋ง ์๋ฌ ๋ฐ์ -->
<bean id="coffeeMaker" class="com.example.demo.CoffeeMakr"> <!-- ์คํ! -->
<property name="coffeeMachin" ref="espressoMachine"/> <!-- ์คํ! -->
</bean>
2. IDE ์ง์ ์ ํ
- ์๋์์ฑ, ๋ฆฌํฉํ ๋ง ์ง์ ๋ถ์กฑ
- ํด๋์ค๋ช ๋ณ๊ฒฝ ์ XML ํ์ผ ์๋ ์์ ํ์
3. XML ํ์ผ ๋ณต์ก์ฑ ์ฆ๊ฐ
๋๊ท๋ชจ ํ๋ก์ ํธ์์๋ XML ํ์ผ์ด ๋งค์ฐ ๋ณต์กํด์ง ์ ์์ต๋๋ค.
๐ ํ๋์ ์ ๊ทผ๋ฒ๊ณผ์ ๋น๊ต
Annotation ๊ธฐ๋ฐ ์ค์ (ํ์ฌ ๊ถ์ฅ ๋ฐฉ์)
@Component
public class EspressoMachine implements CoffeeMachine {
// ๊ตฌํ
}
@Component
public class CoffeeMaker {
private final CoffeeMachine coffeeMachine;
@Autowired
public CoffeeMaker(CoffeeMachine coffeeMachine) {
this.coffeeMachine = coffeeMachine;
}
@PostConstruct
public void makeCoffee() {
System.out.println(coffeeMachine.brew());
}
}
Java Config ๊ธฐ๋ฐ ์ค์
@Configuration
public class CoffeeConfig {
@Bean
public CoffeeMachine espressoMachine() {
return new EspressoMachine();
}
@Bean
public CoffeeMaker coffeeMaker(CoffeeMachine coffeeMachine) {
CoffeeMaker maker = new CoffeeMaker();
maker.setCoffeeMachine(coffeeMachine);
return maker;
}
}
๐ฏ ์ธ์ XML ์ค์ ์ ์ฌ์ฉํ ๊น?
์ ํฉํ ๊ฒฝ์ฐ
- ๋ ๊ฑฐ์ ์์คํ ํตํฉ: ๊ธฐ์กด XML ๊ธฐ๋ฐ ์ค์ ๊ณผ์ ํธํ์ฑ ํ์
- ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์ : ์์ค ์ฝ๋ ์์ ์ด ๋ถ๊ฐ๋ฅํ ์๋ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
- ๋ฐํ์ ์ค์ ๋ณ๊ฒฝ: ์ ํ๋ฆฌ์ผ์ด์ ์ฌ๋ฐฐํฌ ์์ด ์ค์ ๋ณ๊ฒฝ ํ์
ํ์ฌ ๊ถ์ฅ ๋ฐฉ์
-
Annotation ๊ธฐ๋ฐ:
@Component,@Service,@Repository -
Java Config:
@Configuration,@Bean -
Spring Boot Auto Configuration:
@SpringBootApplication
๐ก ์ค๋ฌด ํ
XML๊ณผ Annotation ํผ์ฉ
@SpringBootApplication
@ImportResource("classpath:legacy-beans.xml") // ๋ ๊ฑฐ์ XML ์ค์
public class ModernApplication {
// ํ๋์ ์ ๋
ธํ
์ด์
๊ธฐ๋ฐ ์ฝ๋
}
Profile๋ณ XML ์ค์
<!-- beans-dev.xml -->
<beans profile="dev">
<bean id="dataSource" class="com.example.DevDataSource"/>
</beans>
<!-- beans-prod.xml -->
<beans profile="prod">
<bean id="dataSource" class="com.example.ProdDataSource"/>
</beans>
๐ ํ์ต ํฌ์ธํธ
ํต์ฌ ๊ฐ๋
- ์ ์ด์ ์ญ์ : ๊ฐ์ฒด ์์ฑ๊ณผ ๊ด๋ฆฌ ์ฑ ์์ ํ๋ ์์ํฌ์ ์์
- ์ธ๋ถ ์ค์ : ์ฝ๋ ๋ณ๊ฒฝ ์์ด ์ค์ ํ์ผ๋ง์ผ๋ก ๋์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
- ์๋ช ์ฃผ๊ธฐ ๊ด๋ฆฌ: init-method, destroy-method๋ฅผ ํตํ ๊ฐ์ฒด ์๋ช ์ฃผ๊ธฐ ์ ์ด
๋ค์ ํ์ต ๋จ๊ณ
-
Annotation ๊ธฐ๋ฐ IoC:
@Component,@Autowired๋ฑ -
Java Config:
@Configuration,@Bean - Spring Boot Auto Configuration: ์๋ ์ค์ ์ ์๋ฆฌ
- Bean Scope: Singleton, Prototype, Request, Session
XML ๊ธฐ๋ฐ IoC๋ Spring Framework์ ๊ธฐ์ด๋ฅผ ์ดํดํ๋ ๋ฐ ์ค์ํ ๊ฐ๋ ์ด์ง๋ง, ํ์ฌ๋ ๋ ๊ฐํธํ๊ณ ํ์ ์์ ํ Annotation ๊ธฐ๋ฐ ์ค์ ์ ์ฃผ๋ก ์ฌ์ฉํฉ๋๋ค.