Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd

Slides:



Advertisements
Similar presentations
AspectWerkz 2 - and the road to AspectJ 5 Jonas Bonér Senior Software Engineer BEA Systems.
Advertisements

Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
1 JAC : Aspect Oriented Programming in Java An article review by Yuval Nir and Limor Lahiani.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development.
Aspect Oriented Programming - AspectJ Radhika Rajput.
Secure Systems Research Group - FAU Aspect Oriented Programming Carlos Oviedo Secure Systems Research Group.
Overview of AspectJ Aspect Oriented Software Development Seminar Technion presented by Oren Mishali.
Spring AOP Aspect Oriented Programming
Aspect-Oriented Programming In Eclipse ® Aspect-Oriented Programming in Eclipse with AspectJ Dr Helen Hawkins and Sian January.
University of British Columbia Software Practices Lab CAS Seminar 06 Fluid AJ - A Simple Fluid AOP Tool Terry Hon Gregor Kiczales.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
More on AspectJ. aspect MoveTracking { private static boolean _flag = false; public static boolean testAndClear() { boolean result = _flag; _flag = false;
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Introduction to AOP.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
Aspect Oriented Programming (AOP) in.NET Brent Krueger 12/20/13.
Spring core v3.x Prepared by: Nhan Le. History v3.0 Spring Expression Language Java based bean metadata v3.1 Cache Abstraction Bean Definition Profile.
Aspect Oriented Programming Razieh Asadi University of Science & Technology Mazandran Babol Aspect Component Based Software Engineering (ACBSE)
Aspect Oriented Programming Scott Nykl CSSE 411 Senior Seminar.
SAMANVITHA RAMAYANAM 18 TH FEBRUARY 2010 CPE 691 LAYERED APPLICATION.
©Ian Sommerville 2006Software Engineering, 8th edition. Chapter 32 Slide 1 Aspect-oriented Software Development 1.
Modularizing Web Services Management with AOP María Agustina Cibrán, Bart Verheecke { Maria.Cibran, System and Software Engineering.
Aspect Oriented Programming Sumathie Sundaresan CS590 :: Summer 2007 June 30, 2007.
Spring Training 17/09/ Dependency Injection.
Aspect Oriented Programming Gülşah KARADUMAN.
Joel Phinney March 31, ◦ Concerns  Separation of Concerns, Tangled and Scattered Concerns, Cross-Cutting Concerns, Aspects ◦ Aspect-Oriented Software.
Methodology: The AOP Refactoring Process Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton Advisor:
A Distributed Aspect-Oriented System for J2EE Applications Muga Nishizawa and Shigeru Chiba (Tokyo Institute of Technology, Japan) Background - As benefits.
AOSD1 Aspect-Oriented Software Design Karl Lieberherr Theo Skotiniotis.
A Presentation By V AIBHAV S AHARAN Web-enHanced Information Management COMS E6125.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
Virtual Support for Dynamic Join Points C. Bockisch, M. Haupt, M. Mezini, K. Ostermann Presented by Itai Sharon
1 An Aspect-Oriented Implementation Method Sérgio Soares CIn – UFPE Orientador: Paulo Borba.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Inter-Type Declarations in AspectJ Awais Rashid Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
AspectJ – AOP for Java Tom Janofsky. Instructor at Penn State Abington Consultant with Chariot Solutions JUG Member.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
Chapter 8: Aspect Oriented Programming Omar Meqdadi SE 3860 Lecture 8 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring AOP – Part 2 Advanced Aspect-Oriented.
Kansas City Java User’s Group Jason W. Bedell July 12, 2006
问题 Code scattering Blocks of duplicated code Blocks of complementary code, and different modules implementing complementary parts of the concern Code.
Comparison of Different AOP Approaches Presented by: Xiaojing Wang.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
AOSD'04, Lancaster, UK 1 Remote Pointcut - A Language Construct for Distributed AOP Muga Nishizawa (Tokyo Tech) Shigeru Chiba (Tokyo Tech) Michiaki Tatsubori.
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1.
Aspect-Oriented Software Development (AOSD)
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
PPL 2004, Gamagori, AICHI1 Remote Pointcut - A Language Construct for Distributed AOP Muga Nishizawa (Tokyo Tech, Japan) Shigeru Chiba (Tokyo Tech, Japan)
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
 Is a programming paradigm  Extends OOP  Enables modularization of cross cutting concerns.
Features of AOP languages AOP languages have the following main elements: –a join point model (JPM) wrt base PL –a specification language for expressing.
Leveraging ColdSpring to build a robust Flex applications Chris Scott, Cynergy Systems.
University of Central Florida COP 3330 Object Oriented Programming
Chengyu Sun California State University, Los Angeles
University of Central Florida COP 3330 Object Oriented Programming
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
By Ru Shen Department of Computer Science UAlbany, 2008
Aspect-Oriented Programming
Aspect-Oriented Programming
Java Programming Language
Chengyu Sun California State University, Los Angeles
JAsCo an Aspect-Oriented approach tailored for
SAMANVITHA RAMAYANAM 18TH FEBRUARY 2010 CPE 691
CS520 Web Programming Spring – Aspect Oriented Programming
Leveraging ColdSpring To Make Better Applications
Aspect Oriented Software Design
Aspect Oriented Programming
Presentation transcript:

Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd Presentation By ANUSHA N Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AGENDA Introduction To AOP AOP Concepts Aspect Joinpoint Advice Pointcuts Target Object Weaving Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd What Is AOP Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns It is often defined as a programming technique that promotes separation of crosscutting concerns with in a software system. concerns : A concern is a particular issue, concept, or area of interest for an application: typically, a goal the application must meet. Adlux Consultancy Services Pvt Ltd

Cross-cutting concerns The systems or concerns that tend to cut across multiple components in a system are referred as Cross-cutting concerns System wide concerns that span multiple modules. Cuts across the typical division of responsibility. Examples such as Transaction Management Security Logging Adlux Consultancy Services Pvt Ltd

Cross Cutting Concerns in OOP Approach OOP creates a coupling between core and crosscutting concerns. This causes Leads to duplicated code. Hard to maintain code. Hard to use code. Cross cutting concerns Withdraw method Deposit method Transaction Management Logging Checking for the Privileged User Actual Withdraw Logic comes here Transaction Management Logging Checking for the Privileged User Actual Deposit Logic comes here Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Public class Account { public void deposit() { // Transaction Management // Logging // Checking for the Privileged User // Actual Deposit Logic comes here } public void withdraw() { // Actual Withdraw Logic comes here AOP calls this kind of logic that cross-cuts the existing business logic as Cross-Cutting Concerns . Adlux Consultancy Services Pvt Ltd

Could we not write these cross cutting concerns as functions and call? Results in code permeating though the system at various places – hard to maintain Harder to express concerns this way You have to modify your code to invoke these concerns requires understanding at each level Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd If we use AOP Spring AOP will take care of calling concerns, we just need to declare about concerns once in configuration file. You can focus on the concerns at one place Easier to add and remove concerns Easier to modify or fine tune concerns Easier to understand Efficient to implement More efficient Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP Terminology Joinpoint A point during the execution of a program, such as the execution of a method or the handling of an exception. We can insert additional logic at Joinpoint's. In Spring AOP, a join point always represents a method execution. Advice Action taken at a particular joinpoint is called Advice. PointCut A declarative condition that identifies for which joinpoint, the advices should fire. Aspect: An aspect is a modularization of a crosscutting concern; the gathering together of code that might otherwise have been scattered. It is termed as the combination of the point-cut and the advice. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP vs OOP Object Oriented Aspect Oriented Class – code unit that encapsulates methods and attributes. Aspect – code unit that encapsulates pointcuts, advice, and attributes. Method signatures – define the entry points for the execution of method bodies. Pointcut – define the set of entry points (triggers) in which advice is executed. Method bodies – implementations of the primary concerns. Advice – implementations of the cross cutting concerns. Compiler – converts source code into object code. Weaver – instruments code (source or object) with advice. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP History Spring implements the AOP Alliance interception interfaces . Emerged about 10 years ago from different research efforts studying the separation of concerns in software Supported in industry today by IBM, BEA, AOP support is available for Java, C++, C, PHP, AspectJ, AspectC++, AspectC, AOPHP, Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice Action taken at a particular joinpoint is called Advice. The following are the different types of advices available in Spring. Before Advice After Advice Throws Advice Around Advice Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice Before Advice : It is used to intercept before the method execution starts. public class Authentication extends BeforeAdvice { public void before(Method method, Object[] args, Object target) throws Throwable if (args[0] instanceof User) User user = (User)args[0]; // Authenticate if he/she is the right user }  } Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice From the above code before() method will be called before the execution of method call. method object represents target method to be invoked. Object[] args refers to the various arguments that are passed on to the method. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice After Advice It will be useful if some logic has to be executed before Returning the Control within a method execution. public class CleanUpOperation implements AfterReturningAdvice {  public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { // Clean up session and user information. } Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice From the above code afterReturning() will be called once the method returns normal execution. If some exception happens in the method execution the afterReturning() method will never be called. Throws Advice : some kind of exception happens during the execution of a method, then to handle the exception properly. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice public class DeleteFile implements ThrowsAdvice { public void afterThrowing(Method method, Object[] args, Object target, IOException exception) String targetFileName = (String)args[2]; // Code to delete the target file. } From the above code afterThrowing() method will be called when an Exception, that too of type IOException is thrown Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice Around Advice : Advice that surrounds a joinpoint such as a method invocation. They are responsible for choosing whether to proceed to the joinpoint executing by returning their own return value or throwing an exception. Intercepts the calls to the target method. this Advice provides finer control whether the target method has to be called or not. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice public class ValidateArguments implements MethodInterceptor { public Object invoke(MethodInvocation invocation) throws Throwable Object arguments [] = invocation.getArguments(); if ((arguments[0] instanceof Parent) && (arguments[1] instanceof Child) ) { Object returnValue = invocation.proceed(); return returnValue; } throw new Exception ("Arguments are of wrong type"); Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Advice From the above code It is important to make a call to MethodInvocation.proceed(), if we are happy with the arguments validation, else the target method will never gets invoked. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd POINTCUTS Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts Point Cuts define where exactly the Advices have to be applied in various Join Points i.e Set of Joinpoints specifying when an advice should fire. Generally they act as Filters for the application of various Advices into the real implementation. Each built-in Pointcuts has an advisor PointcutAdvisor=Pointcut + Advice Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts public interface Pointcut { ClassFilter getClassFilter(); MethodMatcher getMethodMatcher(); } public interface ClassFilter boolean matches(Class clazz); public interface MethodMatcher boolean matches(Method m,Class targetClass); boolean isRuntime(); boolean matches(Method m,Class targetClass,Object[] args); Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts Spring defines two types of Point Cuts namely the Static and Dynamic Point Cuts. Static Pointcuts NameMatchMethod Pointcut Regular Expression Pointcut NameMatchMethod Pointcut : Here the name of the methods that are to be given advices can be directly mentioned in the Configuration File. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts MyInterface.java public interface MyInterface { public void setMessage(String msg); public void method1(); public void method2(); public void getMethod1(); public void getMethod2(); } MyClass.java public class MyClass implements MyInterface { private String message; public void setMessage(String msg) { this.message=msg; public void method1(){} public void method2(){} public void getMethod1() {} public void getMethod2(){} Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts ApplicationContext.xml <beans> <bean id=“getMethodsAdvisor" class="org.springframework. aop. support.NameMatchMethodPointcutAdvisor"> <property name="mappedName"> <value>get*</value> </property> (OR) <property name=“mappedNames”> <list> <value>get*</value> <value>set*</value> <value>method1</value> </list> <property name="advice"> <ref bean=“sampleAdvice "/> </bean> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts <bean id=“myTargetClass" class=“MyClass“/> <bean id=“sampleAdvice" class=“MySampleAdvice“/> <bean id=“proxy“ class=“org.springframework.aop.framework.ProxyFactoryBean“> <property name="proxyInterfaces“> <value>MyInterface</value> </property> <property name="target"> <ref bean=“myTargetClass "/> <property name="interceptorNames"> <value>getMethodsAdvisor </value> </bean> </beans> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts get* tells that all method names starting with the method name get will be given Advices. If we want all the methods in the MyClass to be adviced, then the 'value' tag should be given '*' meaning all the methods in the Class. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts Regular Expression Pointcut It is used if you want to match the name of the methods in the Class based on Regular Expression. Spring distribution already comes with two supported flavors of Regular Expression namely Perl Regular Expression and Jdk Regular Expression . Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts MyInterface.java public interface MyInterface { public void method1(); public void method11(); public void method2(); public void getMethod1(); public void getMethod2(); } MyClass.java public class MyClass implements MyInterface { public void method1(){} public void method11(){} public void method2(){} public void getMethod1(){} public void getMethod2(){} Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts ApplicationContext.xml <bean id="regExpAdvisor“ class="org.springframework.aop. support.RegexpMethodPointcutAdvisor "> <property name=“advice"> <ref bean="sampleAdvice"/> </property> <property name=“patterns”> <list> <value>m.*1</value> <value>.*method.*</value> </list> </bean> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts <bean id=“myTargetClass" class=“MyClass“/> <bean id=“sampleAdvice" class=“MySampleAdvice“/> <bean id="person" class=“org.springframework.aop. framework.ProxyFactoryBean“> <property name=“proxyInterfaces” > <value>MyInterface</value> </property> <property name=“target”> <ref bean=“myTargetClass”/> <property name=“interceptorNames”> <value> regExpAdvisor </value> </bean> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts Symbol Description . Matches Any Single Character + Mathches the Preceding Character one or more times * Mathches the Preceding Character Zero or more times / Escapes any Regular Expression Symbols Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts Dynamic Pointcuts : However, there may be some cases where your pointcuts will need to evaluate runtime attributes. Spring provides one built-in dynamic pointcut: ControlFlowPointcut. This pointcut matches based on information about the current thread’s call stack. That is, it can be configured to return true only if a particular method or class is found in the current thread’s stack of execution. Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts Dynamic Pointcut Configuration <bean id="dynamicPointcut“ class="org.springframework. aop. support.ControlFlowPointcut"> <constructor-arg> <value>javax.servlet.http.HttpServlet</value> </constructor-arg> </bean> <bean id="dynamicAdvisor" class="org.springframework. aop.support.DefaultPointcutAdvisor"> <property name="advice"> <ref bean=" sampleAdvice "/> </property> <property name="pointcut"> <ref bean="dynamicPointcut"/> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Pointcuts <bean id=“myTargetClass" class=“MyClass“/> <bean id=“sampleAdvice" class=“MySampleAdvice“/> <bean id="person" class=“org.springframework.aop. framework.ProxyFactoryBean“> <property name=“proxyInterfaces” > <value>MyInterface</value> </property> <property name=“target”> <ref bean=“myTargetClass”/> <property name=“interceptorNames”> <value>dynamicAdvisor</value> </bean> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Target Object containing the joinpoint . Also referred to as the advised or proxied object . Example <bean id=“myTargetClass” class=“sample.MyClass”> <property name=“message”> <value>Welcome to Adlux</value> </property> </bean> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd Weaving It 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 joinpoints. different points where weaving can be applied ·  Compile Time ·   Class load Time ·   Runtime Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP Proxy A proxy is the object created after applying advice to the target object. The target object (pre-AOP) & the proxy object (post-AOP) are the same. The object created by the AOP framework that includes the advise. The three properties you will probably use most often are 1. target 2. proxyInterfaces 3. interceptorNames Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP Proxy Sample ProxyFactoryBean Configuration <bean id=“sampleProxy" class=“org.springframework.aop. framework.ProxyFactoryBean“> <property name="proxyInterfaces“> <value>SampleInterface</value> </property> <property name="target"> <ref bean="sampleTarget"/> <property name="interceptorNames"> <value>sampleAdvice</value> </bean> <bean id=“sampleTarget" class=“MySampleTarget“/> <bean id=“sampleAdvice" class=“MySampleAdvice“/> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP Proxy Spring will create a proxy using a JDK dynamic proxy or a CGLIB proxy. 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. Different types of AutoProxying ·    DefaultAdvisorAutoProxyCreator ·    BeanNameAutoProxyCreator Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP Proxy BeanNameAutoProxyCreator <bean id=“beanNameAutoProxy” class="org.springframework.aop. framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <list><value>*Service</value></list> </property> <property name="interceptorNames"> <value>performanceThresholdInterceptor</value> </bean> <bean id=“performanceThresholdInterceptor” class=“student.performanceThresholdInterceptor”/> <bean id=“studentService” class=“student.StudentService”/> <bean id=“courseService” class=“student.CourseService”/> <bean id=“sampleService” class=“sample.SampleService”/> Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd AOP Proxy 2. DefaultAdvisorAutoProxyCreator <bean id="advisor" class="org.springframework.aop.support. RegexpMethodPointcutAdvisor"> <property name="advice"> <bean class="performanceThresholdInterceptor"/> </property> <property name="pattern"> <value>.+Service\..+</value> </bean> <bean id="autoProxyCreator” class="org.springframework.aop. framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> <bean id=“studentService” class=“student.StudentService”/> <bean id=“courseService” class=“student.CourseService”/> <bean id=“sampleService” class=“sample.SampleService”/> Adlux Consultancy Services Pvt Ltd

Spring AOP Capabilities Implemented in pure Java Suitable for use in J2EE container since does not need to control the class loader hierarchy Spring supports interception of methods Does not support field interception Provides classes to represent pointcuts and different advise types Spring advises object at instance, rather than class loader level Adlux Consultancy Services Pvt Ltd

Adlux Consultancy Services Pvt Ltd THANK YOU Adlux Consultancy Services Pvt Ltd