introduction to aspect oriented programming

Post on 16-Apr-2017

513 Views

Category:

Software

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Introduction to Aspect Oriented Programming

By: Amir Kost

Agenda

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

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”

Usages of AOP

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

How does AOP work?

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

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.

AOP – Types of Advice

There are five major types of advice:

• Before• AfterReturn• AfterRunning• AfterThrowing• Around

AOP Libraries - AspectJ

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

•Requires weaving during compile time or load time.

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.

What About Perfomance?

Perfomance Impact - AspectJ

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

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.

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

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

().getName()); }

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

Return Type PackageAdvice

TypeClass Name

Method Name

Method ParamsAspectJ

Keyword

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;

}

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.

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.

AspectJ Weaving Types

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

Using AspectJ

AspectJ can be used in the following ways:

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

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

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

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

javaagent:<path_to>/aspectjweaver.jar

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>

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>

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>

Using AspectJ – Maven – AspectJ Plugin – Cont’d

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

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.

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.

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

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

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.

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

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

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

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

top related