08. Using Annotations

18 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 how to use annotation as configuration metadata for working with a spring application. Till now, we have used XML as the configuration metadata wherein we gave all the bean definitions within an XML file. And while instantiating the container, we gave a XML file name. Now, instead of using XML, we are going to use annotations. These are the annotations that are used by spring at the rate component are great auto wire qualifier. inject resource predicts try and post conflict.

There are few more annotations available. While doing the program we will see that also after a component, this annotation should be added up over class. This master class has a spring beam. When you are working with XML you are using a bean definition bean ID quarters some dummy in a class is equal to fully qualified class name. If you give this bean definition diversity container will automatically call the default constructor of that particular class and create the object same day if you are using a direct component class the IOC container will create an object for this particular class accurate auto wire. What does this annotation for whenever a class is having dependencies?

Till now we were adding the dependencies all by ourselves like if it is going to be a collection or a map or property or any other class you are adding the dependency either using setter methods are using constructor that is to use that that angle bracket property name equal to address reference equal to address. And again, I would like a property name equal to bookless. You don't have a reference again you go when timing you added a change that was listed. That's right. So you are specifying the data This diversity container is injecting these objects to the class. But when you use our great auto wire, it means you are automatically wiring the dependency to the Java class.

Actually, it does not use who's specifying the dependencies. But the IOC container will go in search of the annotations. Depending upon the annotations that you have added it will inject the dependencies. We will be talking about this auto wire in detail in another session. So I'm just leaving it up. Together with the rate auto wired at the rate qualifier is also used as direct injection, it is equally similar to either a Tata wide only either you can use a direct injection or you can go in for a direct auto wired Ethernet resource.

This is just to match a class as a resource. When you're working in a layered architecture, you need to use this annotation in a deep layer so that Spring IOC container can understand that this is a class which is getting connected to the database. Similar to that there is one more annotation at the rate service. This is just to mark that this is this is a class which is taking care of the service layer. Basically, you are not creating the objects, you are asking the IOC container to create the object for that particular class just to specify what layer they are in, we are using those kinds of annotations. Next are direct pre destroy and accurate post conflict.

These two annotations are used in the lifecycle of a base while talking about the lifecycle of the beach. We will be discussing these two annotations Okay, how to specify that annotations are used by this class. In this case, we are going in for annotation config application context. Till now you are working with classpath XML application context. Now you need to go in for annotations. config application context wherein you need to pass the package name.

The idea over here, when you give the package name, the IOC container will go inside the package go in search of the classes that are annotated with at the rate component and automatically it will create the objects for those classes. So you can work on labor talent Asians, you don't need any XML part what if there is a scenario wherein you want to use XML and annotations together like you have got few bean definitions that are written in the XML file and you got few beans which are created using at the rate component annotation for the inbuilt classes here using XML for your one classes we are using at the rate component. So it is a combination of XML and annotation put together. In that scenario, you need to create an XML file and here you will be using class topics classpath XML application context You will be working on the XML file name in the XML file, you're allowed to do beam definitions for your own classes, you need to specify tell the IOC container that you are using annotations.

How to do that you have to use this command context colon component scan this package equal to what why why do you need to view the base package, if you do the base package, the IOC container will going first of each base package and also the sub packages. But the classes are annotated with at the rate component, either even going for creating a spring application only with annotations are a combination of XML and annotation put together. Now, let us see the annotations that are in spring at the red component. This I have explained you. If a class is annotated with that the red component automatically the IOC container will create an object for this and mark this as a spring beam. Next add direct inject this is equal and to add the rate auto wire and you have got accurate name which is equal and to add the rate qualifier we will not be using these two annotations but we will be using at the rate auto wire and Adelaide called at the rate qualifier this we will be talking about later.

Next at the rate value, what is the use of this annotation? Whenever you want the U value for your instance variables, you need to use either setter based dependency injection or you will be going in for constructor based dependency injection. That is you are using property tax or you were using constructor arguments tab in the XML file. In case of annotation, how to assign value for the instance variable. You can add a thread value above the setter method because you are adding a taboada setter method it is called a setter based dependency injection. The same thing you can do if you are having a constructor You are the tabaka instructor it becomes constructor based dependency injection.

In this case, I am just giving dollar curly brace. If I want to give a hard coded value directly I can give out the rate value within double quotes I can just give it a name right. So, it is Java course dot name. So it is Java course duration it is five days course that price is 20 days. I am hard coding the values is such a right approach. No, we have to create a separate properties file and retrieve the values from the properties file only.

That way I have created a properties file called dot name quotes that price quotes the duration you can you can add any name you want. I am just trying to relate so I have given Kohls dot name course dot price and cost or duration. And of course, I'm trying to retrieve it who's over here. In this scenario, we'll be going for this. If I want to give the values or details of the database, I can have a prop These files wherein I can give the driver name, user name, URL, password, all the stuff they can give it in the properties file, and I can retrieve it using this as rate value and dollar curly brace. We will be talking about this external, how to invoke values from external properties while also in the upcoming session.

So I'm just leaving it up. Just understand this, if you want to use annotations in a spring application, the first thing is you need to annotate the class with at the rate component, so that this will be considered by IOC container as a string being there to give values for years instance variables, you need to go in for accurate value annotation. If you want the dependencies to be wired automatically, you need to go in for accurate auto wired annotation, annotation. So these are the three main annotations which you need to know to start with in this particular session. We will see a simple example of how to use at the rate components how to use at the rate body with hard coded values and how to use accurate auto wire for a simple address instance. Let me go back to eclipse I am in Eclipse I have created a string Maven project and given the name of annotations demo, I will change the version in POM dot XML I have bought a package comm dot n o dot training I have copied the same classes that already we have worked with that doesn't set a demo employee client an address this is my employee class it is having three parameters as instance variables and address which is another class Okay.

