In this session, let us discuss about IOC container spring bean configuration metadata and dependency injection. Let us first understand what is I will see, I will see is inversion of control. It is a concept which helps to decouple the components in an application. Let me explain this with an example. When you assume that you are creating a Java application, which is having employee and addresses classes, I am going in for a very basic application. Now, this implying is having addresses instance variable.
In this case employee depends on address our employee object depends on address object. As a developer when you're trying to write the code first you create the employee object. Then you create that address object. Then you add the address object to the image I object like you write employee dot set address are using a constructed you write a new employee up within round brackets you will be passing the address object that is, it is your work that you create the address object and you add that address as dependency to the employee object. In this case, the components are tightly coupled, there is a dependency as you are writing these are you are creating the objects which makes the components tightly coupled. But with spring This is reversed in general of control, you are just giving the details to spring whereas, spring will create these objects spring is going to create the employee object spring will create the address object and add the dependency to the employee object at inject the dependency to imply object.
So this concept is called as I will See inversion of control, because of this concept, the components are loosely coupled in a spring application. So, I will see is a process that the objects define their dependencies, these dependencies are injected by the IOC container, how they are dependencies are injected for this spring uses dependency injection concept. Whereas, it can be done using setter based methods, it is called a setter based dependency injection or it can be done using constructor arguments, which is called as constructor based dependency injection. How this IOC container will know about these details, it is done by these details are given in the configuration metadata file. The configuration metadata can be XML or it can be annotation or it can be Java based configuration, wherein we are going to write the configuration details in a Java class. Spring framework has two packages which are responsible for having the classes associated with IOC container.
The two packages are the dot spring framework dot b y d dot spring framework dot context being module and context module. The top level interface for the IOC container is bean factory, we know which you are having a sub sub interface called as application context, we will be working only with the application context This is a pictorial representation which shows how our spring application will be here there is a spring container which is also called as IOC container. Then, there are Java pojo classes it can be employee address, or it can be book service book do just like I was talking about. So all that classes are Java pojo classes, plain old Java objects, then I have Data metadata can be XML or annotation, our Java based configuration. So, let us discuss one day one part. So, first thing is IOC container for IOC container we have got bean factory and application context.
The application context is an interface which is the IOC container. This is responsible for instantiating that is creating the Java objects then configuring adding the dependencies to that particular object and assembling the beam. all the informations that are needed by the IOC container is given in the configuration metadata file. This is an interface. So it has got multiple implementations I have just shown to implementation class path XML application context, file system XML application context, you share configuration metadata is XML, then you need to use either of this class path or file system. dump your fear XML file is within the application then you can go in for classpath XML application context.
If your XML file is within in your system, then you will be using file system XML application context. Okay, what is the use of this? These are these two classes are responsible for instantiating the IOC container. Let me just show you the code of instantiating the IOC container first. So here I have used application context app context is equal to new class path XML application context. This is the only place that you use a new keyword.
This is used for instantiating the IOC container. So, here I have given the XML file name, this is called as configuration metadata. This is the place where you would have given all the definitions that are lettered for creating the Java object. We will talk about that later. So, just understand the sequence configuration metadata file, if we are going to use annotation as configuration metadata in that scenario, you need to use annotation config application context. So this is for the IOC container.
Let us understand about a spring beam as string B is a Java object which is instantiated, configured and managed by the IOC container. The IOC container will go and read the details from the configuration metadata because you would have given the bean definitions in the configuration metadata only as a totally can be XML or annotation or Java based configuration. If the bean is having the having few dependencies, that dependency should be injected to the bean. The dependencies are also called collaborators. Okay, references to other beings What is the meaning of that references to other objects imply or just the past Sonata objects. So, this is a collaborator or a dependency for an employee object parsing beam.
Now, let us understand about configuration metadata metadata is something which is going to give the details about your classes to the IOC container. You are just going to specify for which classes you want to create objects, what are the dependencies that particular object is having. So these kinds of informations are given in configuration metadata, you photos XML, then they are called bean definitions. You will be using XML tags to create the Java objects are you are just giving the information to the IOC container. The spring beans can be service layer objects, dmo objects, presentation layer objects are JMS queues or JMS objects. That's what I have told you use book service depends on book to work that information you need to give them the story XML files automatically TP understood by the IOC container.
And if you create the objects, let me show you the three different types XML annotation and Java base configuration. The first one is XML I have used being tagged Can you see it as being sad and I have given a dummy name and a fully qualified class name calm dot spring dot data dot employee. For this class, I'm asking the IOC container to create an object. So the IOC container will create the employee object they employ depends on address. So I am telling the IOC container create an object for address I am giving the details that is in fact you creating an address object using new keyword now the IOC container is going to create the object. Now I have created an address object addresses God city and status instance variable explicitly and giving values over here.
This is just for example purpose. Basically, we are Talking about the dependencies in terms of classes, if you see him, he depends on address. Okay, it is not a primitive type. So don't bother about the primitive type we are talking about the dependencies in terms of classes. So, here an address object is created and it is injected to the employee object who is doing this IOC container by reading this configuration metadata file. This is for if someone if you are going in for annotation, in that case about a class you are going to add the annotation at the rate component.
So here I am asking the IOC container to create a student object that is called as a spring bean student bean, then the student has address as a dependency. So what I'm telling the IOC container you better create an address object and indirect object to this student object. So an address been will be created and it will be ended To student means, for this, this address class also should be annotated with at the rate component This is annotation based configuration. Next Java based configuration wherein you are going to create a separate class a Java class first you will annotate the classes at the RAID configuration and then explicitly you are creating the objects can you see I am creating an employee object I am creating an address object now address will be injected the employee object but this code I have written how automatically it will be charged.
So, for that I'm using the red bean annotation. The IOC container will come to this particular class first, it'll identify all the methods that are annotated with that the red bean and create the objects. So, you are asking the IOC container only to create all the objects and inject the dependencies to those objects. You are not supposed to say them as objects. They are called a spring beans Let me now go back to the pictorial representation. So, this is a pictorial representation, I hope you can relate now, you have got few Java classes you there is an IOC container you are returning metadata file we can be XML or annotation or Java based configuration.
Now, the IOC container is going to read the configuration metadata and create the beans depending upon the bean definitions that is given over there. Now the application is ready to be deployed. you inject the Java classes, you inject the metadata to the IOC container, the IOC container thus re creating the string beans which are ready to be used by the application. Okay, so this is C represent a pictorial representation. Now again, I'm going in fact, the two different types of IOC containers. One is the bean factory.
The other one is application context. Bean factory is the top level interface application context the C sub interface In case of bean factory it is instantiated nice lazy Lee What is the meaning of it when you use new keyword that is bean factory equal to new XML bean factory, any of you are not going to use to spark we will be using application context. In this case, the factory alone is loaded the IOC container is loaded it will not create the objects only when you try to retrieve the object using get bean that object will be created the employee object will be created. So, being stopped instantiated lazily only when you ask for it, it will be given to you in case of application context. It has got few more additional features. It has it you can just have a look over here.
It's it supports HTTP message resources, so many other things. In this case, the beans are instantiated yearly, what is the meaning of it? The moment you create the IOC containers, all jobs spring beans will be created by reading the metadata file configuration metadata Alright, let me go to the instantiate and the moment you give this line, the IOC container will go and read this particular file and create all the spring beans. Then when you are trying to get the bean by calling get bean method, that particular bean will be given to decline for servicing the application. So, this is the difference between bean factory and application context, IOC container, spring bean and configuration metadata. Now, let us talk about dependency injection.
What is dependency injection? dependency injection is a process in which dog objects define the dependencies and the dependencies are injected to the objects either using constructor arguments are using setter based methods. So there are two types of dependency injection one a setter based dependency injection, the other one is constructor based dependency injection virus in case of Saturday's dependency injection that the currencies are injected using the setter methods. In case of constructor based dependency injection, that dependencies are injected using the constructor arguments. We will be learning about these two types of dependency injection in the upcoming sessions. So this is about the IOC container, spring beam configuration metadata and dependency injection.
These are the four main things you need to know if you want to work with a spring application. IOC container can be application context or bean factory, it is responsible for instantiating. configuring and assembling the beans is doing all these with the help of configuration metadata. Configuration metadata can be either XML or annotation or Java based configuration. All the details that are needed by the IOC container is given in the configuration metadata. You are just giving the details about the objects that has to be Created by the IOC container I am the dependencies that has to be injected to these objects.
This is the second annex the spring beans what is the spring beans it is a simple Java object which is created by the IOC container and it only IOC container is only going to manage the spring beans. Next is dependency injection. In dependency injection is a process in which the dependencies are injected to the objects either by using setter based dependency injection are they using constructor base in dependency injection. So, this is only for the theory part going forward. We will be seeing only examples of how to use setter based contract based dependency injection and few other concepts. Thank you