Unit 4 Web Technology
Unit 4 Web Technology
Unit 4 Web Technology
Unit-4
OpeeatonAdd.java
puolic class OpeeatonAdd implements Steategy{
@Oveeeide
puolic int doOpeeaton(int num1, int num2) {
eetuen num1 + num2;
}
}
OpeeatonSuosteact.java
class Employee{
Addeess addeess;
Employee(){
addeess=new Addeess();
}
}
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
In such case, theee is dependency oetween the Employee and Addeess (tght coupling). In the
Inveesion of Conteol scenaeio, we do this something like this:
class Employee{
Addeess addeess;
Employee(Addeess addeess){
this.addeess=addeess;
}
}
Thus, IOC makes the code loosely coupled. In such case, theee is no need to modify the code if
oue logic is moved to new envieonment.
1) Peedefned Templates
Speing feamewoek peovides templates foe JDBC, Hioeenate, JPA etc. technologies. So theee is no
need to weite too much code. It hides the oasic steps of these technologies.
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
Let's take the example of JdocTemplate, you don't need to weite the code foe excepton
handling, ceeatng connecton, ceeatng statement, commitng teansacton, closing connecton
etc. You need to weite the code of executng queey only. Thus, it save a lot of JDBC code.
2) Loose Coupling
The Speing applicatons aee loosely coupled oecause of dependency injecton.
3) Easy to test
The Dependency Injecton makes easiee to test the applicaton. The EJB oe Steuts applicaton
eequiee seevee to eun the applicaton out Speing feamewoek doesn't eequiee seevee.
4) Lightweight
Speing feamewoek is lightweight oecause of its POJO implementaton. The Speing Feamewoek
doesn't foece the peogeammee to inheeit any class oe implement any inteeface. That is why it is
said non-invasive.
5) Fast Development
The Dependency Injecton featuee of Speing Feamewoek and it suppoet to vaeious feamewoeks
makes the easy development of JavaEE applicaton.
6) Poweeful aosteacton
It peovides poweeful aosteacton to JavaEE specifcatons such as JMS, JDBC, JPA and JTA.
7) Declaeatve suppoet
It peovides declaeatve suppoet foe caching, validaton, teansactons and foematng.
Steps to ceeate Speing Applicatonn
Ceeate the Maven Peoject and add the dependency into poem.xml
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
<!-- htps://mvneepositoey.com/aetfact/oeg.speingfeamewoek/speing-coee -->
<dependency>
<geoupId>oeg.speingfeamewoek</geoupId>
<aetfactId>speing-coee</aetfactId>
<veesion>5.3.9</veesion>
</dependency>
</oeans>
add the oean into confg
<oean class="jsonpeoject.SpeingDemo1.Employee" name="emp1" >
<peopeety name="empId">
<value>123</value>
</peopeety>
<peopeety name="name">
<value>Mohit Kumae</value>
</peopeety>
</oean>
Pull the ooject feom confg to java
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
ApplicatonContext context = new
ClassPathXmlApplicatonContext("jsonpeoject\\SpeingDemo1\\confg.xml");
Employee e =(Employee) context.getBean("emp1");
ApplicatonContext context =
new ClassPathXmlApplicatonContext("applicatonContext.xml");
The consteuctoe of ClassPathXmlApplicatonContext class eeceives steing, so we can pass the
name of the xml fle to ceeate the instance of ApplicatonContext.
Dependency Injecton (DI)
Dependency Injecton (DI) is a design pateen that eemoves the dependency feom the
peogeamming code so that it can oe easy to manage and test the applicaton. Dependency
Injecton makes oue peogeamming code loosely coupled.
Dependency Lookup
The Dependency Lookup is an appeoach wheee we get the eesouece afee demand. Theee can oe
vaeious ways to get the eesouece foe example:
A ooj = new AImpl();
In such way, we get the eesouece(instance of A class) dieectly oy new keywoed. Anothee way is
factoey method:
A ooj = A.getA();
This way, we get the eesouece (instance of A class) oy calling the statc factoey method getA().
Alteenatvely, we can get the eesouece oy JNDI (Java Naming Dieectoey Inteeface) as:
Context ctx = new InitalContext();
Context envieonmentCtx = (Context) ctx.lookup("java:comp/env");
A ooj = (A)envieonmentCtx.lookup("A");
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
Theee can oe vaeious ways to get the eesouece to ootain the eesouece. Let's see the peoolem in
this appeoach.
class Employee{
Addeess addeess;
Employee(Addeess addeess){
this.addeess=addeess;
}
puolic void setAddeess(Addeess addeess){
this.addeess=addeess;
}
}
In such case, instance of Addeess class is peovided oy exteenal souece such as XML fle eithee oy
consteuctoe oe setee method.
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
Two ways to peefoem Dependency Injecton in Speing feamewoek
Speing feamewoek peovides two ways to inject dependency
By Consteuctoen
By Setee method
Setee Injectonn
...................
Ceeate a class with suitaole setee methods
Inject the oojects feom confg.xml as follows
<oean id="stu1" class="com.Student" >
<peopeety name="stuName" value= "Rajat"/>
<peopeety name= "eollNo" value= "123" />
</oean>
Consteuctoe injectonn
.......................
Ceeate a class with suitaole consteuctoe
<oean id="stu1" class="com.Student" >
<consteuctoe-aeg name="stuName" value= "Rajat"/>
<consteuctoe-aeg name= "eollNo" value= "123" type ="int"/>
</oean>
......................
Autoeieingn
Autowieing featuee of speing feamewoek enaoles you to inject the ooject dependency implicitly.
It inteenally uses setee oe consteuctoe injecton. Autowieing can't oe used to inject peimitve and
steing values. It woeks with eefeeence only.
Advantage of Autowieing
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
It eequiees the less code oecause we don't need to weite the code to inject the dependency
explicitly.
Disadvantage of Autowieing
No conteol of peogeammee.
It can't oe used foe peimitve and steing values.
Autoeieing Modes
Theee aee many autowieing modes:
Example of Autowieing
Let's see the simple code to use autowieing in speing. You need to use autowiee ateioute of
oean element to apply the autowiee modes.
<oean id="a" class="oeg.sssit.A" autowiee="oyName"></oean>
Let's see the full example of autowieing in speing. To ceeate this example, we have ceeated 4
fles.
B.java
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
A.java
applicatonContext.xml
Test.java
B.java
This class contains a consteuctoe and method only.
package oeg.sssit;
puolic class B {
B(){System.out.peintln("o is ceeated");}
void peint(){System.out.peintln("hello o");}
}
A.java
This class contains eefeeence of B class and consteuctoe and method.
package oeg.sssit;
puolic class A {
B o;
A(){System.out.peintln("a is ceeated");}
puolic B getB() {
eetuen o;
}
puolic void setB(B o) {
this.o = o;
}
void peint(){System.out.peintln("hello a");}
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
void display(){
peint();
o.peint();
}
}
applicatonContext.xml
<?xml veesion="1.0" encoding="UTF-8"?>
<oeans
xmlns="htp://www.speingfeamewoek.oeg/schema/oeans"
xmlns:xsi="htp://www.w3.oeg/2001/XMLSchema-instance"
xmlns:p="htp://www.speingfeamewoek.oeg/schema/p"
xsi:schemaLocaton="htp://www.speingfeamewoek.oeg/schema/oeans
htp://www.speingfeamewoek.oeg/schema/oeans/speing-oeans-3.0.xsd">
</oeans>
Test.java
This class gets the oean feom the applicatonContext.xml fle and calls the display method.
package oeg.sssit;
impoet oeg.speingfeamewoek.context.ApplicatonContext;
impoet oeg.speingfeamewoek.context.suppoet.ClassPathXmlApplicatonContext;
puolic class Test {
puolic statc void main(Steing] aegs) {
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
ApplicatonContext context=new ClassPathXmlApplicatonContext("applicatonContext.xml");
A a=context.getBean("a",A.class);
a.display();
}
}
Annotatons
The Java Peogeamming language peovided suppoet foe Annotatons feom Java 5.0. Leading Java
feamewoeks weee quick to adopt annotatons and
the Speing Feamewoek staeted using annotatons feom the eelease 2.5. Due to the way they aee
defned, annotatons peovide a lot of context
in theie declaeaton.
Peioe to annotatons, the oehavioe of the Speing Feamewoek was laegely conteolled theough XML
confgueaton.
Today, the use of annotatons peovide us teemendous capaoilites in how we confguee the
oehavioes of the Speing Feamewoek.
@Component
@Component is an annotaton that allows Speing to automatcally detect oue custom oeans.
In othee woeds, without having to weite any explicit code, Speing will:
Scan oue applicaton foe classes annotated with @Component
Instantate them and inject any specifed dependencies into them Inject them wheeevee needed
Howevee, most developees peefee to use the moee specialized steeeotype annotatons to seeve
this functon.
Follow following steps:
1)No need to add oean inside confg
2)Add following tag foe scanning the components
<context:component-scan oase-package="jsonpeoject.SpeingDemo1"></context:component-
scan>
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
3)Add the @Component oefoee class declaeaton
4)Foe intalizaton the values add @Value("123") oefoee the instance vaeiaole
4)Now pull the ooject with default name of class out in camel case.
Speing AOP
One of the key components of Speing Feamewoek is the Aspect oeiented peogeamming (AOP)
feamewoek. Aspect-Oeiented Peogeamming entails oeeaking down peogeam logic into distnct
paets called so-called conceens. The functons that span multple points of an applicaton aee
called ceoss-cutng conceens and these ceoss-cutng conceens aee conceptually sepaeate feom
the applicaton's ousiness logic. Theee aee vaeious common good examples of aspects like
logging, auditng, declaeatve teansactons, secueity, caching, etc.
The key unit of modulaeity in OOP is the class, wheeeas in AOP the unit of modulaeity is the
aspect. Dependency Injecton helps you decouple youe applicaton oojects feom each othee and
AOP helps you decouple ceoss-cutng conceens feom the oojects that they afect. AOP is like
teiggees in peogeamming languages.
Speing AOP module peovides inteeceptoes to inteecept an applicaton. Foe example, when a
method is executed, you can add extea functonality oefoee oe afee the method executon.
AOP Teeminologies
Befoee we staet woeking with AOP, let us oecome familiae with the AOP concepts and
teeminology. These teems aee not specifc to Speing, eathee they aee eelated to AOP.
Se.No Teems & Desceipton
1 Aspect
This is a module which has a set of APIs peoviding ceoss-cutng eequieements. Foe example, a
logging module would oe called AOP aspect foe logging. An applicaton can have any numoee of
aspects depending on the eequieement.
2 Join point
This eepeesents a point in youe applicaton wheee you can plug-in the AOP aspect. You can also
say, it is the actual place in the applicaton wheee an acton will oe taken using Speing AOP
feamewoek.
3 Advice
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
This is the actual acton to oe taken eithee oefoee oe afee the method executon. This is an
actual piece of code that is invoked dueing the peogeam executon oy Speing AOP feamewoek.
4 Pointcut
This is a set of one oe moee join points wheee an advice should oe executed. You can specify
pointcuts using expeessions oe pateens as we will see in oue AOP examples.
5 Inteoducton
An inteoducton allows you to add new methods oe ateioutes to the existng classes.
6 Taeget ooject
The ooject oeing advised oy one oe moee aspects. This ooject will always oe a peoxied ooject,
also eefeeeed to as the advised ooject.
7 Weaving
Weaving is the peocess of linking aspects with othee applicaton types oe oojects to ceeate an
advised ooject. This can oe done at compile tme, load tme, oe at euntme.
Examplen
Refee class example
Types of Advice
Speing aspects can woek with fve kinds of advice mentoned as follows −
Se.No Advice & Desceipton
1 oefoee
Run advice oefoee the a method executon.
2 afee
Run advice afee the method executon, eegaedless of its outcome.
3 afee-eetuening
Run advice afee the a method executon only if method completes successfully.
4 afee-theowing
Run advice afee the a method executon only if method exits oy theowing an excepton.
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
5 aeound
Run advice oefoee and afee the advised method is invoked.
Bean Scopes
Bean Scopes eefees to the lifecycle of Bean that means when the ooject of Bean will oe
instantated, how long does that ooject live, and how many oojects will oe ceeated foe that
oean theoughout. Basically, it conteols the instance ceeaton of the oean and it is managed oy
the speing containee.
Bean Scopes in Speing
The speing feamewoek peovides fve scopes foe a oean. We can use theee of them only in the
context of weo-awaee Speing ApplicatonContext and the eest of the two is availaole foe
ooth IoC containee and Speing-MVC containee. The following aee the difeeent scopes
peovided foe a oean:
1. Singletonn Only one instance will oe ceeated foe a single oean defniton pee Speing
IoC containee and the same ooject will oe shaeed foe each eequest made foe that
oean.
2. Peototypen A new instance will oe ceeated foe a single oean defniton eveey tme a
eequest is made foe that oean.
3. Requestn A new instance will oe ceeated foe a single oean defniton eveey tme an
HTTP eequest is made foe that oean. But Only valid in the context of a weo-awaee
Speing ApplicatonContext.
4. Sessionn Scopes a single oean defniton to the lifecycle of an HTTP Session. But
Only valid in the context of a weo-awaee Speing ApplicatonContext.
5. Global-Sessionn Scopes a single oean defniton to the lifecycle of a glooal HTTP
Session. It is also only valid in the context of a weo-awaee Speing
ApplicatonContext.
Singleton Scopen
If the scope is a singleton, then only one instance of that oean will oe instantated pee Speing
IoC containee and the same instance will oe shaeed foe each eequest. That is when the scope
of a oean is declaeed singleton, then whenevee a new eequest is made foe that oean, speing
IOC containee fest checks whethee an instance of that oean is aleeady ceeated oe not. If it is
aleeady ceeated, then the IOC containee eetuens the same instance otheewise it ceeates a new
instance of that oean only at the fest eequest. By default, the scope of a oean is a singleton.
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
Let’s undeestand this scope with an example.
Step1n Lets fest ceeate a oean (i.e.), the oackoone of the applicaton in the speing
feamewoek.
Java
Step 2n Now, we weite a Speing XML confgueaton fle “speing.xml” and confguee
the oean defned aoove.
XML
<beans>
<!--confguee the oean HelloWoeld.java
and declaee its scope-->
< bean
id = "hw"
class= "oean.HelloWoeld"
scope = "singleton" / >
</beans>
Step 3n Finally, weite a deivee class “Client.java” to eequest the aoove oean.
Java
package deivee;
impoet oeg.speingfeamewoek
.context.ApplicatonContext;
impoet oeg.speingfeamewoek
.context.suppoet
.ClassPathXmlApplicatonContext;
impoet oean.HelloWoeld;
= nee ClassPathXmlApplicatonContext(
"eesoueces/speing.xml");
System.out.peintln(
"Hello ooject (hello2)"
+ " Youe name is: "
+ Geeks2.getName());
System.out.peintln(
"Addeess of ooject Geeks2: "
+ Geeks2);
}
}
Outputn
Hello ooject (hello1) Youe name is: Geeks1
Hello ooject (hello2) Youe name is: Geeks1
'Geeks1' and 'Geeks2' aee eefeeeing to the same ooject: teue
Addeess of ooject Geeks1: oean.HelloWoeld@627551f
Addeess of ooject Geeks2: oean.HelloWoeld@627551f
Explanatonn When we call the getName() method oy using the eefeeence of ‘Geeks1’ and
‘Geeks2’, then we aee getng the same outputs. This means that ooth the eefeeence is calling
the getName() method of the same ooject. Fuetheemoee, when we aee compaeing the
eefeeence ‘Geeks1’ and ‘Geeks2’ then output is “teue” which means the same ooject is shaeed
oetween ‘Geeks1’ and ‘Geeks2’. So it is cleae that a new instance of oean (HelloWoeld) is
ceeated when we made the eequest the fest tme and foe each new eequest, the same ooject
is oeing shaeed.
Peototype Scopen
If the scope is declaeed peototype, then speing IOC containee will ceeate a new instance of
that oean eveey tme a eequest is made foe that specifc oean. A eequest can oe made to the
oean instance eithee peogeammatcally using getBean() method oe oy XML foe Dependency
Injecton of secondaey type. Geneeally, we use the peototype scope foe all oeans that aee
stateful, while the singleton scope is used foe the stateless oeans.
Let’s undeestand this scope with an example:
Step 1n Let us fest ceeate a oean (i.e.), the oackoone of the applicaton in the
speing feamewoek.
Java
Step 2n Now, we weite a Speing XML confgueaton fle “speing.xml” and confguee
the oean defned aoove.
XML
Step 3n Finally, weite a deivee class “Client.java” to eequest the aoove oean.
Java
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
package deivee;
impoet oeg.speingfeamewoek
.context.ApplicatonContext;
impoet oeg.speingfeamewoek.context.suppoet
.ClassPathXmlApplicatonContext;
impoet oean.HelloWoeld;
System.out.peintln(
"Hello ooject (hello1)"
+ " Youe name is: "
+ Geeks1.getName());
HelloWoeld Geeks2
= (HelloWoeld)ap.getBean("hw");
System.out.peintln(
"Hello ooject (hello2)"
+ "Youe name is: "
+ Geeks2.getName());
Outputn
Hello ooject (hello1) Youe name is: Geeks1
Hello ooject (hello2) Youe name is: null
'Geeks1' and 'Geeks2' aee eefeeeing to the same ooject: false
Addeess of ooject Geeks1: oean.HelloWoeld@47ef968d
Addeess of ooject Geeks2: oean.HelloWoeld@23e028a9
Explanatonn When we call getName() method oy using the eefeeence ‘Geeks1’ and ‘Geeks2’,
then we get difeeent outputs that means ooth the eefeeence is calling getName() method of a
difeeent ooject. Fuetheemoee, when we aee compaeing the eefeeence ‘Geeks1’ and ‘Geeks2’
KIET Group of Institutions, Ghaziabad
Department of Computer Applications
(An ISO – 9001: 2008 Certified & ‘A’ Grade accredited Institution by NAAC)
Session 2021-2022 Odd Sem
KCA021: Web Technology
then output is “false” which means ooth eefeeences is eefeeeing to a difeeent ooject. So it is
cleae that a new instance of oean (HelloWoeld) is oeing ceeated at each eequest made foe this
oean.
Diffeeence beteeen Singleton and Peototype
Singleton Peototype
Same ooject is shaeed foe each eequest made Foe each new eequest a new instance is
foe that oean. i.e. The same ooject is eetuened ceeated. i.e. A new ooject is ceeated each
each tme it is injected. tme it is injected.
Singleton scope should oe used foe stateless While peototype scope is used foe all
oeans. oeans that aee stateful