Now, let me start what is the first step I need to annotate this class with at the rate component Okay, and how to the same values, the value should we are saying about a setter methods, it means that you are giving you are using sector based dependency injection get me going at the rate value. Law. Similarly let me assign value for salary and for number. Everything should be within double quotes, automatically the conversion will, will happen so you don't have to bother about it. Pain value for these three instance variables, or three or primitive types, whatever address addresses are class time as of now, I'm not going to touch this address, we will discuss about that later. Let me come to the client program.

This is a client class change having here we are not going to use classpath XML application context. Instead, it does annotation cause I mean annotation config application context. You can search for that you will get it. Now within parameters you need to pass on the package name. My package name is combat. Under training you either I can do the full package or I can do the base package what is the purpose in which way you have to do you need to give in the base package The reason is you may be having multiple sub folders or sub packages inside comm you will be having one classes is annotated with at the rate component inside the subfolder of a package of course, you will be adding two more classes that are annotated with at the rate component so, to avoid conflict and for the IOC container to identify all the classes that are annotated with that the red component as a beauty base package in my case it is calm saying just giving it as calm if you feel that your your base package is combat or no other classes are coming inside this combat or no means you can just statically give Comdata No.

Okay. employed okay this is the first line this is that let me remove this. Now I'm trying to call context dot get enough employee, what does this imply? When you were working with XML we gave bean ID equal to employee in this case we have not given any name. So how does IOC container will identify the name? Whenever your class annotated with that great component, when the container is trying to create an object for this particular class, di D will be the lower case, suppose a car class having to name Employee Details.

In that scenario, the ID will be in camel case, Employee Details in lowercase b uppercase, that will be the ID or name for this particular class. Now with that only I'm trying to retrieve it either I can do the dummy name or I can do this class employee dot class that also is fine. So now I will pass it. Let me save this. Now let me try that. I brought up all named Ron salary 9000 Phone number what about address address we have not given value.

So it is not okay fine. This is the first step of annotation. Next, what is my employee class which is annotated with at the rate component wants to have a different name, how to give it a go to the sampler class within parameter pass a different name EMP. Now, what is the ID of this particular employee class EMP when you want to retrieve it, how will you retrieve it context dot get b not EMP, that is all you need to call. I'll go to the slide here I need to use context dot get enough EMP. Let me save this and let me run it there is no change in the output I have got the same output Okay, this is done.

Next, let us see how to inject address into employee. In case of address it assists First Class, it is not having any hierarchy. So, I can inject address into employee class directly. The first step is the class also should be annotated with at the rate component. So, let me add a great component annotation in this class and we need to do value for city and state also sorry city and zip code also that we do that I have added added value annotation this add annotation has added a border setter method it means that we are using setter based dependency injection okay this is done, let me come to the employee class. In this employee class I need to inject address to this employee class.

How to Do I have to go in for accurate auto wired remember you can use address auto wired with certain conditions. In case of address a toll it's not having any sub classes. If it is having multiple sub classes, there will be a confusion which subclass to be injected to employee So, we will be discussing thing about that part later. Now for this application employee has one instance of address. So I need to annotate it this advert auto wired was wiring this dependency, the IOC container, you don't have to go on inject the dependency the IOC container is injecting the dependency to the employee object. Now, let me save this, I am not going to change anything in the client program.

Let me run the application. You can see the output now I have got address details also city phone and of course, some value that I have given now how to use a combination of XML and annotations put together for this. Let me go and create an XML file. Either you can create it or you can copy it from any other application. I have got better demo. Let me just copy this and I will paste it here.

Src main resources, let me rename it. I know don't take someone like this To open this file, I don't need the contents inside this particular file. So I'll remove everything because already we are using other eight components that part is not needed. So here what actually I'm going to use is context colon component scan context is a namespace, so you need to get the namespace and add it over here in this part, how to do go to the bottom here you got namespaces select namespaces. Here, concept, check this context, you will get a message click OK. Automatically context will be added I'll go to the source.

Now can you see XML ns colon context is added within this now you need to go and add context colon component scan, what is the base package comm? Let me save this. So only this line is needed because the bean definition we are trying to give using our direct component annotation. This is an indication to IOC container That's going to package and create been for all the classes that are annotated with other eight components within this comm package. I will go to this client in this case I cannot use annotation config application context. So let me comment it, I need to go in classpath XML application context.

And here I need to give this XML file name What is the XML file name are no dot XML. And what about this line context that get been a BMP it is the same no change in that part only to identify the annotations we are using the XML basically it will be used when you're working with in when you're trying to create bean definitions for the inbuilt classes offspring. Okay, now this is done. Let me run this application. Can you see the output there's no change in the output to the same output together with address. So, in this particular session, what actually we will see in this how to use another To the spring application, we have learned how to use at the rate component, how to use at the rate value and at the rate auto wired.

And also we have learned how to use a combination of XML and annotations together in which scenario will be used. This is basically when you want to create bean definitions for the inbuilt classes of spring and you're working with spring MVC, you will be doing the spot. So learn how to use add great value together with the properties file and add great auto wired in case of inheritance hierarchy in the upcoming sessions. This is for annotations. 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.