aspect oriented programming with spring

23
Aspect Oriented Programming with Spring

Upload: gerald

Post on 16-Mar-2016

84 views

Category:

Documents


0 download

DESCRIPTION

Aspect Oriented Programming with Spring. Topics. Introducing Spring AOP Types of AOP AOP in Spring Advisors and Pointcuts in Spring All About Proxies. Introduction. The Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Aspect Oriented Programming with Spring

Aspect Oriented Programming with Spring

Page 2: Aspect Oriented Programming with Spring

2

Topics

• Introducing Spring AOP • Types of AOP • AOP in Spring • Advisors and Pointcuts in Spring • All About Proxies

Page 3: Aspect Oriented Programming with Spring

3

Introduction

• The Spring AOP (Aspect-oriented programming) framework is used to modularize cross-cutting concerns in aspects.

• Put it simple, it’s just an interceptor to intercept some processes, for example, when a method is executed, Spring AOP can hijack the executing method, and add extra functionality before or after the method execution.

Page 4: Aspect Oriented Programming with Spring

4

AOP• Aspect-Oriented Programming (AOP) complements

Object-Oriented Programming (OOP) by providing another way of thinking about program structure.

• key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect.

• Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed crosscutting concerns in AOP literature.)

• One of the key components of Spring is the AOP framework.

• While the Spring IoC container does not depend on AOP, meaning you do not need to use AOP if you don't want to, AOP complements Spring IoC to provide a very capable middleware solution.

Page 5: Aspect Oriented Programming with Spring

Why AOP?

• Aspect-oriented programming (AOP) provides for simplified application of cross-cutting concerns

• Examples of cross-cutting concerns– Logging– Transaction management– Security– Auditing– Locking– Event handling

Page 6: Aspect Oriented Programming with Spring

6

AOP is used in the Spring Framework

• To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management.

• To allow users to implement custom aspects, complementing their use of OOP with AOP.

Page 7: Aspect Oriented Programming with Spring

7

AOP concepts

• Aspect• Join point • Advice • Pointcut • Introduction • Target object• AOP proxy • Weaving

Page 8: Aspect Oriented Programming with Spring

AOP Concepts: Joinpoint

• Well-defined point during the execution of your application

• You can insert additional logic at Joinpoint's• Examples of Jointpoint's

– Method invocation

– Class initialization

– Object initialization

Page 9: Aspect Oriented Programming with Spring

AOP Concepts: Advice

• The code that is executed at a particular joinpoint

• Types of Advice– before advice, which executes before joinpoint– after advice, which executes after joinpoint– around advice, which executes around joinpoint

Page 10: Aspect Oriented Programming with Spring

AOP Concepts: Pointcuts

• A collection of joinpoints that you use to define when advice should be executed

• By creating pointcuts, you gain fine-grained control over how you apply advice to the components

• Example– A typical joinpoint is a method invocation.

– A typical pointcut is a collection of all method invocations in a particular class

• Pointcuts can be composed in complex relationships to further constrain when advice is executed

Page 11: Aspect Oriented Programming with Spring

AOP Concepts: Aspects

• An aspect is the combination of advice and pointcuts

• It is a modularization of a concern that cuts across multiple classes.

• Transaction management is a good example of a crosscutting concern in J2EE applications.

• In Spring AOP, aspects are implemented using regular classes or regular classes annotated with the @Aspect annotation (the @AspectJ style).

Page 12: Aspect Oriented Programming with Spring

AOP Concepts: Weaving

• Process of actually inserting aspects into the application code at the appropriate point

• Types of Weaving– Compile time weaving– Runtime weaving

Page 13: Aspect Oriented Programming with Spring

AOP Concepts: Target

• An object whose execution flow is modified by some AOP process

• They are sometimes called advised object

Page 14: Aspect Oriented Programming with Spring

AOP Concepts: Introduction

• Process by which you can modify the structure of an object by introducing additional methods or fields to it

• You use the Introduction to make any object implement a specific interface without needing the object's class to implement that interface explicitly

Page 15: Aspect Oriented Programming with Spring

15

Spring AOP supports four types of advices

• Before advice – Run before the method execution • After returning advice – Run after the method returns a

result • After throwing advice – Run after the method throws an

exception • Around advice – Run around the method execution,

combine all three advices above.

Page 16: Aspect Oriented Programming with Spring

Types of AOP

• Static AOP– The weaving process forms another step in the build process for an

application

– Example: In Java program, you can achieve the weaving process by modifying the actual bytecode of the application changing and modifying code as necessary

• Dynamic AOP– The weaving process is performed dynamically at runtime– Easy to change the weaving process without recompilation

Page 17: Aspect Oriented Programming with Spring

Spring AOP

• Based on proxies– When you want to create an advised instance of

a class, you must use the ProxyFactory class to create a proxy of an instance of that class, first providing the ProxyFactory with all the aspects that you want to be woven into the proxy

– You typically use ProxyFactoryBean class to provide declarative proxy creation

Page 18: Aspect Oriented Programming with Spring

HelloWorld Spring AOPHelloWorld Spring AOP

Page 19: Aspect Oriented Programming with Spring

MessageWriter Class

• We want to display “Hello World !” through AOP

public class MessageWriter { public void writeMessage() { System.out.print("World"); }

}

Page 20: Aspect Oriented Programming with Spring

Target

• The joinpoint is the invocation of the writeMessage() method

• What we need is an “around advice”

public class MessageWriter{ public void writeMessage() { System.out.print("World"); }

}

Page 21: Aspect Oriented Programming with Spring

Around Advice

• MethodInterceptor is AOP Alliance standard interface for around interface• MethodInvocation object represents the method invocation that is being

advised

import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MessageDecorator implements MethodInterceptor {

public Object invoke(MethodInvocation invocation) throws Throwable { System.out.print("Hello "); Object retVal = invocation.proceed(); System.out.println("!"); return retVal; }

}

To compile this class, you have to add aopalliance-1.0.jar in classpath

Page 22: Aspect Oriented Programming with Spring

Weaving MessageDecorator Advice

• Use ProxyFactory class to create the proxy of the target object

import org.springframework.aop.framework.ProxyFactory; public static void main(String[] args) { MessageWriter target = new MessageWriter(); // create the proxy ProxyFactory pf = new ProxyFactory();

//Add the given AOP Alliance advice to the tail //of the advice (interceptor) chain pf.addAdvice(new MessageDecorator());

Page 23: Aspect Oriented Programming with Spring

Weaving MessageDecorator Advice

//Set the given object as target pf.setTarget(target);

//Create a new proxy according to the //settings in this factory MessageWriter proxy = (MessageWriter)

pf.getProxy(); // write the messages target.writeMessage(); System.out.println(""); // use the proxy proxy.writeMessage(); }}

For runtime support you have to Add cglib-nodep-2.2.jar file in classpath