AspectJ for Spring Developers

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.
ASTA Aspect Software Testing Assistant Juha Gustafsson, Juha Taina, Jukka Viljamaa University of Helsinki.
1 JAC : Aspect Oriented Programming in Java An article review by Yuval Nir and Limor Lahiani.
Spring, Hibernate and Web Services 13 th September 2014.
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.
Aspect Oriented Programming Written by Michael Beder.
Introduction to EJB INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Address - #22, 1 st Floor, Station View Road, Kodambakkam, Chennai JTech Soft Solutions Website:
A Framework for Smart Proxies and Interceptors in RMI Nuno Santos P. Marques, L. Silva CISUC, University of Coimbra, Portugal
The Spring Framework: A brief introduction to Inversion of Control James Brundege
Introduction to the Spring Framework By: Nigusse A. Duguma Kansas State university Department of Computer Science Nov 20, 2007.
Spring Overview, Application demo -Midhila Paineni 09/23/2011 Spring Overview, Application demo9/8/20151.
Introduction to AOP.
Introduction to Aspect Oriented Programming Presented By: Kotaiah Choudary. Ravipati M.Tech IInd Year. School of Info. Tech.
Bologna, September 2003 Giorgia Lodi Department of Computer Science University of Bologna V.Ghini, F. Panzieri.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
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.
Dependency Injection in Action Jan Västernäs. CADEC2006, DI, Slide 2 Copyright 2006, Callista Enterprise AB Agenda (DI=Dependency Injection) Background.
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.
Spring Framework. Spring Overview Spring is an open source layered Java/J2EE application framework Created by Rod Johnson Based on book “Expert one-on-one.
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.
Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton February 28th, 2003 Advisor: David G. Hannay Client:
Spring Training 17/09/ Dependency Injection.
Methodology: The AOP Refactoring Process Aspect-Oriented Refactoring of the Apache Cocoon Shared-Object Resource Allocation System Jeff Dalton Advisor:
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.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Spring Remoting Simplifying.
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.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma Presentation to the 20 th.
1 Spring Framework April, 2012 Lam Ho Lam To. © 2010 Cisco and/or its affiliates. All rights reserved. Cisco Confidential 2 1.Spring Overview 2.Framework.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring AOP – Part 2 Advanced Aspect-Oriented.
Aspect Oriented Programming Adlux Consultancy Services Pvt Ltd
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.
aspectj tools new and noteworthy Mik Kersten University of British Columbia Adrian Colyer IBM Hursley OOPSLA, October
Introduction to Yan Cui Aspect Oriented Programming by
CSC450 Software Engineering Devon M. Simmonds University of North Carolina, Wilmington 1.
Text 16 de mayo de 2009 Spring Framework Part III. Portable Service Abstractions Buenos Aires, June 2009.
Enterprise JavaBeans: Fundamentals. EJB Fundamentals(c)CDAC(Formerly NCST)2 Contents Introduction Technology Overview EJB Architecture EJB Specification.
AOP with AspectJ Awais Rashid, Steffen Zschaler © Awais Rashid, Steffen Zschaler 2009.
Singleton Academy, Pune. Course syllabus Singleton Academy Pune – Course Syllabus1.
Implementing Distribution and Persistence Aspects with AspectJ WAS CLASS.
L’origine dei mali: le dipendenze tra componenti Stefano Leli 14° Workshop DotNetMarche Venerdì 16 aprile
Creative Commons Attribution- NonCommercial-ShareAlike 2.5 License Sakai Programmer's Café Sakai Montreal CRIM Workshop Introduction to Spring Framework.
Enterprise Library 3.0 Memi Lavi Solution Architect Microsoft Consulting Services Guy Burstein Senior Consultant Advantech – Microsoft Division.
Leveraging ColdSpring to build a robust Flex applications Chris Scott, Cynergy Systems.
J2EE Lecture 6: Spring – IoC and Dependency Injection
Chapter 1 Introduction to Computers, Programs, and Java
Chengyu Sun California State University, Los Angeles
Aspect-Oriented Programming with the Eclipse AspectJ plug-in
By Ru Shen Department of Computer Science UAlbany, 2008
ЕВОЛУЦИЈА СОФТВЕРА ПОДРЖАНА АСПЕКТНО-ОРИЈЕНТИСАНИМ ПРОГРАМИРАЊЕМ
Chengyu Sun California State University, Los Angeles
Introduction to Spring Framework and Dependency Injection
Developing and testing enterprise Java applications
CS520 Web Programming Spring – Aspect Oriented Programming
Leveraging ColdSpring To Make Better Applications
Java Remote Method Invocation
Aspect Oriented Programming
Presentation transcript:

