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
€66.27
List Price:  €94.67
You save:  €28.40
£55.39
List Price:  £79.13
You save:  £23.74
CA$98.13
List Price:  CA$140.19
You save:  CA$42.06
A$107.73
List Price:  A$153.90
You save:  A$46.17
S$93.76
List Price:  S$133.95
You save:  S$40.19
HK$544.73
List Price:  HK$778.23
You save:  HK$233.49
CHF 61.80
List Price:  CHF 88.29
You save:  CHF 26.48
NOK kr772.72
List Price:  NOK kr1,103.93
You save:  NOK kr331.21
DKK kr494.34
List Price:  DKK kr706.23
You save:  DKK kr211.89
NZ$118.90
List Price:  NZ$169.87
You save:  NZ$50.96
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,323.66
List Price:  ৳11,891.45
You save:  ৳3,567.79
₹5,909.11
List Price:  ₹8,441.95
You save:  ₹2,532.83
RM313.06
List Price:  RM447.25
You save:  RM134.19
₦116,728.62
List Price:  ₦166,762.32
You save:  ₦50,033.70
₨19,349.93
List Price:  ₨27,643.94
You save:  ₨8,294.01
฿2,418.69
List Price:  ฿3,455.42
You save:  ฿1,036.73
₺2,421.05
List Price:  ₺3,458.79
You save:  ₺1,037.74
B$406.02
List Price:  B$580.06
You save:  B$174.03
R1,265.11
List Price:  R1,807.38
You save:  R542.27
Лв129.77
List Price:  Лв185.40
You save:  Лв55.62
₩97,597.80
List Price:  ₩139,431.41
You save:  ₩41,833.60
₪261.71
List Price:  ₪373.90
You save:  ₪112.18
₱4,123.18
List Price:  ₱5,890.51
You save:  ₱1,767.32
¥10,762.63
List Price:  ¥15,375.85
You save:  ¥4,613.21
MX$1,416.38
List Price:  MX$2,023.49
You save:  MX$607.10
QR254.03
List Price:  QR362.91
You save:  QR108.88
P950.29
List Price:  P1,357.62
You save:  P407.32
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,467.03
List Price:  E£4,953.11
You save:  E£1,486.08
ብር8,622.47
List Price:  ብር12,318.34
You save:  ብር3,695.87
Kz63,865.87
List Price:  Kz91,240.87
You save:  Kz27,375
CLP$68,034.44
List Price:  CLP$97,196.23
You save:  CLP$29,161.78
CN¥506.75
List Price:  CN¥723.96
You save:  CN¥217.21
RD$4,196.84
List Price:  RD$5,995.75
You save:  RD$1,798.90
DA9,336.57
List Price:  DA13,338.53
You save:  DA4,001.96
FJ$158.85
List Price:  FJ$226.94
You save:  FJ$68.09
Q538.15
List Price:  Q768.81
You save:  Q230.66
GY$14,572.68
List Price:  GY$20,819.01
You save:  GY$6,246.32
ISK kr9,629.22
List Price:  ISK kr13,756.62
You save:  ISK kr4,127.40
DH697.40
List Price:  DH996.34
You save:  DH298.93
L1,272.08
List Price:  L1,817.34
You save:  L545.25
ден4,077.86
List Price:  ден5,825.76
You save:  ден1,747.90
MOP$558.50
List Price:  MOP$797.89
You save:  MOP$239.39
N$1,261.49
List Price:  N$1,802.21
You save:  N$540.71
C$2,563.56
List Price:  C$3,662.39
You save:  C$1,098.82
रु9,404.05
List Price:  रु13,434.94
You save:  रु4,030.88
S/264.76
List Price:  S/378.24
You save:  S/113.48
K280.20
List Price:  K400.31
You save:  K120.10
SAR262.74
List Price:  SAR375.36
You save:  SAR112.62
ZK1,920.74
List Price:  ZK2,744.04
You save:  ZK823.29
L329.81
List Price:  L471.18
You save:  L141.36
Kč1,677.50
List Price:  Kč2,396.54
You save:  Kč719.03
Ft26,973.98
List Price:  Ft38,535.91
You save:  Ft11,561.93
SEK kr767.99
List Price:  SEK kr1,097.18
You save:  SEK kr329.18
ARS$69,886.30
List Price:  ARS$99,841.85
You save:  ARS$29,955.55
Bs481.32
List Price:  Bs687.64
You save:  Bs206.31
COP$309,520.29
List Price:  COP$442,190.80
You save:  COP$132,670.50
₡35,473.68
List Price:  ₡50,678.85
You save:  ₡15,205.17
L1,759.47
List Price:  L2,513.64
You save:  L754.17
₲542,904.89
List Price:  ₲775,611.66
You save:  ₲232,706.76
$U3,004.92
List Price:  $U4,292.93
You save:  $U1,288
zł286.84
List Price:  zł409.79
You save:  zł122.95
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.