Results 1 to 2 of 2

Thread: What is the difference between the depencylookup and dependency injection

  1. #1
    Join Date
    Oct 2009
    Posts
    10

    Default 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. #2

    Default

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •