Aspect Oriented Programming

hopeacceptableΛογισμικό & κατασκευή λογ/κού

28 Οκτ 2013 (πριν από 3 χρόνια και 10 μήνες)

86 εμφανίσεις

Aspect Oriented Programming

By

Rohit Ghatol

&

Loukik

Purohit

What is Aspect Oriented
Programming?

Core Business Logic

Concerns

Caching

Profiling

Problem Statement

Layered Architecture

Database

Service

View

Inventory DAO

Shopping Cart
DAO

Inventory
Service

Shopping
Service

Shopping Cart
Operator

CartView.jsp

Inventory.jsp

Non AOP Solution

Cross Cutting Concerns

Inventory DAO

Shopping Cart
DAO

Inventory
Service

Shopping
Service

Shopping Cart
Operator

Logging

Security

Transactions

Caching

Pooling

AOP Implementations

AspectJ


Compile time changes the
byte code of existing .class
to inject new concerns in
code

Spring AOP


Works with runtime proxies
which extend and wrap and
delegate calls to original
objects

AspectJ

Problem
-
1 Tracing/Logging

New Project

New Person

Debugging Flow

Logging/Tracing

Logging/Tracing

Database

Service

View

Inventory DAO

Shopping Cart
DAO

Inventory
Service

Shopping
Service

Shopping Cart
Operator

CartView.jsp

Inventory.jsp

L

L

L

L

L

L

Problem
-
2 Profiling

Problem
-
3 Caching

Caching to avoid DB hit

Database

Service

View

Inventory DAO

Shopping Cart
DAO

Inventory
Service

Shopping
Service

Shopping Cart
Operator

CartView.jsp

Cache

Cache

Inventory.jsp

Problem
-
4 Connection Pooling

Connection Pooling

Database

Service

View

Inventory DAO

Shopping Cart
DAO

Inventory
Service

Shopping
Service

Shopping Cart
Operator

CartView.jsp

Inventory.jsp

4

4

Problem
-
5 Contracts

Use of
System.out.println

View calling DAO

……….

Problem
-
6 Adding to existing
Classes

Item

String
getID
();

String
getName
();

String
getDescription
();

b
oolean

equals();

Int

hashcode
();

Item clone();

String
toString
();

Extending without
changing code

Referred as “Introduction” or ITD

AspectJ

using STS

Download from
http://www.springsource.com/devel
oper/
sts


HelloWorld

AspectJ

Code Example

package
com.test
;


/**


*
@author
rohit


*


*/

public class
Helloworld

{



public static void main(String
args
[]){



Helloworld

helloworld

=
new
Helloworld
();



helloworld.hello
();


}




public void hello(){



System.
out.print
("Hello");


}

}

HelloWorld.java

package
com.test.aspects
;


/**


*
@author
rohit


*


*/

public aspect
WorldAspect

{



pointcut

world() :



call( public void
com.test.Helloworld.hello
()) ;




before(): world(){




System.
out.print
("
--
> ");


}


after() : world() {



System.
out.print
(" World!");


}







}

WorldAspect.java

Decorator.java

ackage

com.test.aspects
;


/**


*
@author
rohit


*


*/

public aspect Decorator {


declare precedence: Decorator,
WorldAspect
;


pointcut

world() :



call(* *.hello(..))



&& !
within(Decorator)



&& !
within(
WorldAspect
)



;




void around(): world(){



System.out.print
("**** ");



proceed();



System.out.print
(" ****");


}

}

****
--
> Hello World! ****

Output of Program

AspectJ

Process

Main Java Source

.Classes

Aspects Source

AspectJ

Compiler

.Classes

Business Logic with
Concerns now
weaved in

Business Logic

Different Types of Aspect

Dynamic Cross Cutting

Static Cross Cutting

Dynamic Cross Cutting

Join Points

Join Points

Join Points

Point
Cut

Advice

Flow Example

InventoryService

Item
getItem
(long id)

InventoryDAO

Item
getItem
(long id)

JoinPoint

PointCut

:execution(
InventoryDAO.ge
tItem
(..))


PointCut

:execution
(*
DAO.getItem
(..)
)


Around Advice

Psuedo

Code

If(item in cache){


return item;

}else{


//fetch from
db
;



proceed();


put item in cache;


return item

}

Static Cross Cutting

Employee

p
rivate String
firstName
;

p
rivate String
lastName
;

p
rivate long
birthDate


//…getters & setters()

Introduction/ITD

Inject equals() &
hashCode
()

Inject persist() &
merge()

Inject implements
Serializable

Static Cross Cutting

Compile Type Declaration

declare warning :
get(*
System.out
)
:


”Use Logging Framework"
;

Target

Caller

Proxy

Pointcut

RUN TIME WEAVING

Only Method
Pointcuts

Advice
2

Advice
1

Pointcut

Spring Proxy Generation





Using
java.lang.reflect.Proxy

Class


Using
the
CGLIB

library

Writing
pointcuts

execution(*
com.mypackage.myClass.method
(..))

Trigger on
method
execution

Any Return
type

Method Declaration

Takes any
args

Springs Advice

Before

Method

Successful
Return

Exception

After
Returning

After
Throwing

Around

AspectJ

Syntax

public aspect
ExampleAspect

{



Map<Long, Object>
userIdMap

=
new
HashMap
<Long, Object>();



//Caching Point Cut


pointcut

daoGetters
(User user):



execution(*
com.test
..*
ShoppingCartDao.get
*(..))



&&
args
(
user
);



Object
around
(User
user
) :
daoGetters
(
user
) {



if

(
userIdMap.containsKey
(
user.getUserId
())) {




System.out.println
(" #Caching: Got
ShoppingCart

from Cache
-

Cheap
");




return

userIdMap.get
(
user.getUserId
());



}
else

{





Object
object

=
proceed
(
user
);




System.out.println
(" #Caching: Got
ShoppingCart

from Database
-

Expensive
");




userIdMap.put
(
user.getUserId
(),
object
);




return

object
;



}


}


//….
Turn

to
next

slide



}

public aspect
ExampleAspect

{



……


//ITD /
Introduction


public
String

ShopItem.
toString
(){



return

"
ShopItem

[
itemId
=" +
getItemId
() + ",
itemName
=" +
getItemName
()



+ ",
itemDescription
=" +
getItemDescription
() + "]";


}




//Compiler
Warnign

and
Errors


declare

warning:get
(*
System.out
):"Do not
use

System.out.println
";



declare

warning

: (
call
(*
com.test
..*Dao.*(..)) &&


!
within
(
com.test
..*Service)):


"
DAOs

should

only

be

called

from Service
layer
";





}

Logging, Tracing & Profiling

Code Example

Pooling
and Caching

Code Example

Spring AOP

Target

Caller

Proxy

Pointcut

RUN TIME WEAVING

Only Method
Pointcuts

Advice
2

Advice
1

Pointcut

Spring Proxy Generation





Using
java.lang.reflect.Proxy

Class


Using
the
CGLIB

library

Writing
pointcuts

execution(*
com.mypackage.myClass.method
(..))

Trigger on
method
execution

Any Return
type

Method Declaration

Takes any
args

Springs Advice

Before

Method

Successful
Return

Exception

After
Returning

After
Throwing

Around

Conclusion

AspectJ


More Powerful


methods,
fields, constructor, aspect,
field weaving


Compile time weaving


works with likes of Android


Learning curve is more


Much better in speed


Needs special compiler

Spring AOP


Less Powerful


only
supports method level
weaving


Runtime weaving strictly
requires Spring


Easier to learn


Much slower than
AspectJ


No such need