Skip to content

Dependency Injection

Ramesh Fadatare edited this page Jan 26, 2023 · 1 revision

Constructor Injection with Multiple Constructors

package com.spring.core.dependency;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {

    private MessageService messageService;

    private MessageService smsService;

    public MessageSender(@Qualifier("emailService") MessageService messageService){
        this.messageService = messageService;
        System.out.println("constructor based dependency injection");
    }

    @Autowired
    public MessageSender(@Qualifier("emailService") MessageService messageService,
                         MessageService smsService){
        this.messageService = messageService;
        this.smsService = smsService;
        System.out.println("constructor based dependency injection");
    }


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

Setter based injection

package com.spring.core.dependency;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {

    private MessageService messageService;

    private MessageService smsService;

//    public MessageSender(@Qualifier("emailService") MessageService messageService){
//        this.messageService = messageService;
//        System.out.println("constructor based dependency injection");
//    }
//
//    @Autowired
//    public MessageSender(@Qualifier("emailService") MessageService messageService,
//                         MessageService smsService){
//        this.messageService = messageService;
//        this.smsService = smsService;
//        System.out.println("constructor based dependency injection");
//    }


    @Autowired
    public void setMessageService(@Qualifier("emailService") MessageService messageService) {
        this.messageService = messageService;
        System.out.println("setter based dependency injection");
    }

    @Autowired
    public void setSmsService(MessageService smsService) {
        this.smsService = smsService;
    }

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

Setter based injection with multiple parameters

package com.spring.core.dependency;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {

    private MessageService messageService;

//    public MessageSender(@Qualifier("emailService") MessageService messageService){
//        this.messageService = messageService;
//        System.out.println("constructor based dependency injection");
//    }
//
//    @Autowired
//    public MessageSender(@Qualifier("emailService") MessageService messageService,
//                         MessageService smsService){
//        this.messageService = messageService;
//        this.smsService = smsService;
//        System.out.println("constructor based dependency injection");
//    }


    @Autowired
    public void setMessageService(@Qualifier("emailService") MessageService messageService) {
        this.messageService = messageService;
        System.out.println("setter based dependency injection");
    }

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

Field based injection

package com.spring.core.dependency;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {

    @Autowired
    @Qualifier("emailService")
    private MessageService messageService;

    @Autowired
    private MessageService smsService;

//    public MessageSender(@Qualifier("emailService") MessageService messageService){
//        this.messageService = messageService;
//        System.out.println("constructor based dependency injection");
//    }
//
//    @Autowired
//    public MessageSender(@Qualifier("emailService") MessageService messageService,
//                         MessageService smsService){
//        this.messageService = messageService;
//        this.smsService = smsService;
//        System.out.println("constructor based dependency injection");
//    }


//    @Autowired
//    public void setMessageService(@Qualifier("emailService") MessageService messageService) {
//        this.messageService = messageService;
//        System.out.println("setter based dependency injection");
//    }
//
//    @Autowired
//    public void setSmsService(MessageService smsService) {
//        this.smsService = smsService;
//    }

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