AOP crashcourse 2007

289 views

Published on

Short introduction to aspect-oriented programming.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
289
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
7
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

AOP crashcourse 2007

  1. 1. Aspect-OrientedProgrammingBjörn GranvikAOPIn 10 minutes
  2. 2. AspectJ is:An AOP extension to JavaJava platform compatibleOpen Source (Mozilla style)C -> C++ Java -> AspectJA general AO langauge
  3. 3. AOP: Crosscut”Crosscutting concerns” System wide areas of interestFor instance: Security Transactions Caching …
  4. 4. Tomcat: XML parsingGood modularity
  5. 5. Tomcat: URL Pattern matchingStill nice fit using inheritence
  6. 6. Tomcat: LoggingLogging all over the code!aspect• Lets make it into a module– an aspect of logging
  7. 7. Language elementsjoin 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
  8. 8. Join PointWell defined point during execution A call to a method Method invocation Class initialization Object instantiationUsed as ”hooks” foraspectsNameBean bean = new NameBean();bean.foo();InvocationCallObject instantiationClass initialization
  9. 9. PointcutCollection of joinpoints that defines where theadvice should be appliedexecution(* foo(..)) or execution(* bar(int))
  10. 10. AdviceAdditional code executed at a specific joinpointVarious types that defines when the adviceshould be executed Before After Around Throws ...Example Log a method call
  11. 11. Simple Example: UML
  12. 12. Simple Examplepointcut 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());
  13. 13. Simple Examplepointcut 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!”);}
  14. 14. Simple Exampleaspect 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!”);}}
  15. 15. Simple Example: thisJoinPointaspect 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 );}}
  16. 16. Simple Example: wildcardaspect 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 );}}
  17. 17. Hello
  18. 18. WorldAspect
  19. 19. HelloWorld!
  20. 20. ProsPowerful new paradigm improving modularityEasier system evolutionLate binding in design decisionsMore code reuseExtension of Java, therefore it runs on existing systems andtoolsIt integrates well with different toolsUsed in good frameworks such as Spring
  21. 21. ConsMore 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.

×