10. Autowiring

16 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 learn about auto wiring. Auto wiring is a concept wherein spring will automatically wire the dependencies to a Java class. After wiring automatically wire the dependencies. already we have seen one example where an employee class was having addresses instance variable. A web address instance we use are the red auto wired address objects are created by the IOC container and automatically injected to the employee object. This concept is auto wiring but you don't have to use the property tax or the constructor attached to constructor arguments are to inject the dependencies automatically it will happen.

We are not going to see I would have done an example we are going to learn it with annotation for annotation we have seen it it is accurate auto wired annotation. This can be added up or the instance variable above. Property above the setter method or observer constructor. Once you have added it automatically spring will inject the dependency there is an issue in this in when we were working with the employee class, we had an employee class with addresses instance variable, this address is not having any sub classes, neither it is an interface. So when you give out the rate auto wired, one instance of address is created and it is injected to the employee. Suppose say I'm having a scenario like this, I'm having an employee, wherein I am having a subclass a sorry, I'm having an instance variable, cause the instance variable under employee this course is a superclass it is having multiple sub classes.

ABC. When you use Arthur it auto wired up all the scores. As I told you, it can be a superclass or it can be an interface. It is adding multiple classes which implemented when you add the rate automatically. For this instance variable, the IOC container, it's a confusion. You're asking to inject only one value, but there are three different beings of the same type.

A subtype could be subtype courses of type course diversey container gets a confusion. So you need to say which particular objects should be injected. For that we will be using our thread qualifier which is qualified to be injected into the employee object. Okay, rather than explaining it here, we will understand it a little more better. I will explain this with the proper example. I have an example over here, I have a shape.

This is an interface and I'm adding two subclasses of shapes. One is rectangles, the other one is circle. Now I have another class, shape factory. This shape factory is having shared for instance variable. If you want So, okay. So, for instance variable and the shirt is having an area method, which is implemented properly in rectangles and circles.

So, this is the hierarchy I hope you can understand one interface to implementation classes implementing area method that is wonder class shape factory which is taking shape for instance variable. Now, if you want to call the area method what you have to do you have to explicitly create an object or circle a rectangle. So, what you will write check shape is equal to new rectangle then within any method we will try to call this area directly. That's what you do right. But with the help of spring, we will be ingesting the shape instance. When I say the shape instance it is a class which implements the shape can be rectangle or it can be circle.

Now, let us try to So, I will go back to eclipse, a spring Maven project and I have given the name of auto wiring demo. I have added all the relevant classes here. Let me start with Chase shared With an interface with one method calc area, and I'm having three implementing classes, rectangle, square and circle, I have annotated all the three classes with an array component meaning to say that they should be identified as a spring me spring beam as the IOC container should create an object for this the method is implemented properly. So, this is rectangle, this is circle and this is crap. Next, let me come to the shape factory, the shape factory is having shape as instance variable as a shape to be injected to the shape factory, either I have to go into a database dependency injection are constructed with dependency injection.

Now let me go in first database dependency injection. So I have to add getter and setter methods. Go to soap, generate getter and setter method, select all click OK. This is done. I'm in shape factory, I'm having one more method get area within which I'm trying to call the method of shape. What are some methods we have got calc area and I'm passing x&y from BARROWMAN getting this x and y from this parameter okay.

So, this is how to create an object of shapes operators very simple you can write a factory factory called the new shape factory ah you annotated with add the red component now what actually are they telling the IOC container to create an object for the shape factory also, right now, let me come to this our test test auto class. This is the main class which is having public static void Main. I have instantiated the IOC container here so I'm using annotation config application context. What is the package name? Let me just give it as comm dot auto where you go and do the call. I mean, either you can go in for the base packages calm or you can give the child I mean sub package.

I have gone from combat auto via what is the next step I need to retrieve the shape factory objects Shape factory factory go to context dot get big enough that we do that that is a name that I have to pass within the double quotes the ID of the shape factory. This is two words shape factory so what will be the name? It will be in camel case. Yes lowercase and uppercase. Let me add that here it says yes lowercase what is the method that I have to call factory dot get area I need to pass values 10 comma 20 the method is wide so I don't have to call it within print statement Okay, this is that what is the flow when you run this application, the IOC container will go in search of this comm dot auto a package it will create objects for all the classes that are annotated with at the rate component.

So all the objects are ready now what about this shape? I need to get this shape, if I am doing it as a normal Java application in that scenario, I have to write shape is equal to New Circle our shape equal to new rectangle and then only I can call this method, if I am trying to run this application as such, what will happen? What is the value of shape null on a null reference you are trying to call a method you will get a null pointer exception. Let me show you that. I'll just go to test start over here. Let me run this application no pointer exception.

Can you see this is calculate area is like the printer and statement null pointer exception. So, if it is a normal Java application what basically you will do shape equal to New Circle you will get like this. And now if you try running this application, you will get the output of circle 31.4 it is for circle right so, let me just show it to you. See source Ok and save it. Now let me run it again, can you see calculating area circle and it is giving the output. This is okay fine.

