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
€66.35
List Price:  €94.79
You save:  €28.44
£55.27
List Price:  £78.97
You save:  £23.69
CA$97.91
List Price:  CA$139.89
You save:  CA$41.97
A$107.61
List Price:  A$153.73
You save:  A$46.12
S$94.02
List Price:  S$134.32
You save:  S$40.30
HK$544.76
List Price:  HK$778.26
You save:  HK$233.50
CHF 61.99
List Price:  CHF 88.57
You save:  CHF 26.57
NOK kr773.52
List Price:  NOK kr1,105.08
You save:  NOK kr331.55
DKK kr494.87
List Price:  DKK kr706.99
You save:  DKK kr212.12
NZ$119.04
List Price:  NZ$170.06
You save:  NZ$51.02
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,334.01
List Price:  ৳11,906.24
You save:  ৳3,572.23
₹5,905.12
List Price:  ₹8,436.24
You save:  ₹2,531.12
RM312.96
List Price:  RM447.10
You save:  RM134.14
₦117,219.95
List Price:  ₦167,464.25
You save:  ₦50,244.30
₨19,380.58
List Price:  ₨27,687.73
You save:  ₨8,307.15
฿2,429.14
List Price:  ฿3,470.35
You save:  ฿1,041.21
₺2,412.08
List Price:  ₺3,445.98
You save:  ₺1,033.89
B$405.06
List Price:  B$578.68
You save:  B$173.62
R1,267.31
List Price:  R1,810.53
You save:  R543.21
Лв129.73
List Price:  Лв185.34
You save:  Лв55.60
₩97,779.80
List Price:  ₩139,691.42
You save:  ₩41,911.61
₪261.55
List Price:  ₪373.67
You save:  ₪112.11
₱4,130.77
List Price:  ₱5,901.36
You save:  ₱1,770.58
¥10,901.93
List Price:  ¥15,574.86
You save:  ¥4,672.92
MX$1,414.26
List Price:  MX$2,020.46
You save:  MX$606.19
QR254.33
List Price:  QR363.35
You save:  QR109.01
P948.83
List Price:  P1,355.53
You save:  P406.70
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.50
List Price:  E£4,969.50
You save:  E£1,491
ብር8,583.46
List Price:  ብር12,262.61
You save:  ብር3,679.15
Kz63,877.12
List Price:  Kz91,256.94
You save:  Kz27,379.82
CLP$67,980.58
List Price:  CLP$97,119.28
You save:  CLP$29,138.70
CN¥507.18
List Price:  CN¥724.58
You save:  CN¥217.39
RD$4,200.06
List Price:  RD$6,000.34
You save:  RD$1,800.28
DA9,338.81
List Price:  DA13,341.73
You save:  DA4,002.91
FJ$158.92
List Price:  FJ$227.04
You save:  FJ$68.12
Q538.38
List Price:  Q769.16
You save:  Q230.77
GY$14,583.53
List Price:  GY$20,834.51
You save:  GY$6,250.97
ISK kr9,640.42
List Price:  ISK kr13,772.62
You save:  ISK kr4,132.20
DH696.78
List Price:  DH995.44
You save:  DH298.66
L1,273.82
List Price:  L1,819.82
You save:  L546
ден4,077.60
List Price:  ден5,825.40
You save:  ден1,747.79
MOP$559.09
List Price:  MOP$798.74
You save:  MOP$239.64
N$1,260.89
List Price:  N$1,801.35
You save:  N$540.46
C$2,566.52
List Price:  C$3,666.61
You save:  C$1,100.09
रु9,423.16
List Price:  रु13,462.23
You save:  रु4,039.07
S/264.65
List Price:  S/378.10
You save:  S/113.44
K280.62
List Price:  K400.90
You save:  K120.28
SAR262.75
List Price:  SAR375.38
You save:  SAR112.62
ZK1,926.57
List Price:  ZK2,752.36
You save:  ZK825.79
L330.17
List Price:  L471.69
You save:  L141.52
Kč1,676.98
List Price:  Kč2,395.79
You save:  Kč718.80
Ft27,187.34
List Price:  Ft38,840.72
You save:  Ft11,653.38
SEK kr769.78
List Price:  SEK kr1,099.74
You save:  SEK kr329.95
ARS$70,112.88
List Price:  ARS$100,165.56
You save:  ARS$30,052.67
Bs483.65
List Price:  Bs690.96
You save:  Bs207.30
COP$308,470.35
List Price:  COP$440,690.82
You save:  COP$132,220.46
₡35,450.59
List Price:  ₡50,645.88
You save:  ₡15,195.28
L1,762.11
List Price:  L2,517.41
You save:  L755.29
₲543,153.02
List Price:  ₲775,966.15
You save:  ₲232,813.12
$U2,992.07
List Price:  $U4,274.58
You save:  $U1,282.50
zł287.83
List Price:  zł411.21
You save:  zł123.37
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.