12. Using Java based Configuration

13 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
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
Already have an account? Log In

Transcript

Now that we are learned how to use XML annotation as configuration metadata, let us learn about Java based configuration. So, in this session, we will be learning about Java based configuration. Together with that, I will also talk about how to use scope and lifecycle in case of a Java based configuration. In case of Java based configuration metadata, yes class behaves like a configuration metadata file. The class should be annotated with at the rate configuration. That is a example that I have given over here, the class name I have given us app config, just to specify that this is a configuration file.

The bean definitions are given in this configuration file. How to give the bean definition you put as an XML you use angle bracket beam, that is you use that being tagged. In case of annotation. You use as the component in case of Java based configuration, you need to have a method which is annotated with at the rate beam. In that method you are going to create the object all by yourself, you are not asking the IOC container to create the object you are creating the object as you are returning it. In my case you can just have a look I have created a method get employee and I am returning an employee object and this is annotated with at the rate beam and what is the name of this particular beam.

If it is XML you use bean ID equal to if it is annotation, if you use a separate component, the class name will be the the name if you want to give a different name you have to give it within round brackets. In case of this Java based configuration, you whatever method name you have given, that will be the name. If you want to give a different name you need to use naming So, can you see I have given name equal to address. By default, the address the name is get address only I want to give a different name. So I have given name equal to address and what is the scope Scopus prototype? Together with this you can also add lifecycle methods and you can specify what are the lifecycle methods.

Now, let me go back to eclipse, I am an eclipse, I have created a spring Maven project I have given the name of Java based demo, I'm going to work with the same Java classes that we have seen in Saturday's dependency injection and in annotations that is the same employee and address I have created a package comm dot spring dot j basic wherein I have got an employee class, the class is having just name and addresses instance variables. Address I have used the annotation at the rate auto wired up or setter method I have used a great value to initialize the instance variable name Okay, what about at the rate component here, we are not going to use our rate component. We are going to use Java based configuration. Let me save this file. Let me come to this address. In address I'm having city and state as instance variables and giving the values using address value annotation.

As I told you, you don't have to hard code it over here, you can have a properties file and you can retrieve the values from the properties file. I need to view the configuration metadata for this I need a Java class properly I have created the Java class and comm dot spring dot config package. The class name is app config. The class should be annotated with other a configuration. This will say for sanitation. Next.

This class is in a different package hierarchy, not an issue. Yes. What you need to do is you have to go on at either a component scan, component scan annotation, This is just to tell the IOC containers go and search in this particular package for the spring beans component scan, let me give the package name package is equal to comm dot spring Okay, now within this class I need to create objects for the employee and address. So let me go in for the red bean annotation and I have to create a method that is you're creating a method that is annotated with at the rate being public employee get employee return new employee. Please let me create one more method which will return an address object I have to import all these steps. I have to do that also I need to import the annotation and the Java class that we do that I have added the method both methods are ready it means that you have created two objects or two spring beans one week names get employee the other big names get address.

Now, let me go back to the employee class in the employee class I have use activate activate auto wired annotation. So, what actually the IOC container will do is this dependency has to be injected to employee first if you go and check for a bean with the name address, auto wire by name, if that particular beam is not available, rewind check for a beam which belongs to address class of course, we have a beam which belongs to address class that is this one. So, this particular object will be injected through the employee objects, what does the employee object name get employee if you want to give a different name again you can just go and give name equal to employee. So we will see that part later. Now I will come to this main line in the main client what we have to do as usual instantiate the IOC container and we have to call get bean method.

Let me add the code. I have added the code now What should I pass Within the parameter, even we were working with annotations we were passing the package name, when we were working with XML, we were parsing the XML file name, in this case, we need to pass the class name which is six, which is having the configuration details. So it is app config.ca Okay, now, this is done next context dot get big enough here what you have to pass you need to pass the ID of this particular spring beam in our case actually, in case of XML, we had ID in case of a third component it was the same class name, but in this case, what is the class name it is get employee. So either you can just pass that method name itself, ah, you can pass the class name. So, if you just do Ctrl Space, you can just have a look.

