05. Example for Setter based DI

23 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 see an example for second base dependency injection. In case of second wave to pendency injection, the dependencies are injected using the setter method. Already we have done one example for creating a spring application using Maven virus, we created the beam and we had one property tag using this property that we assigned values to the instance variable wherein the setter methods are getting called This is not about talking about the primitive data type, it is about the dependencies that we have in our class. Okay, let us try doing this so that you can understand it better. In my in this case, I am going to create an employee class with instance variables. I will create getter and setter method here also i'm going to use the configuration metadata XML together with this I'm going to have one other class which is going to be the address class.

The address class will come and set up an instance variable in the employee class, I want to inject this particular dependency address into employee. Let us try this out. Let me go back to eclipse. Let me create a project new spring legacy project. Set that demo, simple spring Maven, click Finish. Once application is created, you need to open palm dot XML and change the spring version.

Let me open it palm dot XML and changing the Java version to 1.8 and spring version 4.3 point one two thanks. This is done. Now let me check whether the Maven dependencies have changed. Yeah, it does change. Now let me start doing it in SRC main Java I need to create an employee class with three instance variables. Initially I'm going to start with prototypes and then I will go in for Association part so select SRC main Java, right click new class dot center dot training employee click Finish pay employee classes ready let me add three instance variables.

I am going to start with primitive data types and then I will move on to Association part. So let me add thing name, double salary and long for now I need to add getter and setter methods and generate two string assets they imply classes ready. Next, let me create the spring bean configuration file. Select the project right click new spring bean configuration file. It means you the name of setter demo dot XML And click Finish. Okay have ADD we have to add it we have to add it inside the sources.

So go and select the path, you need to have the appropriate path on the top set a demo SRC main resources, and click Finish from a configuration file is ready. Next I need to give the bean definition. When I use a bean tag, it means that I'm trying to instantiate the Java object or create an object for that particular class. Let me do that bean ID is equal to employee class is equal to fully qualified class name, combat setup dot training. Now I have created it right. A training dot employee sorry.

Okay, let me save it. The moment I say Automatically I'm getting an XML Can you see this see this ah in the project you are getting Espana ms Maven, yes this spring it means that this is a spring application. Let me just remove this bean definition and let me save this. Now, you see this is a simple Java class it is not behaving like a spring beam. Let me add the definition again. Let me say this, the moment I say automatically I am getting a symbol it means this is a spring beam.

This is just for your reference. If you are getting an error, you can just go and cross check whether you are having the shear symbol. If you don't have it, it means that you have not created the beam definition. That is the idea. Now I need to give values for the instance variables. For this I'm going to use the property tax property name equal to just to control space.

All the instance variables will be listed out here. Let me just select one by one name value equal to Now let me change the value here. Name is for value equal to its long number, so I can just give it the name of salary values double. Okay, now my employee objects is ready how many objects I have created, I'm adding only one beam definition so only one object. Next, let me create the main class. So select the package right click new class.

This class is going to have public static void Main. Within this class only we are going to instantiate the IOC container. Let me do that. So it says application context. Context is equal to new classpath XML application context. within which you need to specify the configuration metadata file name.

So in this case it is setup demo dot XML it may ended with a semicolon, then context dot get big enough. What is that the name, the dummy name employee ID, this is the employee what it is going to return it is going to return an object. I don't want the top class object I want my employee object only. So I'm down casting it here. Now let me try printing it so employed. Okay.

So, let me just go through this application again. What we actually we have done this we have created a spring legacy project we have selected simple spring Maven, then modified the string version in Palm dot XML to 4.3. point one, two and then To display POM dot XML here. So, change the spring version to 4.3 point one to change the Java version to 1.8. Then we created an employee class with three instance variables created getter and setter methods. And to think we are going to use setter based dependency injection That is why we need this getter and setter method. Then we have created this configuration metadata file which is XML in the XML file, we have created the bean definition and we are saying values for the instance variables using the property that basically we are trying to call the setter methods actually not be the IOC container has to call the setter methods based on the instance variable you have given and obtain values to these instance variables.

