Skip to content

Spring Dependency Injection#

What Is The Dependency Injection?#

  • Dependency Injection (DI) is a design pattern used to implement IoC. It allows the creation of dependent objects outside of a class and provides those objects to a class through different ways. Using DI, we move the creation and binding of the dependent objects outside of the class that depends on them.

Spring Injection Types#

  • There are 3 main types of injection with Spring.
    • Constructor Injection With XML
    • Setter Injection With XML
    • Annotation Injection.

Constructor Injection Type XML#

  • In this approach, the DI will be injected with the help of constructors. Now to set the DI of a bean as Constructor Injection Type in XML bean-configuration file, we have to use the <constructor-arg> tag in the bean-config.
applicationContext.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="englishExaminationService"
          class="com.spring.core.spring.dependency.injection.EnglishExaminationService">
    </bean>

    <bean id="englishCoach"
          class="com.spring.core.spring.dependency.injection.EnglishCoach">
        <constructor-arg ref="englishExaminationService"/>
    </bean>

</beans>
  • To use the Constructor Injection in our Spring, we should follow these steps:

    • Define the dependency interface and class
    • Create a constructor in our class for injections
    • Configure the dependency injection in Spring config file.
  • So let's take an example for the Constructor Injection Type as following steps in Spring Dependency Injection XML

Setter Injection Type XML#

  • This is the simpler of the two DI methods. In this approach, the DI will be injected with the help of setter and/or getter methods. Now to set the DI as setter dependency injection in a bean we have to use the <property> tag in the bean-config.
  • To use the Setter Injection in our Spring, we should follow these steps:

    • Create setter methods in our class for injections
    • Configure the dependency injection in Spring config file.
  • So let's take an example for the Constructor Injection Type as following steps in Spring Dependency Injection Annotation.

Inject Values From Properties File With XML#

  • From the section above, we learnt how to use Setter Injection Type for DI. In this section, we will continue to use it for injecting values from the properties files in our resources folders.

 #zoom

  • To use the Setter Injection for injecting values from the properties file in our Spring, we should follow these steps:

    • Create Properties File.
    • Load Properties File in Spring config file
    • Reference values from Properties file
  • So let's take an example for Setter Injection for injecting values from the properties file as following steps in Spring Dependency Injection XML.

Annotation Injection Type#

What Is The Spring Autowiring?#

  • Spring can automatically wire up our objects together for injecting dependency. So basically the Spring Framework will look for a class that matched a given property and it will actually match by type. The type of the class could be interface or class. Once Spring finds a match then it'll automatically inject it. Hence it's called autowired.

Autowiring Injection Types#

  • There are 3 types of autowiring injection.

    • Constructor Injection Annotation
    • Setter Injection Annotation
    • Field Injections Annotation
  • Let's take examples for these these autowiring injection types in Spring Dependency Injection Annotation.

Inject Values From Properties File By Annotation#

  • From the section above, we learnt how to use Field Injection Type With Annotation for DI. In this section, we will continue to use it for injecting values from the properties files in our resources folders.

 #zoom

  • To use the Field Injection for injecting values from the properties file in our Spring, we should follow these steps:
    • Create Properties File.
    • Load Properties File in Spring config file
    • Reference values from Properties file With Annotation

See Also#

References#