+ All Categories
Home > Technology > AOP crashcourse 2007

AOP crashcourse 2007

Date post: 29-Jun-2015
Category:
Upload: bjoern-granvik
View: 171 times
Download: 1 times
Share this document with a friend
Description:
Short introduction to aspect-oriented programming.
Popular Tags:
21
Aspect-Oriented Programming Björn Granvik AOP In 10 minutes
Transcript
Page 1: AOP crashcourse 2007

Aspect-Oriented Programming

Björn GranvikAOP

In 10 minutes

Page 2: AOP crashcourse 2007

AspectJ is:

An AOP extension to JavaJava platform compatibleOpen Source (Mozilla style)C -> C++ Java -> AspectJA general AO langauge

Page 3: AOP crashcourse 2007

AOP: Crosscut

”Crosscutting concerns” System wide areas of interest

For instance: Security Transactions Caching …

Page 4: AOP crashcourse 2007

Tomcat: XML parsing

Good modularity

Page 5: AOP crashcourse 2007

Tomcat: URL Pattern matching

Still nice fit using inheritence

Page 6: AOP crashcourse 2007

Tomcat: Logging

Logging all over the code!

aspect

• Lets make it into a module – an aspect of logging

Page 7: AOP crashcourse 2007

Language elements

join point”kodpunkt” The possible ”wheres”. Well defined points in the execution of a program.

pointcut”punktsnitt” A filter picking out interesting join points.

advice”direktiv” Code to be run at join points.

= aspect

Page 8: AOP crashcourse 2007

Join Point

Well defined point during execution A call to a method Method invocation Class initialization Object instantiation

Used as ”hooks” for aspects

NameBean bean = new NameBean(); bean.foo();

Invocation Call

Object instantiation Class initialization

Page 9: AOP crashcourse 2007

Pointcut

Collection of joinpoints that defines where the advice should be applied

execution(* foo(..)) or execution(* bar(int))

Page 10: AOP crashcourse 2007

Advice

Additional code executed at a specific joinpoint

Various types that defines when the advice should be executed Before After Around Throws ...

Example Log a method call

Page 11: AOP crashcourse 2007

Simple Example: UML

Page 12: AOP crashcourse 2007

Simple Example

pointcut moves():calls(void Line.setP1(Point)) ||calls(void Line.setP2(Point)) ||calls(void Point.setX(int)) ||calls(void Point.setY(int)) ||calls(void FigureElement.incrXY());

Page 13: AOP crashcourse 2007

Simple Example

pointcut moves():calls(void Line.setP1(Point)) ||calls(void Line.setP2(Point)) ||calls(void Point.setX(int)) ||calls(void Point.setY(int)) ||calls(void FigureElement.incrXY());

after(): moves() {System.println.out(”Moved!”);

}

Page 14: AOP crashcourse 2007

Simple Example

aspect MoveTracking {pointcut moves():

calls(void Line.setP1(Point)) ||calls(void Line.setP2(Point)) ||calls(void Point.setX(int)) ||calls(void Point.setY(int)) ||calls(void FigureElement.incrXY());

after(): moves() {System.println.out(”Moved!”);

}}

Page 15: AOP crashcourse 2007

Simple Example: thisJoinPoint

aspect MoveTracking {pointcut moves():

calls(void Line.setP1(Point)) ||calls(void Line.setP2(Point)) ||calls(void Point.setX(int)) ||calls(void Point.setY(int)) ||calls(void FigureElement.incrXY());

after(): moves() {System.println.out(”Moved from:”

+ thisJoinPoint );}

}

Page 16: AOP crashcourse 2007

Simple Example: wildcard

aspect MoveTracking {pointcut moves():

calls(void Line.set*(Point)) ||calls(void Point.set*(int)) ||calls(void FigureElement.incrXY());

after(): moves() {System.println.out(”Moved from:”

+ thisJoinPoint );}

}

Page 17: AOP crashcourse 2007

Hello

Page 18: AOP crashcourse 2007

WorldAspect

Page 19: AOP crashcourse 2007

HelloWorld!

Page 20: AOP crashcourse 2007

Pros

Powerful new paradigm improving modularityEasier system evolutionLate binding in design decisionsMore code reuseExtension of Java, therefore it runs on existing systems and toolsIt integrates well with different toolsUsed in good frameworks such as Spring

Page 21: AOP crashcourse 2007

Cons

More to learn? Yes. Use small doses, get experience.Requires new design strategies? Yes.Extension to Java?!

Use AspectWerkzBreaks encapsulation?

Yes, but OOP breaks the technical concerns instead.Start with small and focused aspects.Use available tools.


Recommended