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
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
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.