• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Real world example of Dependency Injection

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

Any one please help to understand Dependency Injection in spring with real world example ?

I have seen few code example but it would be great help if any one put a non technical example , if possible .

Thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know what exactly you expect when you say "non-technical example".

Do you understand what dependency injection is and why it is useful?

Almost all non-trivial programs will consist of a number of components that work together. A component might need to use one or more other components to do its job - so the component depends on those other components.

For example, you might have a login screen in your application that uses a UserInfoService to get information about users, and to check if the username and password that someone enters are valid. The login page needs to get a reference to the UserInfoService to call its methods. The simplest way it could do this is by just creating a new UserInfoService object. But this has a number of disadvantages.

First of all, suppose that UserInfoService is actually an interface (not a class) and that there are several possible implementations of this interface. There might be an LDAPUserInfoService, which gets information about users from an LDAP directory, and there might be a DatabaseUserInfoService which gets the same information from a database table. How is the login screen going to choose which implementation to use? You could ofcourse create an instance of a particular implementation by simply doing: UserInfoService service = new LDAPUserInfoService();, but if you then later decide that you need the DatabaseUserInfoService, you would have to change the source code of the login page.

Also, suppose that for testing the login page you would want to use a mock UserInfoService. So, when testing, you would want to use a MockUserInfoService instead of a real service.

Dependency injection makes it easy to decouple the login page from a specific implementation of UserInfoService. You write a configuration file (for example a Spring XML configuration file) in which you define which implementation of UserInfoService should be used, and then, when you run the program, you let Spring take care of creating the actual UserInfoService and pass it to the login page using dependency injection. When you at some moment need to use a different implementation of UserInfoService, you only have to change the Spring configuration file, and you don't need to change the source code and recompile the program.
 
pari Nagda
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:I don't know what exactly you expect when you say "non-technical example".

Do you understand what dependency injection is and why it is useful?

Almost all non-trivial programs will consist of a number of components that work together. A component might need to use one or more other components to do its job - so the component depends on those other components.

For example, you might have a login screen in your application that uses a UserInfoService to get information about users, and to check if the username and password that someone enters are valid. The login page needs to get a reference to the UserInfoService to call its methods. The simplest way it could do this is by just creating a new UserInfoService object. But this has a number of disadvantages.

First of all, suppose that UserInfoService is actually an interface (not a class) and that there are several possible implementations of this interface. There might be an LDAPUserInfoService, which gets information about users from an LDAP directory, and there might be a DatabaseUserInfoService which gets the same information from a database table. How is the login screen going to choose which implementation to use? You could ofcourse create an instance of a particular implementation by simply doing: UserInfoService service = new LDAPUserInfoService();, but if you then later decide that you need the DatabaseUserInfoService, you would have to change the source code of the login page.

Also, suppose that for testing the login page you would want to use a mock UserInfoService. So, when testing, you would want to use a MockUserInfoService instead of a real service.

Dependency injection makes it easy to decouple the login page from a specific implementation of UserInfoService. You write a configuration file (for example a Spring XML configuration file) in which you define which implementation of UserInfoService should be used, and then, when you run the program, you let Spring take care of creating the actual UserInfoService and pass it to the login page using dependency injection. When you at some moment need to use a different implementation of UserInfoService, you only have to change the Spring configuration file, and you don't need to change the source code and recompile the program.






Hi,

This is really helpful information you shared.

Thanks a lot .

 
reply
    Bookmark Topic Watch Topic
  • New Topic