AspectJ for Spring Developers March 2006 AspectJ for Spring Developers Ramnivas Laddad Principal, Interface21 Author, AspectJ in Action ramnivas.laddad@interface21.com March 2006 Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Speaker introduction Author of books and articles AspectJ in Action: Practical Aspect-Oriented Programming Several articles on IBM developerWorks, TheServerSide, JavaWorld and other publications Consultant and trainer specializing in aspect-oriented programming and enterprise Java Speaker at many professional conferences No Fluff Just Stuff, JavaOne, JavaPolis, Software Development, EclipseCon, O’Reilly OSCON etc. Active involvement in AspectJ since its early form Over a decade of industry experience Java, J2EE, AspectJ, UML, networking, C++, and XML Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

A quick introduction to AOP AspectJ AOP Spring AOP Agenda A quick introduction to AOP AspectJ AOP Spring AOP Spring AspectJ integration Full power AspectJ Demos Q&A Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

A quick intro to AOP Crosscutting concerns AOP Functionality whose implementation spans multiple modules Many examples: Logging and tracing, Transaction management, security, caching, error handling, business rules, performance monitoring… AOP A programming methodology to help with crosscutting concerns Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Core AOP concepts Join point Pointcut Advice Introduction An identifiable point in the execution of a program. Central, distinguishing concept in AOP Pointcut Program construct that selects join points and collects context at those points. Advice Code to be executed at a join point that has been selected by a pointcut Introduction Additional data or method to existing types, implementing new interfaces Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

