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