introduction to aspect oriented programming

36
Introduction to Aspect Oriented Programming By: Amir Kost

Upload: amir-kost

Post on 16-Apr-2017

511 views

Category:

Software


3 download

TRANSCRIPT

Page 1: Introduction to Aspect Oriented Programming

Introduction to Aspect Oriented Programming

By: Amir Kost

Page 2: Introduction to Aspect Oriented Programming

Agenda

•What is Aspect Oriented Programming?•Usage•A short dive to AspectJ•Demos

Page 3: Introduction to Aspect Oriented Programming

What is Aspect Oriented Programming (AOP) ?From Wikipedia:  ”A programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself”

Page 4: Introduction to Aspect Oriented Programming

Usages of AOP

AOP can be used for a multitude of purposes, but the most common are:• Security.• Transactions.• Logging.

Page 5: Introduction to Aspect Oriented Programming

How does AOP work?

AOP works by intercepting methods that comply to certain parameters (pointcuts), and executing a callback wrapper (advice).

Page 6: Introduction to Aspect Oriented Programming

AOP – Basic Terminology

• Joinpoint - a point of execution. • Pointcut – a set of joinpoints.• Advice - Advice brings together a pointcut (to pick out joinpoints) and a body of code.

Page 7: Introduction to Aspect Oriented Programming

AOP – Types of Advice

There are five major types of advice:

• Before• AfterReturn• AfterRunning• AfterThrowing• Around

Page 8: Introduction to Aspect Oriented Programming

AOP Libraries - AspectJ

•Allows interception of all classes and methods, including 3rd party jars.

•Requires weaving during compile time or load time.

Page 9: Introduction to Aspect Oriented Programming

AOP Libraries – Spring AOP

•Has Spring capabilities (IoC). •Allows interception of Spring beans only. •Is proxy based. •Has limited capabilities – no private methods.•Intercepts only methods called from one class to another.

Page 10: Introduction to Aspect Oriented Programming

What About Perfomance?

Page 11: Introduction to Aspect Oriented Programming

Perfomance Impact - AspectJ

•Since AspectJ modifies the bytecode itself through weaving, there is negligible overhead on performance.

Page 12: Introduction to Aspect Oriented Programming

Perfomance Impact – Spring AOP

•Adds a constant delay of a few microseconds for each pointcut.

•Perfomance might be affected if: • Advices are lengthy.• There are several aspects for a single

pointcut.•See the following blog post.

Page 13: Introduction to Aspect Oriented Programming

Example 1 – Before Aspect@Before("call(* org.aspectprogrammer.*.*(..))") public void callFromFoo(JoinPoint joinPoint) {

System.out.println("Call from : " + joinPoint.getSignature

().getName()); }

Page 14: Introduction to Aspect Oriented Programming

Example 1 – Pointcut Analysis@Before("call(* org.aspectprogrammer.*.*(..))")

Return Type PackageAdvice

TypeClass Name

Method Name

Method ParamsAspectJ

Keyword

Page 15: Introduction to Aspect Oriented Programming

Example 2 – Around Aspect@Around("call(* org.aspectprogrammer..*(..))") public Object performance(ProceedingJoinPoint thisJoinPoint) {

// do some business logicObject res = thisJoinPoint.proceed();// do some more business logicreturn res;

}

Page 16: Introduction to Aspect Oriented Programming

A Short Dive into AspectJ

AsepctJ comes in 2 flavors:1. Annotations (like the ones mentioned

above).2. An extentsion of Java, with additional

keywords like pointcut and aspect.

Page 17: Introduction to Aspect Oriented Programming

AspectJ – Under the hood

•AsepctJ uses a process called weaving to identify joinpoints and apply the advices.

•It analyzes and manipulates the bytecode by using ASM and BCEL libraries.

Page 18: Introduction to Aspect Oriented Programming

AspectJ Weaving Types

•Load Time Weaving.•Compile Time Weaving.•Post Compilation Weaving.

Page 19: Introduction to Aspect Oriented Programming