A Logging Aspect public aspect BankLoggingAspect { private static Logger _logger = Logger.getLogger("banking"); public pointcut loggedOperations() : execution(* banking..*.*(..)) && !within(BankLoggingAspect); before() : loggedOperations() { Signature sig = thisJoinPointStaticPart.getSignature(); _logger.logp(Level.INFO, sig.getDeclaringType().getName(), sig.getName(), "Entering"); } Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Dynamic crosscutting: Advice Code to be executed at a join point that has been selected by a pointcut Three kinds: Before before() : call(* ATM.*(..)) { ... } After after(Account account) returning : accountOperation(account) { Around Object around() : call(* Remote+.*(..) throws RemoteException) { try { ... proceed(); ... Retry logic } catch (...) { Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Static crosscutting Introduce new parent types Introduce new members declare parents: banking..* implements Loggable; Introduce new members public Logger Loggable._logger; Soften exceptions declare soft: SQLException : within(banking.model..*); Compile-time errors and warning declare error : call(* Thread.*(..)) && within(javax.ejb.EnterpriseBean+) : "Use of thread disallowed from EJBs"; Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Spring AOP basics A proxy-based AOP framework JDK proxies CGILIB proxies Targets many J2EE use cases Powerful integration with AspectJ Allowing tapping into full AOP features Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Spring AOP schematic Pointcut Advice Proxy Target Object Advisor Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Configuring through XML: Defining advisor <bean id="spamPreventionAdvisor" class="o.s.aop.support.DefaultPointcutAdvisor"> </bean> <property name="advice"> <bean class="example.SpamPreventionInterceptor"/> </property> <property name="pointcut"> <bean class="o.s.aop.support.JdkRegexpMethodPointcut"> <property name="pattern" value=".*send.*"/> </bean> </property> Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Configuring through XML: Creating proxy <bean id="emailerTarget" class="example.EmailerImpl"> </bean> <bean id="emailer" class="o.s.aop.framework.ProxyFactoryBean"> <property name="target" ref="emailerTarget"/> <property name="interceptorNames"> <list> <value>spamPreventionAdvisor</value> </list> </property> <property name="proxyInterfaces"> <value>example.Emailer</value> Target Advice/Advisor Proxy interface (optional) Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Bean client package example; ... public class Main { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Emailer emailer = (Emailer)context.getBean("emailer"); emailer.send("ramnivas@aspectivity.com", "Hi! Your paypal account..."); "Hi, I have an AspectJ question..."); } Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Advantages of proxy-based approach Requires no special compiler Allow per-object interceptors Not per-class Less full-fledged Only method-level interception Allows easing into AOP Easy to modify applicable interceptors dynamically Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Disadvantage of proxy-based approach Limitation of method-only interception No field-access or object creation Explicit creation of proxy required Can’t use ‘new’ to create objects Calls to ‘self’ don’t go through interceptors Low performance Typically not a huge concern, but beware Over-exposure of context Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Spring-AspectJ integration Provides power of AspectJ in Spring Core message Spring AOP is good enough in many cases But, you often need more power Multiple level of integration Something for every one Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Spring-AspectJ: Levels of integration No special compiler AspectJ pointcut expression With proxy mechanism Typed advice XML-based aspects definition with plain Java classes @AspectJ aspects Special compiler or weaver Full power AspectJ Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

AspectJ pointcut expression AspectJExpressionPointcut Still uses proxy-based mechanism Limitation of method-only interception <bean id="spamPreventor" class="o.s.aop.support.DefaultPointcutAdvisor"> <property name="advice"> <bean class="example.SpamPreventionInterceptor"/> </property> <property name="pointcut"> <bean class="o.s.aop.aspectj.AspectJExpressionPointcut"> <property name="expression" value="execution(* send(..))"/> </bean> Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Typed advice Problem: MethodInterceptor and its cousins take parameters of type Object. Solution: Use AspectJ style type-safe interception Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Typed advice: Defining aspect <beans ...> <aop:config> <aop:aspect id="emailLoggerBean" ref="emailerLogger"> <aop:before pointcut="execution(* send(String, String)) and args(address, *)" method="log"/> </aop:aspect> </aop:config> <bean id="emailerLogger" class="example.EmailLogger"/> ... </beans> Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Plain Java aspect package example; public class EmailLogger { public void log(JoinPoint.StaticPart tjpsp, String address) { System.err.println( "Invoking " + tjpsp.getSignature() + " for " + address); } void before(Method method, Object[] args, Object target) throws Throwable; Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Do away with XML: @AspectJ package example; @Aspect public class EmailLogger { @Before(execution(* send(String, String)) and args(address, *)") public void log(JoinPoint.StaticPart tjpsp, String address) { System.err.println( "Invoking " + tjpsp.getSignature() + " for " + address); } Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Using @AspectJ aspect <beans ...> <aop:autoproxy/> <bean id="emailerLogger" class="example.EmailLogger"/> ... </beans> Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Injecting dependencies into aspects Aspects need services, too Use Spring bean configuration to Use aspect instances as beans Inject dependencies into aspects Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Injecting dependency into aspects <bean id="monitoringAspect" class="com.aspectivity.monitor.JDBCMonitorAspect" factory-method="aspectOf"> <property name="monitorAgent" ref="jmxMonitor"/> </bean> Automatically added methods to all aspects New in Spring 1.2 Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

More power with AspectJ When Spring’s AOP isn’t enough Weaving options Compile-time weaver Aspect and class source code  class files Binary weaver (“linker”) Aspect and class source/binary files  class files Load-time weaver Aspect and class binary files  Weave class files when being loaded into VM Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Demos Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Injecting dependencies using Aspect Limitations of Spring’s “traditional” DI Injection limited to beans created through configuration  Not sufficient for objects created thru other mechanisms: Hibernate, JDO, fine grained objects  Prevents richer domain models prescribed by domain-driven design (DDD) Solution: Use aspects to inject dependencies Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

POJO with dependencies @Configurable("emailerClient") public class OrderProcessor { private Emailer emailer; public void process() { ... emailer.send(...) } public void setEmailer(Emailer emailer) { this.emailer = emailer; Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Dependency configuration <beans...> </beans> <aop:spring-configured/> <bean id="smtpEmailer" .../> <bean id="emailerClient"> <property name="emailer" ref="smtpEmailer"/> </bean> Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Choosing Spring vs. AspectJ AOP Use Spring AOP when Method-only interception is sufficient Full power of AOP overwhelming Performance needs are less stringent Don’t want to use special compiler Domain object’s don’t need to be crosscutted Pre-written aspects meet your needs Use AspectJ AOP when Otherwise… Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Pragmatic usages Use Spring AOP with AspectJ Use AspectJ incrementally Pointcut to use a more expressive selection Typed advice @AspectJ aspects Use AspectJ incrementally Development aspects Refactoring aspects Production aspects Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Summary Spring AOP is simple, yet powerful way to modularize crosscutting concerns AspectJ integration bring full power of AOP in the Spring environment Incremental path from Spring AOP to AspectJ AOP is available The fun has just began… Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

Resources Spring books AspectJ books DI using aspect Pro Spring, by Rob Harrop and Jan Machacek Professional Java Development with the Spring Framework, by Rod Johnson, Rod Johnson, Juergen Hoeller, Alef Arendsen, Thomas Risberg, Colin Sampaleanu Spring in Action, by Craig Walls AspectJ books AspectJ in Action, by Ramnivas Laddad Eclipse AspectJ, by Adrian Colyer, Andy Clement, George Harley, Matthew Webster DI using aspect Adrian Coyler, Dependency injection with AspectJ and Spring http://www.ibm.com/developerworks/java/library/j-aopwork13.html Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

AOP training from the source Core AOP: Simplifying Enterprise Applications with AOP November 7-10, Washington, DC http://interface21.com/training Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)

World-class technical conference for the Spring community Spring Conference: The Spring Experience 2006 December 7th – 10th, Hollywood Florida by Interface21 and NoFluffJustStuff Java Symposiums World-class technical conference for the Spring community 3 full days, 5 concurrent tracks, 60 sessions Core Spring 2.0 Core Enterprise 2.0 Core Web 2.0 Domain Driven Design Just Plain Cool Enjoy five-star beach resort and amenities Converse with core Spring team and industry experts Rod Johnson, Adrian Colyer, Ramnivas Laddad, Juergen Hoeller, etc. Registration opens in July at http://www.thespringexperience.com

Questions? Ramnivas Laddad Email: ramnivas.laddad@intereface21.com Web: http://ramnivas.com Blog: http://ramnivas.com/blog Training and consulting available Copyright © Ramnivas Laddad (ramnivas@aspectivity.com)