04. Spring application using maven

14 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 the session, let us do a simple example of a spring application. For this, I need to create a spring project, then I need a Java class. I'm going to use a Java bean. I need configuration metadata. It can be XML or annotation or Java based configuration. To start with, I'm going to use only XML then I need a class which is going to have public static void Main with a niche I will instantiate the container and get the spring bean object that is created by the container.

Let us do that. Now. Let me go back to eclipse. Let me create the spring project. We are going to create it as a spring legacy project. File, New spring legacy project.

Let me do the name of spring basic What type of project It is, it is a simple project only. Basically we are not going to add the jar file. We want Maven to do that for us. So let me select spring, simple spring Maven and click Finish. The application is now ready. The next step is we need to check in for the dependencies in POM dot XML.

Because it is simple spring Maven, all the jar files that are relevant to work with spring is added by Maven itself. Let me open palm dot XML. This is my palm dot XML file. You can see the version. Let me just minimize this. You can see the version of spring.

The version of spring is 3.2. Point three. I don't want to work with 3.2 point three I want to work with 4.3 point one. So let us first go on check the spring moment. The And see the browser Let me open the browser spring Maven dependency, let me click this I can I've got different versions. So I'm going to use 4.3 point one.

Let me click this. Here I've got the version Can you see 4.3 point one two dot release. So I need to have this version alone. No, it is getting copied Ctrl C, come back to eclipse. Now, let me scroll down. Can you see here they are not giving the version directly.

Instead, they are just giving everything in the properties file inside properties for Java. So they have given version then for spring for hibernate for not bad for a seller for the four days that they have specified the versions how they are in saving it using expression language dollar curly brace. So I'm not going to modify any of these things. I'm just going to use the version that I have seen over there. So let me just show it to you. Here.

I'm just pasting it we have copied it right, I have pasted it, I want only these 4.3 point one Ctrl C. Now here Ctrl V and remove this. You don't need the safe save it. Now again, let me restore here I will just open Maven dependencies. Let me check Can you see now the version is 4.3 point one two. Initially the version was 3.2 point three now the version has changed to 4.3 point one two. And again whatever Java version, either you can use 1.6 or you can go in for 1.8.

The idea for going in for one point data, all that one stop concepts of 1.8 also will be supported. Like if you are using On lambda expressions are if you are working with angle brackets on the other side of creating and supported by version 1.8. So I have used 1.8 now let me save this. This is that what is the next step I need to create a simple Java class select SRC main Java right click new class let me create a simple class comm dot basic dot training is my package name, class name as user no public static void Main click Finish. Now I've got a user class. This class I need to add the instance variable.

Let me add only one instance variable string name. Let me generate getter and setter methods and also to string So classes then what is the next thing I need to create the spring bean configuration file. The spring bean configuration file should be created inside the SRC main resources. This is a Maven project. So all the configuration related stuff should be added within SRC main resources. So let me select this SRC main resources, right click new spring bean configuration file, this is what we need to add.

If you have not added spring tools to eclipse, you have to create it as a simple XML file. And then you have to write the code from the scratch, you will not get the skeleton. Of course, you can also get the skeleton by copying the XML schema which is there on the top but this the workers made very easier. Now let me just get the spring bean configuration file. I need to give a name let me give the name of application context dot XML. You can give any name I'm trying to give a name relevant to the container that says click Finish.

Now see, I have got the skeleton of the string being consecration files. This is what I was telling you. If I'm having this in any HTML, I will get the content assets directly. I don't have to bother whether sync also is added or not. What is the root tag for spring bean configuration file beans. So this tag is going to have a list of beings.

These beans are nothing but Java objects which will be created by the IOC container. Now let me create a Java object for the user class to going for being tired. This bean tack has got two attributes which we need ID equal to and class equal to what is class Class A syphilis qualified class name of the Java be in my game It's a user. So what should I use calm dot basic dot training dot user comm dot basic dot training dot user okay and what is the ID This is just a dummy and create a Java object say for user you write user users equal to new user What is this user it is just a reference pointing to this new user object. Same way if you want to give a name for the object that is created by the spring IOC container you can give it inside this ID.

