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 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 below.
Dependency#
- Like we took an example with Spring Inversion Of Control, we will need to add the dependency
spring-context
for 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
Coach
andExanminationService
and they will be implemented by 2 java classesEnglishCoach
andEnghlishExaminationService
correspondingly. Then the classEnglishCoach
will need to useExanminationService
as a dependency for it's method.
- So, firstly let's create
ExaminationService
interface andEnglishExaminationService
implementation 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
Coach
interface andEnglishCoach
implementation 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 theExaminationService
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 |
|
- As you can see, in the constructor of
EnglishCoach
class we will inject theExaminationService
and use it forgetExamination()
method.
Configure the DI in Spring config file#
- Now, let's create the XML configuration file
applicationContext.xml
in ourresources
folder 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 beanenglishExaminationService
by using tagconstructor-arg
for constructor injection type.
Testing#
- So now, let's create the main class
SpringApplication
and 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
EnglishExaminationService
because we injected it into theEnglishCoach
by 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 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 below.
Create Setter Methods In Our Class For Injections#
- Now, we will extend the example that we made before in
Constructor Injection Type
with new interface and implementation class. Let's see the diagram below.
- So let's create class
HistoryExaminationService
which will implementExaminationService
interface as below
HistoryExaminationService.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
- Now, let's create class
HistoryCoach
which will implementCoach
interface 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
HistoryCoach
we will create a setter method forExaminationService
.
Configure The DI in Spring Config File#
- Next, let's add more configuration in the
applicationContext.xml
in ourresources
folder.
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 beanhistoryExaminationService
by usingproperty
tag withname
andref
attributes. In which:name
will contain the name of setter method in thehistoryCoach
withoutset
and the first letter is lower case (examinationService).ref
will contain the bean-id of the bean that we want to inject, in this case it ishistoryExaminationService
.
Testing#
- Finally, let's update our
SpringApplication
for getting and using beanhistoryCoach
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 |
|
- Then when our application run, we will see the successful result as below. We can see the information of
historyExaminationService
which 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 Type
for DI. In this section, we will continue to use it for injecting values from theproperties
files in ourresources
folders.
- To use the
Setter Injection
for injecting values from theproperties
file 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 theHistoryCoach
andEnglishCoach
classes, we will set new attributeteamEmail
withgetter
andsetter
methods 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.properties
in our projectresources
as below.
application.properties | |
---|---|
1 2 |
|
- As you can see in the
application.properties
we have 2 variable withname
andvalue
.name
is the left part of=
value
is 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-placeholder
withlocation
attribute 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
location
incontext:property-placeholder
tag 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.properties
file intoHistoryCoach
andEnglishCoach
beans, we will useproperty
tag withname
andvalue
attributes. In which:name
will contain the name of setter method in thehistoryCoach
andEnglishCoach
withoutset
and the first letter is lower case (teamEmail).value
will contain the name of variable that we defined in theapplication.properties
file (coach.team.english.email
andcoach.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
SpringApplication
for getting and using beanEnglishCoach
andhistoryCoach
with injected values from theapplication.properties
file 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.properties
which are injected and used byEnglishCoach
andHistoryCoach
.
1 2 3 4 5 6 7 8 |
|
See Also#
- Spring Overview
- Spring Inversion Of Control
- Spring Dependency Injection
- Spring Dependency Injection Annotation