Tip:
Highlight text to annotate it
X
Hello, I'm Eli for edu4java and this is tutorial number four of Spring Framework
In this tutorial I want to continue with tutorial number 2. We had created a container
...
The container was a bit ugly because it had news.
It created the objects
Let's run the application
Main, run as...
java application
He is using; MailDeliveryService. If we want to change MailDeliveryService
for ElectronicDeliveryService
we would have to modify the code
this one...
recompilate the application
This is made by Eclipse but if we didn't have Eclipse we would have to do it
let's run it again
And here we have ElectronicDeliveryService
We also talked about using a file. In the last tutorial we said that Spring uses an xml file.
We could modify the container
so that ours also works with a file
...
The idea is that we can modify this method which is the one which creates the hashMap
with
another
implementation... this one
Here I have a problem...
I had an orthographic mistake
I now have to make the imports
I am going to use the properties of java.util
Here what I'm doing is...
When I'm going to load the hashMap I'm going to...
fetch it in a properties...
called context.properties which I'm going to define
we should create this
file .properties
Because of this FileInputStream by default it is going to fetch it to the root of our project
file...
this is the name we are going to put it; context.properties
inside this context.properties
We are going to put the information of which beans we want to create
This hash mark
means comments for a properties file. This file has a special format; name=value
name of the properties=value of the properties.
Let's write first the name of the beans
deliveryService and customerRelationOffice
There are the names of the beans. On this other side we put the name of the Classes
Below we are going to put the properties to be set. CustomerRelationOffice
has a property "deliveryService"
We separate them with a dot.
We want to put inside this property a deliveryService, which would be this object here
Let's go back to the container
Here we have this properties file. When we load this properties we start to go through them
The first thing I do is to go through them
And I ask if the property has a dot
I look for a dot
If the search for the dot is less than 0 because there is no dot it comes inside here
I create a bean with this value here
the value of the property
properties.getProperty(key)
this
and once I have this value
I make a Class.forName which loads the Class
The one I tell him
It is going to load this Class
It's like making an import
Once we load it in memory I can execute a method newInstance to the Class
It creates an Instance
I save the object created in the variable 'bean' and in components I save it with 'key'
with this key I am going to save in the hashMap an Object of this Class
The second time
it will do it for this one and for this one
As I don't save the file, I delete it and it disappears from the properties
And here
Once I have all them loaded... I have iterated looking for the ones of this type
The ones which had a dot will be in the properties
When I start iterating over them, I'm going to do it over the ones which have a dot. I only have one here
Here 'split' separates the content of 'key'. This one
it separates it by a dot
this is an escape character
Here it wasn't necessary because it is a char and to write a dot in a String we have to use an escape character
What I'm saying here is to separate the 'key' depending on where is the dot. It is going to give me to Strings
The first one
which will be this name here
this one
with this name
I'm going to look for this name in the table where these components are. Here we are going to have this object
I'm going to have this object
here in receptorBean. It will be the Bean in which will set
the property
The name of the property is the second one
and the third one will be the Bean to be inserted
The Bean to be inyected
properties.get(key). Here I'm getting what is in this side
this name
and
I ask for the component which is in the hashMap. And this is going to be the Bean to be inyected
This function is given by Apache and let's us
give a Bean a receptorBean a name for the property and another Bean and what it does is in this Bean
it looks for this property, if it there is a set method with the name of the property and it inyects this object
to use this Property we have to import it
In the last tutorial we downloaded a Spring version
which comes with other libraries.Inside these other libraries---Add external jar---We are going to find
--libraries--if we look for jakarta-commons
and two libraries; commons-logging
and
commons-beanutils
here it is
commons-beanutils
we import the logging because it is used by this one
Let's see what happens now
ctrl+shift+o to import
here it is
Let's try this new modification
let's go to Main
run as- java application
We have an error
This minicontainer
The minicontainer is the package. And here the package is container not minicontainer
This mini here is not correct.
And this one too
Run...
Another error
Here it says that it can't
instantiationException... It can't instantiate this Class
CustomerRelationOffice
It can't create it because I put a constructor
with a parameter
delivery and we are building it without parameters. I delete it
Save
and Run again
Here it is. ElectronicDeliveryService
It is getting the objects
which is going to use directly from the configuration file
context.properties.The container reads the context.properties and creates the objects. If we want to change
the ElectronicService for the Mail we just have
to change it in the configuration file
and here you can see now ElectronicDeliveryService, we run it again and we have Mail. We change
this Class
for this one without changing anything in the code
The container was configurated through a configuration file
and we just had to change it. We didn't have to compilate anything. If this was in production already
it doesn't have to be send back to development to change the code
We just have to change the configuration file and it works
This is all for this tutorial, see you in the next one!!