03. IOC Container, spring bean

15 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
Already have an account? Log In

Transcript

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

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.