But what we are using is a spring application. So I'll just go to the Save shape factory, I don't want to create the objects all by myself, the object is already created by the IOC container. It's a singleton. The moment you call application context, all the objects are created and kept ready, whereas asking for it, it will be given. So now what I have to do, I need to go in for a thread auto wired annotation. When the use address auto wired annotation, if a share if the shape is a simple class, without any inheritance hierarchy, the objects of shape will be created and it will be given to a factory, but he or she is an interface which is having multiple implementation classes.

So this is going to give me an exception. Let me try running this application and I will show you though to start away Can you see the exception no qualifying being of type combat auto training that shape available I am expecting only one object, but how many you are adding three circles rectangles triangles sorry square, what are they they are just the ID you are not given any different name because you have use at the rate component A lot they are just the name the class name is nothing but this B name in lowercase circle rectangle and square. So, what I have to do I have to go in for address quantifier annotation at the rate qualifier which object you want, you have to specify over here I want rectangles. Now, let me save this. Now let me run the application for rectangle I will check whether we are having a printf statement.

Yeah, it is there. Now, I'll go to test dot o f. Let me run the application. You can see the output calculating area rectangle 200 Now what if I want to go in for suck i mean scratch, I can give it over here qualifier square save this. Now run the application again you say calculating area square 100. So, what actually you are doing is you are class having another classes instance variable. And if the instance is having an inheritance hierarchy in that scenario together with at the rate auto wired annotation, you need to use a direct quantifier also a great qualifier pass on the string B name should I have given square What if they want to give a different name?

I'm just going to have it yes, you know what I have to do if I'm trying to randomly run this application what I will get I will get an error because in shape factory if you see I'm having squared there is no bean with the name square. There is a bean the name only Yes, yes You circle rectangle and shape factory okay let me run it you can add a lager and check it out then you will see the output now being of the shape available expected at least one way in which qualifies as auto a candidate okay no okay. Let me just show you the exception unsatisfied dependency injection shape factory depends on shapes, but shape is not available that is where you are just getting this exception Okay. Now, instead of this what I can do, if I give you a skew and save this, let me run it this is okay fine, this is working one more thing also I will explain you I will just come and quantify it I will have interrupt Sq let me just have it a rectangle that does a normal stuff and saving it.

Now, I will go to the square class in this class we are giving it us Sq now I will remove this I will have the name of shape. So what are the three beans that are Under shapes, circles, rectangles and shapes, because you are given the name of shape, we have commented address qualifiers, you know what will happen if you comment at the rate qualifier it is going to give you an exception saying that I'm expecting one but you are having three. But now what is the instance variable name shaped So, when you do our great auto wired annotation, the IOC container will go in search of an instance it is of type shape, shape as an interface, you are adding three implementation classes. First it is check if there is any B with the name shape is trying to do auto wiring by name. Often it is doing auto wiring by name already there is one class class with the same name.

So what it is going to do it will automatically wire this class only. So let me save this now. I'll go to start a wire. Let me run this application. Now we can see it's not giving me any error it is giving the output Again I will go to the square class I will just comment this part alone. What is the cloud What is the meaning of the square?

Let me run it I have to get an error because I have commented at the rate quality phase and you see no qualifying mean expected single matching been but found three circles rectangle and square. So, here what actually we have learned is we are doing auto wiring by name there are three things, auto wiring by name, auto wiring by type and auto wiring by constructor. What actually we are seeing now is auto wiring based by name, author wedding by type means what? Yes, I have commented it so fast. This application is trying to optimize by name it will go in shebeen which is having the same name as that of the instance variable. You put a step automatically it will wire if it is not there, it will try to do auto wiring by type.

When it is doing auto wiring by type. You just come into the shape. You're not having only one but you are having many Free Choice giving me an exception in that case what I have to do I have to go in for qualifier and I use qualifier now this will work fine. Now, let me run this see, you're getting the output. So, if you want to do auto wiring by name, then the instance variable name under spring bean name should be same that is what is called as auto wiring by name. What is auto wiring by type auto wiring by typing if there are any sub classes under that super under that class, in case in this case of the shape, if there are any subclasses automatically you need to wire it using qualifier annotation.

Okay, so we can find the highest priority auto wiring by name or auto wiring by type that is now I'm going to use qualifier also. I'm going to give the name for squarish shape also. Which one is clarity? Let us check this out. Let me run application stand what actually is happening first it is doing auto wiring by name. So, here actually, when it is coming to this particular shape factory shape shape already there is a matching beam.

So, it is entering into this class, but what you are doing you are overriding it using at the rate qualifier annotation. So, now, it has become shape shape is equal to new rectangle initially it was shape shape equal to new square, but the moment it is entering into this class and introducing either a quantifier annotation, now, it has become shade shape equal to a new rectangle okay. So, auto wiring bytes I mean qualifiers are in the highest priority. So, what actually we have learned how to use auto wiring when you are having a class assigning another class of instance variables. If this class is not having any inheritance hierarchy, simply use at the rate auto wired annotation data center. You don't have to do any other stuff.

But if the class is having an issue inheritance hierarchy. In that scenario, you need to choose the child class or the sub class by using a direct qualifier annotation. You need to pass in the name of the beam that has to be auto wired. In my case I want to auto add a rectangle to the shape factory. So this is called this. So this is auto wiring.

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.