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
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
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.