Upload
do-minh-toan
View
239
Download
0
Embed Size (px)
Citation preview
8/18/2019 Aop Asjectj
1/39
Aspect OrientedProgramingAspectJ & Spring AOP
present by toan.do
8/18/2019 Aop Asjectj
2/39
Agenda• Aspect Orient Programing
•
AspectJ
• Java Dynamic Programing• Bytecode
• Class Loader
•
Java Refection• Dynamic Proy
• Spring AOP• !"L Base
• AspectJ Annotation
• AOP #amespace
• AOP Alliance
8/18/2019 Aop Asjectj
3/39
Cross$c%tting concern
. Concern
. Cross$c%tting concern
. A concern is a specic re'%irement or consideration t(at m%st addressed in order to satis)y t(e overall system goal.
. Cross-cutting concerns are aspects o) a program t(at a*ectconcerns
. A system$+ide concerns t(at span m%ltiple mod%les are called
ting concerns.
8/18/2019 Aop Asjectj
4/39
Cross$c%tting concern
8/18/2019 Aop Asjectj
5/39
"od%lari,ation
8/18/2019 Aop Asjectj
6/39
Cross$c%tting problems
. Code tangling
. Code scattering
Poor traceability- lo+er prod%ctivity- lo+er code re%se- poor '%ality- aevol%tion.
8/18/2019 Aop Asjectj
7/39
Aspect oriented programing
. Decomposite core concerns +it( cross$c%tting c
. Red%ce code tangling and code scattering
8/18/2019 Aop Asjectj
8/39
Aspect oriented programing
8/18/2019 Aop Asjectj
9/39
•
Dynamic Java Proy• (e bytecode manip%lation library AS"
• JBoss Javassist
• AspectJ
• Spring AOP/proies
• Java 00 interceptors
(ttp1//+++.2avacodegee3s.com/4566/57/practical$introd%ctcode.(tml
http://asm.ow2.org/http://asm.ow2.org/
8/18/2019 Aop Asjectj
10/39
AOP Lang%age
. AOP lang%age specication. Constr%cts
. Synta
. AOP lang%age implementation
. 8eri)y code
. ranslate t(e code to eec%table )orm
8/18/2019 Aop Asjectj
11/39
AOP Lang%age
8/18/2019 Aop Asjectj
12/39
Benets
. Cleaner responsibilities o) t(e individ%al mod%le
. 9ig(er mod%lari,ation
. 0asier system evol%tion
. Late binding o) design decisions
. "ore code re%se
. Red%ced costs o) )eat%re implementation
8/18/2019 Aop Asjectj
13/39
AspectJ
. 0tension Java programing lang%age
. Lang%age specication & Lang%age implementa
. AspectJ Complier
8/18/2019 Aop Asjectj
14/39
8/18/2019 Aop Asjectj
15/39
AOP terminologies
. Join point
. Aspect
. Point C%t
. Advice
. :ntrod%ction
8/18/2019 Aop Asjectj
16/39
Join point
A 2oin point is +ell$dened point in t(e application at +(iccan insert additional logic
. "et(od
. Constr%ctor
. Property
8/18/2019 Aop Asjectj
17/39
Advice
(e code t(at is eec%ted at a partic%lar 2oin poinadvice
. Be)ore
. A)ter
. A)ter Ret%rning
. A)ter (ro+
. Aro%nd
8/18/2019 Aop Asjectj
18/39
Point C%tPoint c%t is a collection o) 2oin points t(at +e %se to dene +(en advice s(oeec%ted.
. "et(od eec%tion
. "et(od call
. Constr%ctor eec%tion
. Constr%ctor call
. Class initiali,ation
. ;ield +rite access
. ;ield read access
. 0ception (ander eec%tion
. Ob2ect initiali,ation
. Ob2ect pre$initiali,ation
. Advice eec%tion
8/18/2019 Aop Asjectj
19/39
Aspect
An aspect is t(e combination o) advices and point
8/18/2019 Aop Asjectj
20/39
8/18/2019 Aop Asjectj
21/39
Re)erences
•
(ttp1//eclipse.org/aspect2/• AspectJ in Action
• (ttp1//+++.r%ntime$verication.org/co%rse57/lect%re6/'%ic3.pd)
8/18/2019 Aop Asjectj
22/39
0ample aspect in AspectJAspectJ Compiler
Concepts 1 aspect- point c%t- advice
8/18/2019 Aop Asjectj
23/39
Point C%t
. (at is a +eaving r%les t(at is collected 2oin poin
8/18/2019 Aop Asjectj
24/39
Point C%t
. denotes any s%bclass or s%binter)ace o) a give. Operators ? ecepting t(ose specied t(ings
@@ matc(ing eit(er o) t(e pointc%ts
&& matc(ing bot( t(e pointc%ts.
8/18/2019 Aop Asjectj
25/39
ype Signat%re
Acco%nt
ype o) name Acco%nt.
=Acco%nt
ypes +it( a name ending +it( Acco%nt s%c( as SavingsAccoC(ec3ingAcco%nt.
2ava.=.Date ype
Date in any o) t(e direct s%bpac3ages o) t(e 2ava pac3age- s 2ava.%til.Date and 2ava.s'l.Date.
8/18/2019 Aop Asjectj
26/39
Signat%re "et(odp%blic void Collection.clear
(e met(od clear in t(e Collection class t(at (as p%blic access- ret%rns void- and ta3es
p%blic void Acco%nt.debitfoat t(ro+s :ns%cientBalance0ception
(e p%blic met(od debit in t(e Acco%nt class t(at ret%rns void- ta3es a single foat arg%declares
t(at it can t(ro+ :ns%cientBalance0ception.
p%blic void Acco%nt.set==
All p%blic met(ods in t(e Acco%nt class +it( a name starting +it( set and ta3ing a single any type.
p%blic void Acco%nt.=
All p%blic met(ods in t(e Acco%nt class t(at ret%rn void and ta3e no arg%ments.
p%blic = Acco%nt.=
All p%blic met(ods in t(e Acco%nt class t(at ta3e no arg%ments and ret%rn any type.
8/18/2019 Aop Asjectj
27/39
Signat%re "et(odp%blic = Acco%nt.=..
All p%blic met(ods in t(e Acco%nt class ta3ing any n%mber and type o) arg%ments.
Acco%nt.=..
All met(ods in t(e Acco%nt class. (is +ill even matc( met(ods +it( private access.
?p%blic = Acco%nt.=..
All met(ods +it( nonp%blic access in t(e Acco%nt class. (is +ill matc( t(e met(ods +de)a%lt- and protected access.
p%blic static void est.mainStringE args
(e static main met(od o) a est class +it( p%blic access.
Acco%nt>.=..
All met(ods in t(e Acco%nt class or its s%bclasses. (is +ill matc( any ne+ met(od intrAcco%ntFs s%bclasses.
8/18/2019 Aop Asjectj
28/39
Signat%re Constr%ctorp%blic Acco%nt.ne+
A p%blic constr%ctor o) t(e Acco%nt class ta3ing no arg%ments.
p%blic Acco%nt.ne+int
A p%blic constr%ctor o) t(e Acco%nt class ta3ing a single integer arg%ment.
p%blic Acco%nt.ne+..
All p%blic constr%ctors o) t(e Acco%nt class ta3ing any n%mber and type o) arg%ments.
p%blic Acco%nt>.ne+..
Any p%blic constr%ctor o) t(e Acco%nt class or its s%bclasses.
p%blic =Acco%nt.ne+..
Any p%blic constr%ctor o) classes +it( names ending +it( Acco%nt.
p%blic Acco%nt.ne+.. t(ro+s :nvalidAcco%nt#%mber0ception
Any p%blic constr%ctors o) t(e Acco%nt class t(at declare t(ey can t(ro+:nvalidAcco%nt#%mber0ception.
8/18/2019 Aop Asjectj
29/39
Signat%re ;ield
private foat Acco%nt.Gbalance
Private eld Gbalance o) t(e Acco%nt class
Acco%nt.=
All elds o) t(e Acco%nt class regardless o) an access modier- type- or name
?p%blic static = ban3ing..=.=
All nonp%blic static elds o) ban3ing and its direct and indirect s%bpac3ages
p%blic ?nal =.=
#onnal p%blic elds o) any class
8/18/2019 Aop Asjectj
30/39
PointC%t ype
• "et(od call
callp%blic = Acco%nt.=
• "et(od eec%tion
eec%tep%blic = Acco%nt.=
• Constr%ctor call
callp%blic = Acco%nt.ne+
• Constr%ctor eec%tion
eec%tep%blic = Acco%nt.ne+
8/18/2019 Aop Asjectj
31/39
• ;ield Set Access
set;ield Signat%re
• ;ield Het Access
get;ield Signat%re
• 0ception (andler eec%tion
(andlerype signat%re
• Ob2ect initiali,ation
initiali,ationConstr%ctor Signat%re
• Ob2ect pre$initiali,ation
preinitiali,ationConstr%ctor Signat%re
8/18/2019 Aop Asjectj
32/39
t(is - target- args- +it(in-
. t(istype- instance o) ob2ect
. targettype- instance o) ob2ect
. args..
8/18/2019 Aop Asjectj
33/39
Advice
(e code t(at is eec%ted at a partic%lar 2oin poinadvice
. Be)ore
. A)ter
. A)ter Ret%rning
. A)ter (ro+
. Aro%nd
8/18/2019 Aop Asjectj
34/39
Be)ore advice0ec%te code be)ore 2oin points t(at point c%t collect +it( +eaving r%
A)ter advice0ec%te code a)ter t(e 2oin points t(at point c%t collect +it( +eaving
8/18/2019 Aop Asjectj
35/39
A)ter t(ro+
8/18/2019 Aop Asjectj
36/39
A)ter t(ro+0ec%te code +(en 2oin points t(at point c%t collect +it( +eaving r%leeception
DonFt eec%te i) t(e 2oin point donFt t(ro+ eception
Capt%re t(e type o) eception
Aro%nd
8/18/2019 Aop Asjectj
37/39
Aro%nd0ec%te code be)ore and a)ter 2oin points t(at point c%t collect +it( +
process
8/18/2019 Aop Asjectj
38/39
Passing contet into advice
8/18/2019 Aop Asjectj
39/39
:ntrod%ction
:ntrod%ce s%c( members elds- met(ods into spclass