์์กด๊ด๊ณ ์ฃผ์ (DI)์ด๋?
์ด๋ค ๊ฐ์ฒด๊ฐ ๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํ์๋ก ํ ๋, ๊ทธ๊ฑธ ์ง์ newํ์ง ์๊ณ ์คํ๋ง์ด ๋์ ๋ฃ์ด์ฃผ๋ ๊ฒ.
์ฝ๊ฒ ๋งํด, ํ์ํ ๊ฐ์ฒด๋ฅผ ๋ด๊ฐ ๋ง๋ค์ง ์๊ณ , ์คํ๋ง์ด ๋์ ๋ง๋ค์ด์ ๋ฃ์ด์ฃผ๋ ๊ฒ
๊ทธ๋ฆฌ๊ณ ๋ฐฉ๋ฒ ์ค ์์ฑ์ ์ฃผ์ ์ด ๊ฐ์ฅ ์์ ์ ์ด๊ณ ๊ถ์ฅ๋๋ ๋ฐฉ์์ด๋ค.
Bean๊ณผ ์์กด๊ด๊ณ ์ฃผ์ (DI)์ ๊ด๊ณ
Bean์ ์คํ๋ง์ด ๋์ ๋ง๋ค์ด์ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด๊ณ
์์กด๊ด๊ณ์ฃผ์ ์ ๊ทธ Bean์ ํ์ํ ๊ณณ์์ ์๋์ผ๋ก ๋ฃ์ด์ฃผ๋ ํ์๋ผ๊ณ ์ดํดํ๋ฉด ๋๋ค.
๋ฆฌ์กํธ๋ก ๋น์ ํ์๋ฉด,
// ์ง์ new ํ๋ ๊ตฌ์กฐ (์์กด๊ด๊ณ ์ฃผ์
x)
function Profile() {
const user = new User(); // ๋ด๊ฐ ์ง์ ๋ง๋ค์์
return <div>{user.name}</div>;
}
// ์์กด๊ด๊ณ ์ฃผ์
๊ตฌ์กฐ (Context ๋๋ props๋ก ์ ๋ฌ๋ฐ์)
function Profile({ user }) {
return <div>{user.name}</div>;
}
user๋ฅผ ์ง์ ๋ง๋ค์ง ์๊ณ ๋ฐ์์ ๋ฃ์ด์ฃผ๋๋ฐ, ์ด๊ฒ ๋ฐ๋ก ์์กด์ฑ ์ฃผ์ .
์คํ๋ง์์๋ ๋ฐฉ๋ฒ์ด 4๊ฐ์ธ๋ฐ, 3๊ฐ๋ง ๊ธฐ์ตํ๋ฉด ๋๋ค.
1. ์์ฑ์ ์ฃผ์
- ๊ฐ์ฅ ์์ ํ๊ณ ๊ถ์ฅ๋๋ ๋ฐฉ์(๋ถ๋ณ+ํ์)
2. ํ๋ ์ฃผ์
- ํ ์คํธ ์ด๋ ค์, ์ธ๋ถ์์ ๋ฐ๊พธ๊ธฐ ์ด๋ ค์(์ง์)
3. Setter์ฃผ์
- ํ์ํ ๋๋ง, ์ ํ์ฌํญ์ผ ๋ ์ฌ์ฉ
์ ์ง์ new ํ์ง ์๊ณ ์์กด์ฑ ์ฃผ์ (DI)์ ์ธ๊น?
์ง์ new ํ๋ฉด ๊ทธ ์์์ ์ด๋ค ๊ฑธ ์ฐ๋์ง ์ ์ ์์ง๋ง, ์ฌ์ฌ์ฉ๋ ์ด๋ ต๊ณ ํ ์คํธ๋ ํ๋ค๊ณ ๋ณ๊ฒฝ๋ ์ด๋ ค์
๋ฐ๋ฉด, ์์กด๊ด๊ณ๋ฅผ ๋ฐ๊นฅ์์ ๋ฃ์ด์ฃผ๊ธฐ ๋๋ฌธ์
ํ ์คํธํ ๋๋ ์ฝ๊ฒ ๊ฐ์ง๊ฐ์ฒด๋ฅผ ๋ฃ์ ์ ์๊ณ
์ฝ๋ ์์ ์์ด ์๋ก์ด ๊ตฌํ์ฒด๋ก ๊ฐ์๋ผ์ธ ์ ์๊ณ
๊ฐํ๊ฒ ๊ฒฐํฉ๋์ง ์์ผ๋ ์ ์ง๋ณด์๊ฐ ํธํด์ง๋ค.
@Component
public class NotificationService {
public void send() {
System.out.println("์๋ฆผ ์ ์ก ์๋ฃ!");
}
}
@Component
@RequiredArgsConstructor
public class UserService {
private final NotificationService notificationService;
public void process() {
// ์ด ๊ฐ์ฒด๋ ๋ด๊ฐ new๋ก ๋ง๋ ๊ฒ ์๋
// ์คํ๋ง์ด ๋์ ๋ฃ์ด์ค Bean์
notificationService.send();
}
}
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
var context = SpringApplication.run(MainApp.class, args);
// ์คํ๋ง์ด ๋ง๋ UserService๋ฅผ ๊ฐ์ ธ์์ ์คํ
UserService userService = context.getBean(UserService.class);
userService.process();
}
}
์ฌ๊ธฐ์ ์ค์ํ ํฌ์ธํธ๋ UserService๊ฐ NotificationService๋ฅผ ์ง์ newํ์ง ์์๋ค๋ ๊ฒ.
๋์ ์คํ๋ง์ด ์ปจํ ์ด๋์ NotificationService๋ฅผ ๋ฑ๋กํด ๋๊ณ ํ์ํ ๊ณณ์ ์๋์ผ๋ก ๋ฃ์ด์ค<= ์ด๊ฒ DI
์์ฑ์ ์ฃผ์ ์ด๋?
@Component
public class MyApp {
private final MyService myService;
@Autowired // ์คํ๋ง์ด ์ด ์์ฑ์์ ์ฃผ์
ํด์ค
public MyApp(MyService myService) {
this.myService = myService;
}
public void run() {
myService.doSomething();
}
}
์คํ๋ง์ด ์ฑ ์คํํ ๋, MyApp ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด์ผ ํ๋ค.
๊ทผ๋ฐ ์์ฑ์ ๋ณด๋๊น MyService๋ฅผ ์ค์ผ ๋ง๋ค์ด์ง ์ ์์
๊ทธ๋์ ์คํ๋ง์ด ์ปจํ ์ด๋์์ MyService ๋น์ ๊บผ๋ด์ ๋ฃ์ด์ค๋ค.
์ด๋ ๊ฒ ์๋์ผ๋ก ์ฐ๊ฒฐํด์ฃผ๋ ๊ฒ ์์กด๊ด๊ณ ์ฃผ์ !!!
React๋ก ๋น์ ํด๋ณด์๋ฉด,
function App() {
const user = new User(); // ์คํ๋ง์ด ๋์ ๋ง๋ค์ด์ค ๊ฐ์ฒด๋ผ๊ณ ์๊ฐ!
return <Profile user={user} />; // ์์กด์ฑ ์ฃผ์
!
}
function Profile({ user }) {
return <div>{user.name}</div>;
}
์์ฑ์ ์ฃผ์ ์ด ์ข์ ์ด์
- ๋ถ๋ณ์ฑ
- final๋ก ๋ง๋ค์ด์ ๋์ค์ ์ค์๋ก ๋ฐ๋ ์ผ ์์
- ํ์์ฑ
- ๊ผญ ํ์ํ ์ ๋ ์์ฑ์์์ ๋ฐ๋ก ๋ฐ์์ผํจ
- ํ
์คํธ ์ฌ์
- new๋ก ๋ง๋ค ๋ ๋ฐ๋ก ๋ฃ์ ์ ์์ด์ ์์ ์๋ฐ์์๋ ํ ์คํธ ๊ฐ๋ฅํจ
- ์ปดํ์ผ ํ์ ์ฒดํฌ
- ์์ผ๋ฉด ์ปดํ์ผ ์๋ฌ, NPE์ํฐ์ง
๋ค๋ฅธ ๋ฐฉ์๋ ์ง๊ณ ๋์ด๊ฐ ๋ณด์,
ํ๋ ์ฃผ์ (์ง์)
@Component
public class UserService {
@Autowired
private NotificationService notificationService;
}
์ฝ๋๋ ์งง์ง๋ง ๋จ์ ์ด ๋ง๋ค.
ํ ์คํธ ํ ๋ ์ง์ ๊ฐ์ ๋ฃ์ ์ ์์-> ํ ์คํธ ์ด๋ ค์
final์ ๋ชป ์ค๋๊น ์ค์๋ก ๋ค๋ฅธ ๊ฐ์ผ๋ก ๋ฐ๋ ์๋ ์์
์คํ๋ง ์์ด ์คํํ๋ฉด ๋์ ์๋จ
๋ฐ๋ผ์ ์ค๋ฌด์์ ํ ์คํธ๋ ์ค์ ์ฝ๋ ์ผ๋ถ์๋ง ์ ํ์ ์ผ๋ก ์ฌ์ฉํ๋ค.
Setter ์ฃผ์ (์ ํ์ ์ผ ๋ ์ฌ์ฉ)
@Component
public class UserService {
private NotificationService notificationService;
@Autowired
public void setNotificationService(NotificationService notificationService) {
this.notificationService = notificationService;
}
}
์ ํ์ ์ธ ์์กด์ฑ ์ฃผ์ ์ด ํ์ํ ๋ ์ฌ์ฉํ๋ค. -> ๊ผญ ์์ด๋ ๋๋ ๋ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ์ผ ๊ฒฝ์ฐ
๊ฐ์ฒด ์์ฑ ํ์ ๋ฐ๊ฟ ๋ผ์ฐ๊ธฐ๋ ๊ฐ๋ฅ-> ์ค๋ฌด์์๋ ๋๋ฌผ์ง๋ง ์ ์ฐํ๊ฒ ์ค๊ณํ ๋ ์ข ์ข ์ฌ์ฉ๋จ
@RequiredArgsConstructor๋ ๋ญ์?
Lombok์ด ์๋์ผ๋ก ์ด ์ฝ๋๋ฅผ ๋ง๋ค์ด์ค๋ค.
@RequiredArgsConstructor
public class MyApp {
private final MyService myService;
// ์๋์ผ๋ก ์ด ์์ฑ์๊ฐ ๋ง๋ค์ด์ง:
// public MyApp(MyService myService) { this.myService = myService; }
}
final ๋ถ์ ํ๋๋ค์ ๋ชจ์์ ์์ฑ์๋ฅผ ์๋์ผ๋ก ๋ง๋ค์ด์ค๋ค
์คํ๋ง์ด ๊ฑฐ๊ธฐ๋ค๊ฐ ์๋์ผ๋ก @Autowired๋ก ์ฃผ์ ํจ.
'Backend > ๐ฑ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Spring] filter์ค์ต๊น์ง ์งํํ ํ ์ดํดํ ๋ด์ฉ ์ ๋ฆฌ (1) | 2025.07.29 |
|---|---|
| [Spring] Bean Validation (5) | 2025.07.28 |
| [Spring]Spring Bean (2) | 2025.07.28 |
| [Spring] Layered Architecture (3) | 2025.07.26 |
| [Spring] Mapping (1) | 2025.07.23 |