Now, once we have done this, we need to create the client class in the client class we are instantiating this IOC container once the IOC container steady it will create all the objects Based on the bean definition that you're given in the XML file, once all the objects are ready, they are ready basically with their ID. Now we can retrieve that particular object using the ID. In my case, it's the employee sign passing the ID of that particular object and retrieving the employee object. Let me run this application. Click OK. Now I have got the output. Can you see Yeah, so this is simple.

What actually we are done. This is a simple Java class with getter and setter methods. What if this class is having another classes instance variable? How to do that? Let me just show you. Let me select this package, right click new class.

I'm going to have addresses instance variable sorry, address a C class. In address let me create two instance variables, string city and long zip code. Let me get generate getter and setter methods and to sync first asset now in address class, let me move to employee class in employee class, I need to have address of instance variable. Remember all these are private instance variables and public getter and setter methods. I have not added private keyboard that's it. So, now what what I have to do I have to create getter and setter methods for this address, let me do that and also I need to modify this to string.

Now in the same flight class, I need to add addresses instance variable address address, all these instance variables should be private, I have not added it you have to do that basically, the right approach for a Java beans, private instance variables and public getter and setter methods. Please remember this. Now I have added the private also. Okay, now I need to create getter and setter method and generate getter. And setter methods for this address and I have to modify this to string. So I'm deleting it up let me do that.

Now this class is also ready. Let me save this, what is the next step if I want to create an employee object in a Java application basically I created by myself employee is equal to new employee he dot name he dot set phone number he dot set salary and whatever they want to set address what I have to do first I have to create the address object because it is a dependency address address is equal to new address address dot city address data sets of code I have to do next I have to say e dot set address of this address object because imply starting address as a dependency. This is one way that we will be doing our in the employee class itself while creating the address object. We will be writing it here it says address address is equal to new address When you create an object within a class itself, it means it is tightly coupled employee becomes tightly coupled to address if you are trying to create an address object inside employee instead of that, if you are going to do it outside, if you are going to delegate the work to someone they become loosely coupled, how it is achieved, it is achieved with the help of spring framework spring framework is going to create the address object and inject it to the employee object.

This concept is called dependency injection because of this concept only we can achieve loosely coupled enterprise application. Let me show you how. Let me go to the setup set a demo dot XML here what is the first step I need an address object right. So, what I have to do I have to go in first bean definition. Let me add the bean definition Have a look as of now it is a normal Java class address. It is not behaving like a spring Be only after you have added the beam definition it will be here like a spring beam let me add that okay now my beam definition is ready let me just save it the moment I save automatically will get yes in the address class.

Now it is a spring beam let me add Robin let me add values for the instance variables using the property that property name equal to city value equal to Bangalore same way for zip code okay I have added both properties for address class what is the next step this address should be injected to the employee object which we have created already. Let me show you how this can be done if you first formatted let me save this. Next, let me come to this nice property name equal to address what is the instance variable name In my case I have given address base address addresses the instance variable name, if you are given a dossier then in that scenario yes the instance variable name, if you are given a test address add add in that scenario you add the instance variable name you are not giving the class name it is just the instance variable name in the employee class.

So, for me it is address when I do control space automatically I'm getting this address Let me press Enter. Now, is it a primitive type no I cannot use value I have to go in for reference to which b this is referring to say this reference is equal to which mean at a suffering to address just now we have created I just kept control space automatically and getting it this imply been nice address. So automatically spring has to inject address Dean into Employee after creating it violators created it has created is here what is the dummy name the dummy name also I have given it as address let me just change it as my address for your understanding. Now, let me just do control space or sorry that means you with us see control space now is coming yes my address my address is t ID so, let me add it Okay, let me save this I will go back to the client program, I don't have to change anything I am getting the employee object also from the IOC container and the address also is already created and injected into employee objects.

So directly I am getting a fully configured employee object. Whatever I am telling over here is with a basic example, this is basically done in the back end when you are working in different layers. You don't have to create objects of those implementation classes automatically it will be done by spring framework going forward. We'll be doing that just understand this. When one class depends on another class, you don't have to create an object and add it to this main class, automatically it will be done by the spring framework, this concept is called as dependency injection. In our case, we are doing it with filter based dependency injection.

Let me run this application. The application is terminated. Let me just open it up. Name Roger salary, phone number, address, address, also shown over here. So this is how you add the dependencies. What if they want one more object, what I can do is I can go back to the setter demo here, I need to create another object.

