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
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
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.