Spring Dependency Injection XML#
Constructor Injection Type With 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 | |
-
To use the
Constructor Injectionin 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 below.
Dependency#
- Like we took an example with Spring Inversion Of Control, we will need to add the dependency
spring-contextfor creating the Spring Container and configuring Constructor Injection Type.
| pom.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
Define the dependency interface and class#
- So now, let's create 2 interfaces
CoachandExanminationServiceand they will be implemented by 2 java classesEnglishCoachandEnghlishExaminationServicecorrespondingly. Then the classEnglishCoachwill need to useExanminationServiceas a dependency for it's method.
- So, firstly let's create
ExaminationServiceinterface andEnglishExaminationServiceimplementation class as below.
| ExaminationService.java | |
|---|---|
1 2 3 4 5 6 7 | |
| EnglishExaminationService.java | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
- Then let's create the
Coachinterface andEnglishCoachimplementation class as below.
| Coach.java | |
|---|---|
1 2 3 4 5 6 7 8 9 | |
| EnglishCoach.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Create a constructor in our class for injections#
- Now, in the
EnglishCoach, let's create a constructor and inject theExaminationServiceas below.
| EnglishCoach.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
- As you can see, in the constructor of
EnglishCoachclass we will inject theExaminationServiceand use it forgetExamination()method.
Configure the DI in Spring config file#
- Now, let's create the XML configuration file
applicationContext.xmlin ourresourcesfolder and put the configuration as below.
| applicationContext.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
- As you can see, for the bean
englishCoach, we will inject the beanenglishExaminationServiceby using tagconstructor-argfor constructor injection type.
Testing#
- So now, let's create the main class
SpringApplicationand then we will create the container with xml configuration file and get beans for using.
| SpringApplication.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
- So when we run our application, we expect that we will get the information from the
EnglishExaminationServicebecause we injected it into theEnglishCoachby using constructor type in xml configuration. - The successful result when run the application is show as below.
1 2 3 4 | |
Setter Injection Type With 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 Injectionin 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 below.
Create Setter Methods In Our Class For Injections#
- Now, we will extend the example that we made before in
Constructor Injection Typewith new interface and implementation class. Let's see the diagram below.
- So let's create class
HistoryExaminationServicewhich will implementExaminationServiceinterface as below
| HistoryExaminationService.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
- Now, let's create class
HistoryCoachwhich will implementCoachinterface as below.
| HistoryCoach.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
- As you can see, in the
HistoryCoachwe will create a setter method forExaminationService.
Configure The DI in Spring Config File#
- Next, let's add more configuration in the
applicationContext.xmlin ourresourcesfolder.
| applicationContext.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
- As you can see, for the bean
historyCoach, we will inject the beanhistoryExaminationServiceby usingpropertytag withnameandrefattributes. In which:namewill contain the name of setter method in thehistoryCoachwithoutsetand the first letter is lower case (examinationService).refwill contain the bean-id of the bean that we want to inject, in this case it ishistoryExaminationService.
Testing#
- Finally, let's update our
SpringApplicationfor getting and using beanhistoryCoachas below.
| SpringApplication.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
- Then when our application run, we will see the successful result as below. We can see the information of
historyExaminationServicewhich is injected and used byhistoryCoach.
1 2 3 4 5 6 | |
Inject Values From Properties File With XML#
- From the section above, we learnt how to use
Setter Injection Typefor DI. In this section, we will continue to use it for injecting values from thepropertiesfiles in ourresourcesfolders.
- To use the
Setter Injectionfor injecting values from thepropertiesfile in our Spring, we should follow these steps:- Create Properties File.
- Load Properties File in Spring config file
- Reference values from Properties file
Prepare#
- So, Let's continue with the example that we did from the last section
Setter Injection Type. In theHistoryCoachandEnglishCoachclasses, we will set new attributeteamEmailwithgetterandsettermethods for them as below.
| EnglishCoach.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | |
| HistoryCoach.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | |
Create Properties File#
- Let's create a properties file with name
application.propertiesin our projectresourcesas below.
| application.properties | |
|---|---|
1 2 | |
- As you can see in the
application.propertieswe have 2 variable withnameandvalue.nameis the left part of=valueis the right part of=
Load Properties File In Spring Config File#
- To load variables from
application.properties, we just need to add the tagcontext:property-placeholderwithlocationattribute as below intoapplicationContext.xml.
| applicationContext.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
- In which, the value of attribute
locationincontext:property-placeholdertag should be start withclasspath:then the properties's file name will be put after.
Reference Values From Properties File#
- Now, to reference values from the
application.propertiesfile intoHistoryCoachandEnglishCoachbeans, we will usepropertytag withnameandvalueattributes. In which:namewill contain the name of setter method in thehistoryCoachandEnglishCoachwithoutsetand the first letter is lower case (teamEmail).valuewill contain the name of variable that we defined in theapplication.propertiesfile (coach.team.english.emailandcoach.team.history.email) and these names have to be put in the syntax${<variable name>}. Ex:${coach.team.english.email}. With this syntax Spring will load the value of that variable for you directly.
| applicationContext.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | |
Testing#
- Finally, let's update our
SpringApplicationfor getting and using beanEnglishCoachandhistoryCoachwith injected values from theapplication.propertiesfile as below.
| SpringApplication.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
- Then when our application run, we will see the successful result as below. We can see the values in the
application.propertieswhich are injected and used byEnglishCoachandHistoryCoach.
1 2 3 4 5 6 7 8 | |
See Also#
- Spring Overview
- Spring Inversion Of Control
- Spring Dependency Injection
- Spring Dependency Injection Annotation