So what does this code equal into this code is equal into you will say user is equal to new user understand this you are not creating the object you are specifying that this is the location of the class from that basic training. And this is the class where you create an object and assign it to this user variable that is the meaning next That particular object is having name. Now I want to give value for this name. How I can do, I can do it either using the constructor or using the setter method. We will be talking about that later, just for this application understand that this class is having one instance variable. And I'm going to give the value for the instance variable using property.

Now, keep the cursor inside this within this being tab. And here you need to add property. property name equal to name is the name of the instance variable. So for me, it is name only. So that's what we have given over here, right name, and what about the value any value you want, so I'm just giving it as rough. Let me save this Ctrl Shift Ctrl F. So I have created a configuration file in the configuration file.

I am telling the IOC containers you please create an object of this user same value for the instance variable name okay. So this is that what is the next step we need to create a Java class that which is having public static void Main. So let me select the package right click new client, sorry new class. That means you the name of client, select public static pointment Click Finish the client class is ready within public static void Main I need to instantiate the container. So I have to use application context. Application context is the interface and the implementation classes classpath XML application context.

Let me add that application you need to import add appropriate import new platform XML application context now The container is ready, you'd have to read the details of the configuration from the configuration metadata file, what is the name of the file application context dot XML. So it has to be passed as parameter within this bracket. So let me add that. Now what does IOC container will do? It will go and read all the bean definitions from the application context dot XML and create the appropriate objects. In our case, we have given only one bean definition.

So only one object will be created that is user objects. How to retrieve the object, you have to get it by the ID. So for that, I need to go in for context dot get be enough. I need to pass the ID of that particular beam definition. What is that we have given user what it is going to return it is going to return an object so you need to downcast it. Let me just show you that context dot get been method Can you see this context that good bean returns an object?

Right now I don't want the object I want I want actually I'm having is the user object. So I just have to downcast it let me don't guess you use it. Next, I need to retrieve the name user dot get make that they can print it use a dot get name, it may save this. Now, let me run the application. Also run this application, right run it as a simple Java application only because it is having public static void means select the file right click run as Java application. Now I have got the output.

What are these four lines? These lines talk about the IOC container. The IOC container is getting initialized and then it is creating all the beans ask for the bean definitions return inside the metadata file. So, you can just have a look a quick look over here classpath XML application context and it is loading the bean definition guess what is the file name application context dot XML right and we are trying to get the name of the user and we are printing it. Okay, what is the client? Actually, if you see in the client that Java program, we are trying to retrieve the name and we are giving the name hard coding the name in the XML file is this the work of spring spring is not to assign values for the primitive data types.

But to create objects. If you see here we have not created the user object, the user object is created only by the IOC container of spring. That is the bean that the Java objects are created only by the IOC container. Just to get an understanding we have started with primitive types that I mean Going forward, you'll understand what is the real use of spring, how does make helping us very easily to create enterprise applications, how to inject dependencies to a particular class, and how to add connections, and how to specify how many objects you want all these kinds of scenarios we will be discussing in the upcoming sessions. So just understand this. If you want to work with a spring application, you need to create the classes and you need to view the configuration metadata details.

It can be XML or Java base or annotation. Once that is done, you instantiate the IOC container and specify what is what type of configuration metadata you are using, and retrieve the data objects that are created by the IOC container. So the IOC container only is going to create the Java objects and add the dependencies that are needed for the Java objects when you say a dependency User can have a dependency of an address where an address is a separate class itself. So, for creating objects and adding the dependencies automatically you don't you are going in first spring framework spring framework helps in creating loosely coupled enterprise application as it is also taking care of the dependencies with the concept of dependency injection. So, we will learn more on this. So, this is for a simple spring application using spring legacy project.

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.