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)
Aspect Oriented Programming
Aspect Oriented Programming

Aspect Oriented Programming

Editor's Notes

  • #4 Intercept method callsInject new behaviour
  • #5 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
  • #7 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.
  • #9 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}
  • #13 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.
  • #14 http://www.intertech.com/Blog/secrets-of-the-spring-aop-proxy/