• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Aspect Oriented Programming
 

Aspect Oriented Programming

on

  • 373 views

Introduction to Aspect Oriented Programming

Introduction to Aspect Oriented Programming

Statistics

Views

Total Views
373
Views on SlideShare
373
Embed Views
0

Actions

Likes
0
Downloads
27
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Intercept method callsInject new behaviour
  • Profiling  is a form of dynamic program analysis that measures space (memory) or time complexity of a program, the usage of particular instructions, or frequency and duration of function calls. The most common use of profiling information is to aid program optimization.Pooling  http://docs.oracle.com/javase/tutorial/jndi/ldap/pool.html
  • Join points are places in your code where an aspect can be inserted, for example, before and after a method call, when a method excepts, or when field or property is being accessed.An advice is a piece of code that can be injected at join points to add new behaviour.This is the boilerplate that you had taken out from your code so that you can encapsulate it into an aspect and then inject it in whilst keeping your code pretty.A point cut is a set of join points where an advice can be applied. It can be attribute drive, like the earlier example of ‘GetBookById’ method where we applied the two attributes to the method.Or it can be filter driven, like the second example where we applied the attributes to the class instead and targeted methods that met our criteria.When a point cut is matched, advice can be executed.An aspect contains point cuts and advice, it is to AOP what class is to OOP, whereas classes holds methods and properties, aspects holds point cuts and advice.Weaving is the process of taking the advice and injecting them into the point cuts in the core component and compose the final result.
  • Spring aspects can work with five kinds of advice: Before—The advice functionality takes place before the advised method is invoked. After—The advice functionality takes place after the advised method completes, regardless of the outcome. After-returning—The advice functionality takes place after the advised method successfully completes.  After-throwing—The advice functionality takes place after the advised method throws an exception.  Around—The advice wraps the advised method, providing some functionality before and after the advised method is invoked. Syntax-----------before(param) : pointcut(param) {body}after(param) : pointcut(param) {body}after(param) returning []: pointcut(param) {body}after(param) throwing []: pointcut(param) {body}type around(param) [throws typelist] : pointcut(param) {body}
  • Compile time—Aspects are woven in when the target class is compiled. This requires a special compiler. AspectJ’s weaving compiler weaves aspects this way. Classload time—Aspects are woven in when the target class is loaded into the JVM. This requires a special ClassLoader that enhances that target class’s byte- code before the class is introduced into the application. AspectJ 5’s load-time weaving (LTW) support weaves aspects in this way. Runtime—Aspects are woven in sometime during the execution of the applica- tion. Typically, an AOP container will dynamically generate a proxy object that will delegate to the target object while weaving in the aspects. This is how Spring AOP aspects are woven.
  • http://www.intertech.com/Blog/secrets-of-the-spring-aop-proxy/

Aspect Oriented Programming Aspect Oriented Programming Presentation Transcript

  • 18/10/2013
  •  Aspect oriented programming (AOP) allows us to keep implement different concerns in isolation  Cross-cutting concerns are conceptually separate from (but often embedded directly within) the application’s business logic. Separating these cross-cutting concerns from the business logic is where aspect- oriented programming (AOP) goes to work.  Whereas DI helps you decouple your application objects from each other, AOP helps you decouple cross-cutting concerns from the objects that they affect.
  •         Centralize concerns implementation More reusable code Cleaner code Write less code Easy to understand More maintainable Less boilerplate code More interesting work
  • Caching Profiling
  •  Aspects are often described in terms of advice, pointcuts, and join points.
  •     Advice defines what needs to be applied and when. Jointpoint is where the advice is applied. Pointcut is the combination of different joinpoints where the advice needs to be applied. Aspect is applying the Advice at the pointcuts.
  • Advice Method Joinpoints Logger Method Transaction Manager Concern Method Concern
  •  Before Advice  After Advice  After returning Advice  Around Advice  Throws Advice Method Method Method Method Exception
  •   your application may have thousands of opportunities for advice to be applied. These opportunities are known as join points. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.
  •  Pointcuts help nar- row down the join points advised by an aspect. If advice defines the what and when of aspects, then pointcuts define the where pointcut move(): call(void Line.setP1(Point)) || call(void Line.setP2(Point));
  •  An aspect is the merger of advice and pointcuts. Taken together, advice and pointcuts define everything there is to know about an aspect—what it does and where and when it does it.
  •  Weaving is the process of applying aspects to a target object to create a new proxied object. The aspects are woven into the target object at the specified join points. The weaving can take place at several points in the target object’s lifetime: ◦ Compile time —Aspects are woven in when the target class is compiled. ◦ Classload time —Aspects are woven in when the target class is loaded into the JVM. ◦ Runtime —Aspects are woven in sometime during the execution of the applica- tion. Typically, an AOP container will dynamically generate a proxy object that will delegate to the target object while weaving in the aspects.
  •  saveCustomer(cus) CustomerService Customer DAO Proxy Logger(Aspect)
  •  AspectJ  AspectWrekz  JBossAOP  Spring AOP
  • 1. Spring AOP (using XML) 2. Spring AoP ( using AspectJ Annotations) 3. AspectJ ( using aspect class) – An Logger Example
  •      Cuts across multiple abstractions Difficult to decompose High-coupling Boilerplate code Code tangling and scattering ◦ ◦ ◦ ◦ Poor traceability Lower productivity Less code reuse Harder refactoring
  • AspectJ Advice advice body pointcut join point Java Program
  •          call(method signature) handler(exception name) cflow(join point designator) this(type name) target(type name) within(class name) execution(method signature) get(signature), set(signature) initialization(signature), staticinitialization(type name)
  •       call(* foo()) call(public bar.*(..)) call(void foo(..)) call(* *(..)) call(*.new(int, int)) handler(File*Exception)