Dependency Injection(IOC) in Spring

The basic concept of the dependency injection (also known as Inversion of Control pattern) is that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.

i.e., Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Example:

 Lets we have two classes-Car and Engine.Car has a object of Engine.

Normal way:

There are many ways to instantiate a object. A simple and common way is with new operator.
so here Car class contain object of Engine and we have it instantiated using new operator.

Without DI

With help of Dependency Injection:
Now we outsource instantiation and supply job of instantiating to third party.Car needs object of Engine to operate but it outsources that job to some third party. The designated third party, decides the moment of instantiation and the type to use to create the instance. The dependency between class Car and class Engine is injected by a third party. Whole of this agreement involves some configuration information too. This whole process is called dependency injection.

With DI

How this whole dependency injection works,we will see it in further posts.

Benifits of Dependency Injection in Spring:

  • Ensures configuration and uses of services are separate.
  • Can switch implementations by just changing configuration.
  • Enhances Testability as mock dependencies can be injected.
  • Dependencies can be easily identified.
  • No need to read code to see what dependencies your code talks to.

Types of Dependency Injection:

    • Setter Injection:Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
    • Constructor Injection:Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.
  • Inteface Injection: In interface-based dependency injection, we will have an interface and on implementing it we will get the instance injected.

We will see above injection type in further posts.
In next post we will see spring hello world program in spring.


import_contacts

You may also like:

Related Posts

  • 24 June

    No qualifying bean of type in Spring or Spring Boot

    In this post, we will see about an exception: No qualifying bean of type. Exceptions are least expected but you might get it while working with Spring. or Spring boot. Did you get this exception: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type? If yes, let’s see different reasons for it. 💻 Awesome Tech Resources: Looking for […]

  • 17 June

    Spring XML configuration example

    In this post, we will see how to create Spring hello world XML based configuration example. Here are simple steps to create Spring XML configuration example. 1. Create a simple java maven project. 2. Maven dependency put spring and cglib maven dependency in pom.xml. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our […]

  • 15 June

    Injecting Prototype bean into a Singleton bean in Spring

    In this post, we will see how to inject prototype bean scope into Singleton Instance in Spring. This is one of the most asked Spring interview questions. Problem When you inject prototype bean to singleton bean, prototype bean still behave like a singleton bean. Let’s understand this with the help of example. 1. Create a […]

  • 15 June

    Circular dependencies in Spring

    In this post, we will discuss one of the most asked Spring interview questions. What happens if there are circular dependencies in Spring. Problem What if there are circular dependencies in Spring? For example: Class A requires an instance of Class B and Class B requires an instance of Class A. Let’s create an example […]

  • 12 September

    Spring init-method and destroy-method example

    In this post, we will see about Spring init-method and destroy-method. When bean is instantiated or destroyed , there are some operations you may need to perform, so you can use init-method and destroy-method to call this methods while bean is being created or destroyed. Lets understand it with the help of simple example: For […]

  • 27 July

    Prefix “context” for element “context:component-scan” or “context:annotation-config” is not bound : Spring error

    When you are working on spring or spring mvc application, you may encounter below error message. org.xml.sax.SAXParseException: The prefix “context” for element “context:component-scan” is not bound . or org.xml.sax.SAXParseException: The prefix “context” for element “context:annotation-config” is not bound . This issue generally occurs when you do not include context namespace. For example: Lets say you have below […]

Comments

  1. it s simple to understand. i was checking thru various websites but cudnt understand them.. hav to learn springs in a day for simple projects… thanks a lot fr making a simple way for learning concepts..

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.