It is having two overloaded methods. One is classes parameters are the one a single parameter. Now in this case, I'm just going to go in for the class Law class. Now, let me save this. So, I hope you can understand we have created a separate class Java class which is going to add a configuration metadata, annotate the classes add the rate configuration, if your classes are in a different package hierarchy, you need to use either a conference cap and specify the base package and then within the class, you add methods that are annotated with at the rate beam, these methods are responsible for creating the Java objects for you. Now, let me save this and let me run this application and go to main class and application.

I have got the output name raw addresses city Bangalore state k, from where it is coming, let me just take it out. Yes. Can you see this? So this is the Okay I will just show you, I will go to this employee class. I can also add at the rate component annotation How many objects are there employee objects are using a thread component IOC create a container is creating an employee object and there is one more employee object which is getting created here and what is the name it is get employee. Now, let me run this application let us see what is happening.

Don't put no qualifying been of type conduct spring that a basic employee available, why expected single but found to water the employees because of this added component annotation get employees because you are creating it again in the configuration file. So, you have to be extra careful either going for this particular beam definitions are used at the rate component annotation. If you are using either a component annotation then in that scenario in the client, don't use App config directly use the package name so that only that will be identified. Okay. So just to make you understand, I have just Given this, now I will remove it. Now let us see how to give the scope of this employee object as prototypes.

So I'm just going to this app config dot java shared directly I can use at the rate scope annotation. Just like how we have worked with the annotation. Same way only this is also working. The only thing is all the configuration details are added within this app. config file. Now I'm saving it.

Let's check it out. I'll go to this main client Ctrl C Ctrl. V, I'm trying to get employee one and then changing the city. Employee one dot, you're changing the name let me change the name. JOHN, and I'm trying to print employee one and employee both. Save it.

Let me run the application. Okay, you can see the output. This is a implies this is employed One, this is again employee. If I'm just commenting the scope then it has to do same result for all for all the three printers and statements. Yeah sorry, first it will be wrong changing it to john. This is for employee one.

Then finally employees which is also pointing to the same object in because it is Singleton. So this is completed. Next, how to add the lifecycle methods. If you are having your own custom made lifecycle methods, add it directly in the class in the employee class. So, let me add two methods in it and cleanup method. I've added two methods in it and cleanup.

Now, I'll go to this app config dot java here. Let me just use init method equal to the name of the method. Comma destroy destroy method Porto cleanup. Okay, now I don't want this as prototype and remove this and have addresses prototype. Okay, now I'll go to this main client. Let me comment this code prototype I'm saving this let me run the application I have brought initialize initialize is coming from this init method which I have created, you can also have methods that are annotated with at the rate rose construct and three the strife they will be called even before these methods, okay cleanup is not cause because of the reason I have told you, I have to go in for upset application context.

And I have to make the employee object not employee equal to null an employee one equal to none now Text dot register shut down and you can close the context also. Let me save this now let me run the application now I'm getting the cleaner port also. So, what actually we have seen in this particular session is how to use Java based configuration was for working with a spring application. In this scenario, I have also shown you how to use annotation if you are using annotation what is the conflict that is coming and I am not given a different name for address. If you want, we can give a different name like maybe call to address when an auto wiring is happening by name. There will be no change in the output.

I'm just showing you the differences, different ways of doing the same application. I have completed the spring core part wherein we have learned how to work with spring application using setter based dependency injection constructor based dependency injection and how to use the difference configuration metadata types, we have learned how to use XML, how to use annotation and Java based configuration. But basically the idea is we don't want to create a spring application like this, we are moving forward to something called a spring boots, which helps to create ready to run application. So in the next session that the start with Spring Boot, 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.