14. Spring Boot application with all concepts

21 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

In this session, let us see another example of creating a spring application using Spring Boot. We will try to incorporate all the concepts that we have learned till now like auto wiring scope and lifecycle. I am an eclipse. Now, let us create the Spring Boot application. If you are in Java EE perspective, our spring perspective well and good if you are in Java perspective in that scenario, you have to go and choose other for creating a spring starter project. Let me show you go to File New.

Other from here you have to type in first string and select spring starter project. In my case, I have watched spring starter project directly. So let me select this. Let me give the name, spring demo all the Beltone is Maven packaging is jar Java version is 1.8, the package name I have given it as comm dot example dot training, that sort of stuff, you can leave it with the default. Click Next. The same author, I'm not going to select any dependency if I want to convert it into a REST service or if I'm going to use arama JDBC.

In that scenario only I need dependencies. As of now I'm going to create a spring application as simple spring application using spring boots. So I need only the spring JAR files that are offered by spring boots so you don't have to select any dependencies. Click Next and click Finish. Now the application is created Let me open it SRC main Java insider comm dot example dot training is the package name that I have given within which an application that Java class has created. Basically the class name is application the Dow only but it are your application name the application Name is spring demo all So, a class famous spring demo application that Java.

Now here we are trying to call the run method of spring application. So that this will be bootstrapped as a spring application as a Spring Boot application that we have learned already. Now, in this case, I want to make this application behave like a spring application only. I don't want to convert it into water. So, we can make this class implements command line runner and override the run method in that particular interface. Let me do that command line runner.

Now, my class implements command line runner, let me import it and I have to override the Milan method of this particular interface. This is the method what is the purpose of this method. From here only you are going to call the back end classes I told you when you are working in a simple Java Application also you also have layer one service layer one Dao layer, which is going to connect to the database that was nothing but V one layer, which is going to talk to the database. Same thing here also, if you want to connect to the back end part, you need to add the coding only from within the sun method, then give it directly inside this public static void Main, we are not going to expose this application as a RESTful web service, we are just going to have this as a spring application.

For that purpose, you need to implement command line and override the run method Okay, you know that when in a Java application when you run it automatically public static void Main only will be called and in this case, you are given spring application dot run, what will happen it will go and call the run method of spring application. But what actually i want i want to call the run method of my class that I have overridden. Now, how to do that, for that you are to create an object of spring application and pass the search parameter. Let me just show you. Now I'm creating an object of spring application, I need to pass this class as parameter in the constructor and let me end this with a semicolon. Now, I am having the object of thing applications.

So directly I can call app dot run. Now, what happens automatically it is called the overridden method in this particular class, it is not taking any arguments. Let me end it with a semicolon. Okay, this is done. Now, let me do the back end part. I am going to have an interface and an implementation class.

The idea over here is I want to call the method of the implementation class. Let me just go and add the interface first. And also I am going to have my one hierarchy of packages. Let me just show you a new interface. Greet service calm dot example dot service to see package Next, I'm not adding the classes under the same hierarchy and going in for a different hierarchy. So, this is the interface let me have one simple method string greet message passing a message.

This should be implemented in our class. Let me create the implementation class also. So select the package right click new class it is greed service I am Pl. Now click Finish. Here I will just add the implementation part. This class should implement breed service.

Okay, and I have to override the method that may just keep the cost and I have got that unimplemented methods now here it says return message or here one second, I will just change it over here I don't want message I love it as name and share also in habitus Nate and let me just give a nice message I have it with me okay. So, this method is implemented properly. Now, let me go back to the spring demo all application dot java. Now I need to call the method off greet service. If it is a simple Java application, what you will do you will write interface reference equal to implementation class and then you will call the method like greet service service is equal to new greet service I am PL and then service dot greet message this is how you do but in case of spring application, you are not going to do that the implementation classes will be totally hidden from you.

You are Not going to create an object for the implementation class, you are asking the spring IOC container to do it for you. So what actually you have to do, the first step is you have to make the IOC container recognize that this is a spring beam. So go and add the first annotation above this class at the rate component. Okay, this is done. Now, let me come to this spring demo application. This class is going to call the method of the service.

