Announcement Announcement Module
Collapse
No announcement yet.
What is the difference between the depencylookup and dependency injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is the difference between the depencylookup and dependency injection

    Hi ,
    what is the difference between dependencylookup and dependency injection.please give me an example also.
    Regards,
    Sameer.

  • #2
    In a nutshell, dependency lookup is when an object finds a way to create/instantiate/lookup its own dependencies (such as a private property).

    Dependency injection is when the same object is provided the dependency by some DI framework.

    Dependency lookup:

    Code:
    private String greeting;
    
    public void printGreeting() {
        greeting = GreetingManager.getEnglishGreeting();
        System.out.println(greeting);
    }
    Dependency injection:

    Imagine that there is a DI framework, such as Spring, which defines the greeting bean and injects it by calling a setGreeting(String greeting) method.

    Code:
    private String greeting;
    
    public void printGreeting() {
        System.out.println(greeting);
    }
    The big difference is the dependency injected object doesn't worry about how to get a greeting. It leaves that up to the DI framework. The DI framework is then free to swap out different versions of the greeting for different enviroments, or testing, etc.

    Comment

    Working...
    X