1 / 23

Aspect Oriented Programming with Spring

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.

gerald
Download Presentation

Aspect Oriented Programming with Spring

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Aspect Oriented Programming with Spring

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

  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.

  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.

  5. 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

  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.

  7. AOP concepts Aspect Join point Advice Pointcut Introduction Target object AOP proxy Weaving

  8. 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

  9. 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

  10. 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

  11. 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).

  12. AOP Concepts: Weaving Process of actually inserting aspects into the application code at the appropriate point Types of Weaving Compile time weaving Runtime weaving

  13. AOP Concepts: Target An object whose execution flow is modified by some AOP process They are sometimes called advised object

  14. 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

  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.

  16. 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

  17. 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

  18. HelloWorld Spring AOP

  19. MessageWriter Class We want to display “Hello World !” through AOP publicclass MessageWriter { publicvoid writeMessage() { System.out.print("World"); } }

  20. Target The joinpoint is the invocation of the writeMessage() method What we need is an “around advice” publicclass MessageWriter{ publicvoid writeMessage() { System.out.print("World"); } }

  21. 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; publicclass 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

  22. Weaving MessageDecorator Advice Use ProxyFactory class to create the proxy of the target object import org.springframework.aop.framework.ProxyFactory; publicstaticvoid 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());

  23. 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

More Related