So let me just add greet service instance, I have added greet service as the instance variable. Now what I have to do using this instance variable, I can call the methods let me call the method from here, great service dot brief message. I'm ending it with a semicolon. This is going to return us I mean a string so let me get it in a second. And I will print it in the next slide. Okay, this is done this is completed.

What is the flow over here I have given great service and directly I'm trying to call the greet service dot greet message. As I told you, I am supposed to write greet service is equal to new great service imp. And if I don't do that, I will get null pointer exception, but we are in a spring application. We want spring to identify the implementation class and automatically wire to this particular class. What is this class spring demo application? This is annotated with accurate Spring Boot.

What is that average Spring Boot application right on the top it means this class also is a spring beam. This class is in need of grief service. So I'm auto wiring it. So I bought this I need to add at the rate auto wired and for this service, we are going to use getter and setter based dependency injection at the source generate getter and setter methods. Click OK. Now, this is done. Let me save this now I need to run this application right click ranas Spring Boot app our Java application for the application started here I am getting an error application fail to start great service in comm dot example dot training required a bean of type greet service that could not be found.

We have use at the rate component and the component annotation here avoid this we have added at the rate component, but still it is not recognizing it. Why because we are in a different package hierarchy. So above this class, we need to add a grade component scan within which you need to specify the base package. Only with the help of the base package. It can identify the classes that are annotated with our grade components. You Basically if you are in the same hierarchy I have given comm dot example dot training if I'm in the same hierarchy of like comm dot example dot training dot service in that case it will be able to identify because we are going in for it through a different route this is a problem okay.

Now I let me add this article to comm dot example dot service. Now let me save this, let me run this application. Now can you see the output and getting the output right here, the annotation con con that what is an annotation config application context the IOC container is getting started. Then the beans are created. It is ready to service that's mine it is giving the outputs and finally you are closing the IOC container basically we have not written any code automatically this happening. So this has the advantage of working with Spring Boot applications.

Later we have added any dependency In palm dot XML, now, we have gone for writing the code of creating the IOC container the IOC container is getting created automatically and also it is getting close automatically. These are few advantages of working with Spring Boot Okay. Now, you can just see this there is a yes symbol it means that this is a Spring Boot. Let me just show you one more example, here itself, I will just show you I want to have two layers, one layer is a service layer and one layer deeper layers in that case how this is going to work. Let us try that out. Let me just create the classes.

So, here inside combat service I mean example dot service itself. Let me create book service and book imp and this is my book service. Let me add two methods book, get book by ID. Inside the book is a class which I need to create I will just create it next Get all these other two methods and I will be adding an implementation class. Also I want to go to a do layer. In this case, I'm not going to connect to the database but I'm just going to have the same method but I will write the logic in the dmo layer instead of writing it in service layer.

Let me just show you that get our books. Okay, I need to import all these first let me create the book class. And then I will continue. Today, I have got title book ID and prices instance variable. I have created the getter and setter methods generator to sync all time having a parameterised constructor. The idea over here is I don't want to retrieve the books from the database.

I am just going to do hard coded values. That is why I'm going in for a parameterised constructor. Next, let us see this book service imp. This is the implementation class of book service. Here I am trying to do my own logic if it is one, the return this book, yes, return and mute. Similarly for guitar books also and trying to convert an array into an ArrayList you can just have a these are the three books for this only I have gone from a parameterised constructor.

Okay, now let me show you one more thing. Now I don't want to have the coding over here. I want to go in for the next layer book da one book DIY MPL. So let me create the interface so SRC main Java, right click new interface. Here it is comm dot example dot not service now what I want I want to go in for the DA layer right so da vo now here this book da vo click Finish. Let me just copy the same methods from this book service Ctrl C in book do and just going to add the method.

Similarly I need to create Ba ba ba EMP. So let me just add that select the package right This new class book DIY MPL. And this has to implement to be able so what I will do is I'll just copy the code which I have gotten book service imp and okay. So let me just copy it here Ctrl V. Now it is not implementing book service, but it has to implement book do. Basically, this is the from place from where you will be calling the database. I don't have a database, I am just trying to do hard coded values.