Using AspectJ

AspectJ can be used in the following ways:

1. Command line launcher.2. Maven plugin.3. Via javaagent.

Page 20: Introduction to Aspect Oriented Programming

Using AspectJ – Command Line

1. ajc for compile or post compile weaving.Examples:•ajc HelloWorld.java Trace.java•ajc -inpath app.jar -aspectpath tracelib.jar -outjar tracedapp.jar

Page 21: Introduction to Aspect Oriented Programming

Using AspectJ – Command Line – Load Time Weaving2. aj for load time weaving:•Compile your java code.•Compile your aspects using ajc.•Run your application with aj:

aj com.mycompany.Myclass

Page 22: Introduction to Aspect Oriented Programming

Using AspectJ – Load Time Weaving - Using javaagentCompile your classes and aspects, and add the following option to the JVM:

javaagent:<path_to>/aspectjweaver.jar

Page 23: Introduction to Aspect Oriented Programming

Using AspectJ – Load Time Weaving – configurationIn order to configure to which classes and aspects to weave, write an aop.xml file:

<aspectj> <aspects> <aspect name="com.aspects.MyAspect"/>

</aspects> <weaver options="-showWeaveInfo"> <include within="com.app.MyController"/> <include within="org.3rdparty.beans..*"/> </weaver></aspectj>

Page 24: Introduction to Aspect Oriented Programming

Using AspectJ - Maven

• Import AOP libraries to your pom file:

<dependency><groupId>org.aspectj</groupId><artifactId>aspectjrt</artifactId><version>1.8.7</version>

</dependency><dependency><groupId>org.codehaus.mojo</groupId><artifactId>aspectj-maven-plugin</artifactId><version>1.8</version>

</dependency>

Page 25: Introduction to Aspect Oriented Programming

Using AspectJ – Maven – AspectJ PluginAdd AspectJ weaving plugin to your pom:

<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>aspectj-maven-plugin</artifactId> <version>1.8</version> <executions> <execution> <goals> <goal>compile</goal> </goals></execution> </executions>

</plugin>

Page 26: Introduction to Aspect Oriented Programming

Using AspectJ – Maven – AspectJ Plugin – Cont’d

Note: This plugin is used both for compiling aspects and weaving aspects in CTW.

Page 27: Introduction to Aspect Oriented Programming

Defining Pointcuts using AspectJ AspectJ defines a broad set of keywords for defining pointcuts. Among them:• call, execute – when calling/executing a method.

• get/set – when accessing a field in a class.• this – the currently executing joinpoint is instance of this.

Page 28: Introduction to Aspect Oriented Programming

AspectJ – Inter Type Declaration Besides running advices, AspectJ can do the following:1. Inject a field to a class.2. Inject a method to a class.3. Inject a constructor to a class.4. Inject an interface to a class.

Page 29: Introduction to Aspect Oriented Programming

Other AOP Libraries – Guice AOP • AOP library for Guice framework. • Does not use ApsectJ annotations.• Works with Matchers.

Page 30: Introduction to Aspect Oriented Programming

Other AOP Libraries – Javassist • Bytecode injection library. • Injects code as strings.

Page 31: Introduction to Aspect Oriented Programming

Why did we need AOP in eBay?

• In one of our RESTful web services, we needed to add a certain request param to each error message in the log.

• The classes writing the error messages are not aware of that param.

• We did not want to change all calls to logger api in our modules, or be concerned with future calls.

Page 32: Introduction to Aspect Oriented Programming

Demo 1 – Spring Demohttps://github.com/amirko/spring-app-aop-example

Page 33: Introduction to Aspect Oriented Programming

Demo 2 – AspectJ LTWhttps://github.com/amirko/aop-webapp-ltw.git

Page 34: Introduction to Aspect Oriented Programming

Demo 3 – AspectJ CTWhttps://github.com/amirko/aop-webapp-ctw.git

Page 35: Introduction to Aspect Oriented Programming

Aspects Library for Demos 2 + 3https://github.com/amirko/aspects.git