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
€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 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.