Okay, now I will come to this book service imp. In boot service, I am PL I don't want to have this logic, because we have written the logic in book Da Ye MP and now we need to call the method of booth da y MPs. So what actually I have to do, I have to have book the instance variable. I hope you can understand from your I'm trying to call this method boop da Okay, so how we will write so first let me import it. Then return mu d dot get book by LD. I hope you can understand the spot boop da was an interface if it is a non simple Java application, what do you need to do book do do equal to new boo ba ye MPL.

This is how you have to create it, and then only you can just call the method, but directly I'm trying to call the methods that is what I want to tell you using a spring application. I'm not going to use new keywords, I'm just going to do the auto wiring the dependencies will be wired automatically to that appropriate matches by spring framework. And in this case, also the turn book the dot get on Okay, now, water sanitation that you are Going to use now either you can go in for address components are you can just specify that this is a service layer so you can use at the rate service here I'm going to add at the rate service meaning to say that this is a service layer Okay. Next let me go to this book DIY MPs here are trying to add annotation what annotation they can add as usual I told you I can go in for a direct component are you this is a DA when they are talking to the database, I can go in for a direct repository.

Okay, this is that Boothbay was completed. Let me again come to this book service imp. In this case water sanitation I need to add I need to add add great auto wired means create an object of Buddha EMP and inject it to book service I am PL Okay, let me import this. Okay, you need to add the getter and setter method if you are going in for database differentiation. Go to source generate getter and setter methods select all click ok this is done okay let me just go in for the main class here from here only we are going to call it so what I have to do here I need to auto a book service at the rate auto wired oops we are not creating an object of this implementation class let me import it okay and one more thing this time let us not make mistakes component scan I don't want to go in for service a lot because I have got one other package D also so the Dave packages comm dot example.

Now from here let me try to call it us in book service let me call it the first one is get booked by ID right so directly book service dot get booked by ID may passing one and then the next one is the list. This also will use printed in statement only directly book service dot get Just to see the difference, I will have a dummy printed and statement. Empty printed statement. Okay, this is all set this application, you know put his book title equal to Java in action book ID zero price to 300. Yeah why book ID is zero. I'm overriding this two string method in book dot java that I have not added book ID.

So that's why it is giving it if you don't want this book ID you can just remove it off. So what I will do I will remove this book ID part. Okay, let me just run this application again. Here spring demo. Okay, let me maximize console. You can see the output title Java in action five 300.

And again, it is listing all the books, all the three books. Okay, I hope you got the feel of how to create a string Using spring boots, this is the Real Usage of spring whenever a class having another class of instance variables in that scenario you don't have to create an object for that class you can use at the rate auto wired annotation automatically it is very wired by spring framework in this particular case book service or sorry book demo class was depending on book service and when you go into this book service book service I am PL was depending on Ruby we'll be using at the rate auto wired annotation automatically thing a they are all getting wired. It's like kind of a change demo the fun fun book service book service depends on book Diego and who's wiring it spring framework. Now if you want to add scope, what do you have to do directly can go in for this book DIY EMP, and if you want to create new objects of eBay MPL for each and every request, you can directly go on that at the rate scope annotation scope It is prototype okay.

And if you want to see it to add lifecycle methods like are once objects to do object is created if you want to call add some connection details in that scenario create two methods they are which are annotated with at the rate post construct and pre destroy. So, this is for the lifecycle part for the scope also you can take I mean you can check whether the scope prototype is working by creating a new object calling the setter method and so on just like what we have done earlier, okay. So, what actually we are seen in this particular session is how to create a spring application using spring boots. We have also tried to incorporate the concepts that we have learned we have learned about few of them into more annotation at the rate service and at the rate repository. Basically at the rate repository will be used when you are connecting to the database.

Here I am not connecting to the database we are we are giving The hard coded values of how you create a spring application using Spring Boot. In the next session, let us learn how to convert a spring application into a RESTful web service using 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.