AOP crashcourse 2007

Post on 29-Jun-2015

171 views 1 download

Tags:

description

Short introduction to aspect-oriented programming.

transcript

Aspect-Oriented Programming

Björn GranvikAOP

In 10 minutes

AspectJ is:

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

AOP: Crosscut

”Crosscutting concerns” System wide areas of interest

For instance: Security Transactions Caching …

Tomcat: XML parsing

Good modularity

Tomcat: URL Pattern matching

Still nice fit using inheritence

Tomcat: Logging

Logging all over the code!

aspect

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

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

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

Pointcut

Collection of joinpoints that defines where the advice should be applied

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

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

Simple Example: UML

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());

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!”);

}

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!”);

}}

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 );}

}

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 );}

}

Hello

WorldAspect

HelloWorld!

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

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.