[6/20 TIL] SPRING(IOC, DI, ApplicationContext)

yumyeonghanΒ·2023λ…„ 6μ›” 22일
0

πŸƒν”„λ‘œκ·Έλž˜λ¨ΈμŠ€ λ°±μ—”λ“œ λ°λΈŒμ½”μŠ€ 4κΈ° κ΅μœ‘κ³Όμ •μ„ λ“£κ³  μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.πŸƒ

Inversion of Control

  • κΈ°μ‘΄μ—λŠ” λͺ¨λ“  μ’…λ₯˜μ˜ μž‘μ—…μ„ μ‚¬μš©ν•˜λŠ” μͺ½μ—μ„œ μ œμ–΄λ₯Ό ν•˜λŠ” κ΅¬μ‘°μ˜€λ‹€λ©΄, IOC(μ œμ–΄μ˜ μ—­μ „)λž€ ν”„λ ˆμž„μ›Œν¬μ— μ˜ν•΄ μ œμ–΄κ°€ λ˜λŠ” ꡬ쑰
  • 즉, κ°μ²΄λŠ” μžμ‹ μ΄ μ‚¬μš©ν•  객체λ₯Ό 슀슀둜 μ„ νƒν•˜κ³  μƒμ„±ν•˜μ§€ μ•ŠμŒ
  • μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ½”λ“œμ˜ 흐름을 μ‚¬μš©μžκ°€ 직접 μ œμ–΄ν•˜λŠ” '라이브러리 μ‚¬μš©'κ³Ό λ‹€λ₯Έ ꡬ쑰

μ˜μ‘΄μ„±

  • μ–΄λ–€ 객체가 ν˜‘λ ₯ν•˜κΈ° μœ„ν•΄ λ‹€λ₯Έ 객체λ₯Ό ν•„μš”λ‘œ ν•  λ•Œ 두 객체 μ‚¬μ΄μ˜ μ˜μ‘΄μ„±μ΄ 쑴재
  • μ˜μ‘΄μ„±μ€ μ‹€ν–‰ μ‹œμ κ³Ό κ΅¬ν˜„ μ‹œμ μ— μ„œλ‘œ λ‹€λ₯Έ 의미λ₯Ό 가짐
    • μ»΄νŒŒμΌνƒ€μž„ μ˜μ‘΄μ„±: μ½”λ“œλ₯Ό μž‘μ„±ν•˜λŠ” μ‹œμ μ—μ„œ λ°œμƒν•˜λŠ” 클래슀 μ‚¬μ΄μ˜ μ˜μ‘΄μ„±
    • λŸ°νƒ€μž„ μ˜μ‘΄μ„±: μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ΄ μ‹€ν–‰λ˜λŠ” μ‹œμ μ˜ 객체 μ‚¬μ΄μ˜ μ˜μ‘΄μ„±

결합도

  • ν•˜λ‚˜μ˜ 객체가 변경이 일어날 λ•Œμ— 관계λ₯Ό λ§Ίκ³  μžˆλŠ” λ‹€λ₯Έ κ°μ²΄μ—κ²Œ λ³€ν™”λ₯Ό μš”κ΅¬ν•˜λŠ” 정도
  • μ–΄λ–€ 두 μš”μ†Œ 사이에 μ‘΄μž¬ν•˜λŠ” μ˜μ‘΄μ„±μ΄ λ°”λžŒμ§ν•  경우 λŠμŠ¨ν•œ(μ•½ν•œ) 결합도, λ°”λžŒμ§ν•˜μ§€ μ•Šμ„ 경우 λ‹¨λ‹¨ν•œ(κ°•ν•œ) 결합도

Dependency Injection

  • 객체가 직접 μ˜μ‘΄ν•˜λŠ” 객체λ₯Ό μƒμ„±ν•˜κ±°λ‚˜ κ΄€λ¦¬ν•˜μ§€ μ•Šκ³ , μ™ΈλΆ€μ—μ„œ μ˜μ‘΄ν•˜λŠ” 객체λ₯Ό μ£Όμž…λ°›λŠ” 방식
  • μƒμ„±μžλ₯Ό ν†΅ν•΄μ„œ 객체λ₯Ό μ£Όμž…λ°›λŠ” νŒ¨ν„΄μ„ μƒμ„±μž μ£Όμž… 방식이라 ν•˜κ³ , μŠ€ν”„λ§μ—μ„œ ꢌμž₯ν•˜λŠ” 방식

ApplicationContext

  • IoC μ»¨ν…Œμ΄λ„ˆλŠ” 객체에 λŒ€ν•œ 생성과 쑰합이 κ°€λŠ₯ν•˜κ²Œν•˜λŠ” ν”„λ ˆμž„μ›Œν¬
  • μŠ€ν”„λ§μ—μ„œλŠ” 이런 IoCμ»¨ν…Œμ΄λ„ˆλ₯Ό ApplicationContext μΈν„°νŽ˜μ΄μŠ€λ‘œ 제곡
  • BeanFactoryλ₯Ό μƒμ†ν•˜κ³ , 객체에 λŒ€ν•œ 생성, μ‘°ν•©, μ˜μ‘΄κ΄€κ³„μ„€μ • 등을 μ œμ–΄ν•˜λŠ” IoC κΈ°λ³ΈκΈ°λŠ₯을 BeanFactoryκ°€ λ‹΄λ‹Ή
  • Bean은 IoC Container에 μ˜ν•΄ κ΄€λ¦¬λ˜λŠ” 객체


κ·Έλ¦Ό 좜처 λ°”λ‘œκ°€κΈ°

μ˜ˆμ‹œ μ½”λ“œ

public interface MessageService {
    void sendMessage(String message);
}

public class EmailService implements MessageService {
    public void sendMessage(String message) {
        // 이메일을 톡해 λ©”μ‹œμ§€ 전솑
        System.out.println("Sending email: " + message);
    }
}

public class SMSService implements MessageService {
    public void sendMessage(String message) {
        // SMSλ₯Ό 톡해 λ©”μ‹œμ§€ 전솑
        System.out.println("Sending SMS: " + message);
    }
}

public class NotificationService {
    private MessageService messageService;

    // DIλ₯Ό 톡해 MessageService μ˜μ‘΄μ„± μ£Όμž…
    public NotificationService(MessageService messageService) {
        this.messageService = messageService;
    }

    public void sendNotification(String message) {
        messageService.sendMessage(message);
    }
}

//AppConfig μ„€μ • 클래슀 등둝
@Configuration
public class AppConfig {
    @Bean
    public MessageService emailService() {
        return new EmailService();
    }

    @Bean
    public MessageService smsService() {
        return new SMSService();
    }

    @Bean
    public NotificationService notificationService() {
        return new NotificationService(emailService());
    }
}

public class Main {
    public static void main(String[] args) {
      	// ApplicationContext 생성
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        // NotificationService 빈 κ°€μ Έμ˜€κΈ°
        NotificationService notificationService = context.getBean(NotificationService.class);

        // μ˜μ‘΄μ„± μ£Όμž…λœ λ©”μ‹œμ§€ μ„œλΉ„μŠ€λ₯Ό μ‚¬μš©ν•˜μ—¬ μ•Œλ¦Ό 전솑
        notificationService.sendNotification("Hello, World!");
}
profile
μ›Ή κ°œλ°œμ— 관심 μžˆμŠ΅λ‹ˆλ‹€.

0개의 λŒ“κΈ€