aspect oriented programming

26
Aspect Oriented Programming(AOP) 18/10/2013 Rajesh Ganesa rajeshganesan@optiriskindia.

Upload: rajesh-ganesan

Post on 25-May-2015

569 views

Category:

Education


3 download

DESCRIPTION

Introduction to Aspect Oriented Programming

TRANSCRIPT

Page 1: Aspect Oriented Programming

Aspect Oriented Programming(AOP)

18/10/2013

Rajesh [email protected]

Page 2: Aspect Oriented Programming

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.

What is AoP?

Page 3: Aspect Oriented Programming

Centralize concerns implementation More reusable code Cleaner code Write less code Easy to understand More maintainable Less boilerplate code More interesting work

Why AoP?

Page 4: Aspect Oriented Programming

Concerns

Logging

Caching

Pooling

Exception

HandlingProfiling

Transactions

Security

Page 5: Aspect Oriented Programming

Aspects are often described in terms of advice, pointcuts, and join points.

AOP Terminology

Page 6: Aspect Oriented Programming

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.

AOP - Definitions

Page 7: Aspect Oriented Programming

AOP – Definitions

Method Method Method

Logger

Transaction

Manager

Joinpoints

Advice Concern

Concern

Page 8: Aspect Oriented Programming

Before Advice

After Advice

After returning

Advice

Around Advice

Throws Advice

Advice Types

Method

Method

Method

Method

Exception

Page 9: Aspect Oriented Programming

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.

Join Points

Page 10: Aspect Oriented Programming

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

Point Cuts

pointcut move():call(void Line.setP1(Point)) ||call(void Line.setP2(Point));

Page 11: Aspect Oriented Programming

An aspect is the merger of advice and pointcuts. Taken together, advice and point- cuts define everything there is to know about an aspect—what it does and where and when it does it.

Aspects

Page 12: Aspect Oriented Programming

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.

WEAVING

Page 13: Aspect Oriented Programming

AOP - Weaving

CustomerDAOCustomerService

Proxy

saveCustomer(cus)

Logger(Aspect)

Page 14: Aspect Oriented Programming

Example – Tracing without AOP

Page 15: Aspect Oriented Programming

Example – Tracing with AOP

Page 16: Aspect Oriented Programming

AspectJ

AspectWrekz

JBossAOP

Spring AOP

AOP Frameworks

Page 17: Aspect Oriented Programming

Aspect Framework Comparison

Page 18: Aspect Oriented Programming

1. Spring AOP (using XML)

2. Spring AoP ( using AspectJ Annotations)

3. AspectJ ( using aspect class)

– An Logger Example

Demo

Page 19: Aspect Oriented Programming

End of Session

Page 20: Aspect Oriented Programming

Backup Slides

Page 21: Aspect Oriented Programming

Cuts across multiple abstractions Difficult to decompose High-coupling Boilerplate code Code tangling and scattering

◦ Poor traceability◦ Lower productivity◦ Less code reuse◦ Harder refactoring

Problems

Page 22: Aspect Oriented Programming

High level view of AspectJ

Java Program

AspectJ

Advice

pointcutadvice body

join point

Page 23: Aspect Oriented Programming

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)

Specifying Join Points with Designators

Page 24: Aspect Oriented Programming

call(* foo()) call(public bar.*(..)) call(void foo(..)) call(* *(..)) call(*.new(int, int)) handler(File*Exception)

Designators with Wildcards

Page 25: Aspect Oriented Programming

Wildcards in pointcuts

Page 26: Aspect Oriented Programming

Poincut Expression Structure