aspect oriented programming

Post on 25-May-2015

569 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Introduction to Aspect Oriented Programming

TRANSCRIPT

Aspect Oriented Programming(AOP)

18/10/2013

Rajesh Ganesanrajeshganesan@optiriskindia.com

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?

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

Why AoP?

Concerns

Logging

Caching

Pooling

Exception

HandlingProfiling

Transactions

Security

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

AOP Terminology

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

AOP – Definitions

Method Method Method

Logger

Transaction

Manager

Joinpoints

Advice Concern

Concern

Before Advice

After Advice

After returning

Advice

Around Advice

Throws Advice

Advice Types

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.

Join Points

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

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

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

AOP - Weaving

CustomerDAOCustomerService

Proxy

saveCustomer(cus)

Logger(Aspect)

Example – Tracing without AOP

Example – Tracing with AOP

AspectJ

AspectWrekz

JBossAOP

Spring AOP

AOP Frameworks

Aspect Framework Comparison

1. Spring AOP (using XML)

2. Spring AoP ( using AspectJ Annotations)

3. AspectJ ( using aspect class)

– An Logger Example

Demo

End of Session

Backup Slides

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

High level view of AspectJ

Java Program

AspectJ

Advice

pointcutadvice body

join point

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

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

Designators with Wildcards

Wildcards in pointcuts

Poincut Expression Structure

top related