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
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
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.