I can just copy paste this Ctrl C Ctrl V, I need to give a different name because employee ID is already their name. Let me just give it a slogan. phone number, a different phone number, and salary. And what about address either you can use the same address objects are you can create another address objects over here. I'm just copying it again my address one and I will inject it here. Now I have got to address objects whichever I want I can select it and I can inject it.

Now this is done. So here we have created two employee objects and to address objects each an entrepreneur object is injected to the appropriate employee objects. We have not we have just given the instructions, who is actually creating the objects. Yeah, I will see containers we are not using new keyword anywhere. Now let me just save this. I'll come to this client dot java.

I need one more object right let me copy paste this. Let me save this now. Let me run it again. Now you can see the output. Can you see? Okay, we're gonna learn how to inject the dependencies.

Using spring without further we have taken two classes employee and address. Let me talk about one more scenario. Let me go back to the setup demo dot XML. In this case, if you see this my address object is kind of public, it can be used by all the employee objects. So, what should I do I want to create an object from within the class itself like I will just show you the equal encoding here I will just open this client what actually we have done in this cases we have created employee EMP employee is equal to new employee. This is just for understanding only and just giving this then address add call to new address.

I have got two objects. I want to inject address to employees. So you find doing it using that Java code. I will write you MP dot set address and I have given this address add this is what we usually do, I hope you can understand the equal and scenario is what we have written over here, we are not writing this code neither we are creating this object you everything is taken care by spring, when we get even weaker I mean we are getting the employee object is just fully configured the address this is fine. In this case I chose because this address object is available it can be used by all the employee objects. But what I wanted I don't want to do this I want to have my own address object.

So, how can I do I don't need this here directly I can go in for new address what is the meaning of it it is within this particular art It is only for this particular employee object, no other employee object can use this or we can understand only for this EMP this address object is available No other employee object can use this. So, we are going to see a scenario like this As of now, let me go back to the slide here, I have created a beam, I have given the property now I want to create an address object for creating an address object a given property name equal to address in the previous case he gave reference to sequel to incident giving reference, we are going to create the bean object within this property. So, if we are doing like that, in that scenario, it is called an inner beam.

The inner beam has got a scope of prototype. What is the meaning of prototype prototype means it's actually new every time a new object is getting created. There are different scopes we'll be talking about that later. prototype means a new object is getting created every time. Now let me go back to a clip here if you see whenever you are passing, EMP one dot set address of new address a new address object is getting Created EMP doodle to set address of new interests in new orders object is getting created. That is the idea.

Okay, now I'll go back to the setup demo dot XML. Now for this employee one I'm going to use interviewing let me just show you how property name phone salary they are all primitive type. So I have used them as such. Now I will remove this reference equal to my address. Okay, now I will just press enter, I have got, I have to add the details of address being how to do that. You just copy this being completely and add it within this property tag.

In this case, it will not have an ID because it's just an in in there being that we formatted Ctrl H shift control. Now you can just understand better You see, the behavior is going to be the same only, but in this scenario, this address object cannot be used by any other employee objects, it is available only for the employee one that has this address been available to employ one being what about this one this my address it can be used by me as our being because we are given it outside What is this, this is the within the property tab. So, this is called as an intervene. Now, I'll just go back to this client or Java, I will remove this code this is for your understanding only I have just shown it. Let me save it, let me run it again. There'll be no change in the output you can see the output and you see, there is no change in the output.

So, what actually we have seen in this particular session is how to work with setter based dependency injection. In case of setter based dependency injection they dependencies are injected using the setter methods for others we have taken two concepts one is to understand how to inject associations and also how to use inner beam What does Association a class having another classes instance variable rather than creating an object of the associated class within this particular class itself, we are creating it outside who is creating it the IOC container is creating this dependency and injecting it to the main class okay. So, because of this scenario, these two are not tightly coupled, but they are loosely coupled. Next we have learned about inner beam, what is an inner beam I intervene is a beam without any name it is created from within the property tab. So, for the second employee object we try to create an inner beam you address via So, this is not having any ID it is available only For this employee one beam that is so mean.

So this is false database dependency injection. In the next session, let us learn how to inject connection properties and map to a spring beam. 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.