Advances in Aspect-oriented Programming

Similar documents
SCALA AND ASPECTJ. Approaching Modularizing of Crosscutting. Ramnivas Laddad. Concerns. ramnivas

Enterprise AOP With the Spring Framework

What is AOP? Business Logic Requirements Concern Identifier Security Logging (Laddad, 2003, p. 9) What is AOP? Non-AOP implementation of crosscutting

AJDT: Getting started with Aspect-Oriented Programming in Eclipse

Java AOP in Spring 2.0 Rob Harrop, Interface21 Ltd.

AOP 101: Intro to Aspect Oriented Programming. Ernest Hill

Information systems modeling. Tomasz Kubik

Ron Bodkin. New Aspects of Software Colorado Software Summit: October 22 27, 2006

Course 6 7 November Adrian Iftene

Aspect-Oriented Programming

Spring Interview Questions

JAVA COURSES. Empowering Innovation. DN InfoTech Pvt. Ltd. H-151, Sector 63, Noida, UP

Fast Track to Spring 3 and Spring MVC / Web Flow

Programming AspectJ with Eclipse and AJDT, By Example. Chien-Tsun Chen Sep. 21, 2003

Enterprise AOP with Spring Applications IN ACTION SAMPLE CHAPTER. Ramnivas Laddad FOREWORD BY ROD JOHNSON MANNING

Spring & Hibernate. Knowledge of database. And basic Knowledge of web application development. Module 1: Spring Basics

Enterprise Informatization LECTURE

@ASPECTJ BASED AOP WITH SPRING

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

Struts: Struts 1.x. Introduction. Enterprise Application

Introduction to Spring Framework: Hibernate, Web MVC & REST

Enterprise Java Development using JPA, Hibernate and Spring. Srini Penchikala Detroit JUG Developer Day Conference November 14, 2009

Introduction to. Bruno Harbulot. ESNW, the University of Manchester.

This tutorial will take you through simple and practical approaches while learning AOP framework provided by Spring.

Business Logic and Spring Framework

Improve and Expand JavaServer Faces Technology with JBoss Seam

A short introduction to INF329. Spring AOP

SPRING DECLARATIVE TRANSACTION MANAGEMENT

Aspect-oriented programming with AspectJ

Page 1

Improving Software Modularity using AOP

Introduction to Aspect-Oriented Programming

Aspect-Oriented Programming and Aspect-J

Chapitre 6 Programmation orientée aspect (AOP)

Specialized - Mastering Spring 4.2

JML and Aspects: The Benefits of

Call: Core&Advanced Java Springframeworks Course Content:35-40hours Course Outline

com Spring + Spring-MVC + Spring-Boot + Design Pattern + XML + JMS Hibernate + Struts + Web Services = 8000/-

POJOs to the rescue. Easier and faster development with POJOs and lightweight frameworks

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Language support for AOP

The 1st Java professional open source Convention Israel 2006

DYNAMIC PROXY AND CLASSIC SPRING AOP

Dependency Injection and Aspect- Oi Oriented Programming

CHRIS RICHARDSON, CONSULTANT

Groovy & Grails Scripting for Modern Web Applications. Rohit Nayak Talentica Software

Software Project Seminar VII: Tools of the Craft. 23 march 2006 Jevgeni Kabanov

PATTERNS & BEST PRACTICES FOR CDI

Chapter 7. Modular Refactoring. 7.1 Introduction to Modular Refactoring

SPRING MOCK TEST SPRING MOCK TEST I

Introduction to Spring Framework: Hibernate, Spring MVC & REST

Aspect Oriented Programming

Desarrollo de Aplicaciones Web Empresariales con Spring 4

An Aspect-Oriented Approach. Henrique Rebêlo Informatics Center

Fun with AspectJ. 1 Getting Started. 2 Defining Pointcuts. Cleveland State University Electrical and Computer Engineering Distributed: April 8, 2008

ADVANCED JAVA TRAINING IN BANGALORE

injection, objects and aspects

Adapting applications to exploit virtualization management knowledge

A Model for Software Plans

XML SCHEMA BASED AOP WITH SPRING

Language Oriented Modularity: From Theory to Practice

Odysseas Papapetrou and George A. Papadopoulos

An AspectJ-enabled Eclipse Runtime Engine - Demonstration at AOSD 04 - Martin Lippert

JBoss AOP Reference Documentation

Spring Framework 2.5: New and Notable. Ben Alex, Principal Software Engineer, SpringSource

1 Software Architecture

Spring 2.5 on the Way to 3.0

Advanced Web Systems 9- Hibernate annotations, Spring integration, Aspect Oriented Programming. A. Venturini

Spring Certification Questions

Modular Java Applications with Spring, dm Server and OSGi

Analyzing effect of Aspect Oriented concepts in design and implementation of design patterns with case study of Observer Pattern

Take Control with AspectJ

Spring Framework. Christoph Pickl

Using Aspect-Oriented Programming to extend Protégé. Henrik Eriksson Linköping University

Module 8 The Java Persistence API

Spring framework was initially written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

A Brief Introduction to Aspect-Oriented Programming. Historical View Of Languages. Procedural language Functional language Object-Oriented language

Introduction to Web Application Development Using JEE, Frameworks, Web Services and AJAX

Aspect-Oriented Programming

Chapter 32. Aspect-Oriented Software Development (AOSD) Ian Sommerville 2006 Software Engineering. Chapter 32 Slide 1

Integration of Application Business Logic and Business Rules with DSL and AOP

A Brief Introduction to Aspect-Oriented Programming" Historical View Of Languages"

Creating Manageable Systems With JMX, Spring, AOP, and Groovy

Aspect Oriented Programming with AspectJ. Ted Leung Sauria Associates, LLC

Monitoring and Managing Computer Resource Usage on OSGi Frameworks

Spring in Action. Craig Walls. Spring Dallas User Group August 15, 2007 These slides:

purequery Deep Dive Part 2: Data Access Development Dan Galvin Galvin Consulting, Inc.

Software Components and Distributed Systems

Java J Course Outline

AOP Tutorial. Written By: Muhammad Asif. Department of Computer Science, Virtual University of Pakistan

Eclipse MicroProfile: Accelerating the adoption of Java Microservices

Aspect Oriented Programming

Interpreter Implementation of Advice Weaving

Chapter 1 Introducing EJB 1. What is Java EE Introduction to EJB...5 Need of EJB...6 Types of Enterprise Beans...7

Idioms for Building Software Frameworks in AspectJ

InsECTJ: A Generic Instrumentation Framework for Collecting Dynamic Information within Eclipse

AOSA - Betriebssystemkomponenten und der Aspektmoderatoransatz

Application Architectures, Design Patterns

1. Introduction. 2. Technology concepts

Design patterns using Spring and Guice

Transcription:

Advances in Aspect-oriented Programming Ramnivas Laddad Principal, Interface21 Author, AspectJ in Action ramnivas.laddad@interface21.com About Speaker Principal at Interface21 Specializing in aspect-oriented programming and enterprise Java Author of books and articles AspectJ in Action: Practical Aspect-Oriented Programming Several articles on IBM developerworks, TheServerSide, JavaWorld and other publications Speaker at many professional conferences No Fluff Just Stuff, JavaOne, JavaPolis, SpringOne, Software Development, EclipseCon, O Reilly OSCON etc. Active involvement in AspectJ and Spring 1

The state of AOP The power of AOP is clear Writing complex system without AOP in maintainable, understandable manner is impossible AOP is still an emerging technology Only 10 years AspectJ programming model is the de-facto standard Multiple syntax and implementations Adoption is steadily increasing Especially with Spring 2.0 Agenda A quick introduction to AOP Various AOP implementation models Advances in AspectJ Advances in Spring/AOP Advances in usage model The future 2

Agenda A quick introduction to AOP Various AOP implementation models Advances in AspectJ Advances in Spring/AOP Advances in usage model The future A quick intro to AOP Crosscutting concerns 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 3

System Evolution: Conventional Code scattering Code tangling ATM Account System Evolution: AOP based ATM Account Logging aspect Access control aspect 4

Core AOP concepts Join point 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 Inter-type declarations Additional data or method to existing types, implementing new interfaces, attaching new annotations Pointcut Selects join points and collects context at those points Example: pointcut release(connection conn) : call(void Connection.close()) && target(conn); pointcut accountcreation(account account) : execution(account.new()) && this(account); pointcut service() : execution(* Remote+.*(..) throws RemoteException); pointcut accountchangefromatm(atm atm, Account account) : cflow(atmrequest(atm)) && accountchange(account); 5

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 (...) { 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"); 6

Static crosscutting Introduce new parent types 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"; Agenda A quick introduction to AOP Various AOP implementation models Advances in AspectJ Advances in Spring/AOP Advances in usage model The future 7

Understanding various AOP systems AspectJ AOP Byte-code modification based Provides most powerful model Spring AOP is proxy-based Best suited for applying crosscutting concerns to services, DAOs etc. Zero logistical changes to get you started AspectJ AOP schematic Java and aspect source files (.java/.aj) Java and aspect archive files (.jar) AspectJ Weaver (ajc or load-time weaver) Woven system (.class files or.jar file or VM-level byte code) Other possibilities: binary and load-time weaving 8

AspectJ implementation model Business Logic Security Logging Implementation Spring AOP implementation model Pointcut Advice Target Object Proxy Advisor 9

Advantages of proxy-based approach Requires no special compiler Allow per-object interceptors Not per-class Less full-fledged Exposes only method-execution join points Allows easing into AOP Easy to modify applicable interceptors dynamically Limitations of proxy-based approach Method-only interception No field-access or object creation Explicit creation of proxy required Can t use new to create objects Auto-proxy mechanism helps with this Calls to self don t go through interceptors 10

Agenda A quick introduction to AOP Various AOP implementation models Advances in AspectJ Advances in Spring/AOP Advances in usage model The future New @AspectJ syntax @Aspect public class LoggingAspect { private static Logger _logger = Logger.getLogger( tracer"); @Pointcut( execution(* *.* (..)) ) public void loggedoperations() { @Before( loggedoperations() ) public void log(joinpoint.staticpart thisjoinpointstaticpart) { Signature sig = thisjoinpointstaticpart.getsignature(); _logger.logp(level.info, sig.getdeclaringtype().getname(), sig.getname(), "Entering"); 11

XML syntax Base aspect expressed in traditional or @AspectJ syntax public abstract aspect MonitoringAspect { public abstract pointcut monitoredops(); Object around() : monitoredops() { long starttime = System.nanoTime(); Object ret = proceed(); long endtime = System.nanoTime(); monitoragent.record(thisjoinpointstaticpart, endtime-starttime); return ret;... XML syntax: Defining concrete aspect <aspectj> <aspects> <concrete-aspect name="com.aspectivity.monitoring.jdbcmonitoringaspect" extends="com.aspectivity.monitoring.monitoringaspect"> <pointcut name="monitoredops" expression="call(* java.sql.*.*(..))"/> </concrete-aspect>... </aspects>... </aspectj> 12

Load-time weaving Weave while classes are being loaded No explicit compile-time or binary weaving needed Agents supported JVMTI-based Application-server agnostic VMs supported All 5.0+ VMs java -javaagent:aspectjweaver.jar JRockit VM 1.3/1.4 java -Xmanagement:class=org.aspectj.weaver.loadtime.JRockitAgent Classloader-based Application-server specific Not available for all app servers LTW deployment: aop.xml <aspectj> <aspects> <aspect name="com.aspectivity.security.finegrainedsecurity"/> <aspect name="com.aspectivity.tracing.exceptiontracer"/> <concrete-aspect name="com.aspectivity.monitoring.jdbcmonitoringaspect" extends="com.aspectivity.monitoring.monitoringaspect"> <pointcut name="monitoredops" expression="call(java.sql.*.*(..))"/> </concrete-aspect> </aspects> <weaver> <include within="com.interface21..*"/> <exclude within="com.interface21.rss..*/> </weaver> </aspectj> 13

Agenda A quick introduction to AOP Various AOP implementation models Advances in AspectJ Advances in Spring/AOP Advances in usage model The future 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 14

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 AspectJ pointcut expression <bean id="spampreventor" class="o.s.aop.support.defaultpointcutadvisor"> <property name="advice"> <bean class="example.spampreventioninterceptor"/> </property> <property name="pointcut"> <bean class="o.s...aspectjexpressionpointcut"> <property name="expression" value="execution(* send(..))"/> </bean> </property> </bean> 15

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

Do away with XML: @AspectJ package example; @Aspect public class EmailLogger { @Before(execution(* send(string, String)) && args(address, *)") public void log(joinpoint.staticpart tjpsp, String address) { System.err.println( "Invoking " + tjpsp.getsignature() + " for " + address); Using @AspectJ aspect <beans...> <aop:aspectj-autoproxy/> <bean id="emailerlogger" class="example.emaillogger"/>... Other beans... </beans> 17

Choosing Spring vs. AspectJ AOP Use Spring AOP when Method-only interception is sufficient Full power of AOP overwhelming 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 Agenda A quick introduction to AOP Various AOP implementation models Advances in AspectJ Advances in Spring/AOP Advances in usage model The future 18

Layering violation enforcement Problem UI layer often directly use DAO layer contrary to architectural policies No automatic enforcement Solution One simple, reusable aspect to enforce the policies Just add to your build system Layering policy enforcement aspect public aspect LayeringPolicyEnforcementAspect { pointcut dataaccess() : call(* com.mycompany.dao..*.*(..)) call(* org.hibernate..*.*(..)) call(* *..sql..*.*(..)); declare error : dataaccess() && within(com.mycompany.ui..*) : "UI layer shouldn t directly access data; Use business layer instead"; 19

First failure data capture (FFDC) Problem: Exception at low level trickles through layers; however, original context is lost Solution: FFDC aspect logging the exception at the first failure site FFDC: Base aspect public abstract aspect FFDC { public abstract pointcut ffdcop(); after() throwing(exception ex) : ffdcop() { if(_logger.isloggable(level.warning)) { Signature sig = thisjoinpointstaticpart.getsignature(); logffdc(sig.getdeclaringtype().getname(), sig.getname(), ex, thisjoinpoint.getthis(), thisjoinpoint.getargs()); public void logffdc(string classname, String methodname, Exception ex, Object thiz, Object[] args) {... 20

FFDC: Derived aspect public aspect BlogFFDC extends FFDC { public pointcut ffdcop() : execution(* com.mycompany..*.*(..) throws RemoteException) execution(* com.mycompany..dao.*.*(..)); Mix-in implementation Problem: Boilerplate implementation of an implementation Solution: Provide the default implementation using static crosscutting 21

Interface needing boilerplate code public interface ServiceCenter { public String getid(); public void setid(string id); public String getaddress(); public void setaddress(string address); Interface with mixin implementation public interface ServiceCenterMixin extends ServiceCenter { static aspect IMPL { private String ServiceCenterMixin.id; private String ServiceCenterMixin.address; public String ServiceCenterMixin.getId() { return id; public void ServiceCenterMixin.setId(String id) { this.id = id; public String ServiceCenterMixin.getAddress() { return address; public void ServiceCenterMixin.setAddress(String address) { this.address = address; 22

Interface implementations public class ATM extends Teller implements ServiceCenterMixin { Try it yourself:... No set/getid(), set/getaddress()... No id, address public class BrickAndMortar extends Teller implements ServiceCenterMixin {... No set/getid(), set/getaddress()... No id, address Mixin for *Aware interfaces BeanNameAware ApplicationContextAware Example: Sanitizing input data @Aspect public class SanitizeUntrustedData { @Pointcut("execution(* service..*(..)) && args(untrusted,..)") public void untrusteddatausage( Untrusted untrusted) { @Before("untrustedDataUsage(untrusted)") public void sanitize(untrusted untrusted) {... Santitize data 23

Annotation-driven Crosscutting Combines Power of AOP Ease of annotations Offers AOP with ease Core developers supply annotations Aspect developers consume annotations A lot smoother learning curve Transaction management Issue: How to designate methods How to specify transaction attributes Solution: Annotations to designate methods Annotation properties to specify transaction attributes 24

Making a service transactional @Transactional class DefaultAccountService implements AccountService { public void debit(account account, float amount) { // @Transactional(readOnly=true) public float getbalance(account account) { //... Transaction management: XML configuration <?xml version="1.0" encoding="utf-8"?> <beans...> <bean id="accountservice" class="service.defaultaccountservice" /> <bean id="txmanager" class="o.s...datasourcetransactionmanager"> <property name="datasource" ref="datasource" /> </bean> <tx:annotation-driven transaction-manager="txmanager"/> </beans> 25

Data-driven access control Utilize pointcut expression language to select methods based on Annotations to defining type Annotations to methods Annotations to return types Annotations to arguments Data-driven access control @Sensitive public class MedicalRecord {... details 26

Monitoring Sensitive Information Access @Aspect class SensitiveAccessMonitoring { @Pointcut("execution((@Sensitive *) *(..))") public void sensitiveaccess() { @AfterReturning(value="sensitiveAccess()", returning="sensitiveobj") public void monitor(object sensitiveobj) {... Access control logic 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 27

Dependency declaration: Based on template id @Configurable("emailerClient") public class Order { private Emailer emailer; public void process() {... emailer.send(...) public void setemailer(emailer emailer) { this.emailer = emailer; Dependency configuration: Based on template id <beans...> <bean id="smtpemailer".../> <bean id="emailerclient" scope="prototype"> <property name="emailer" ref="smtpemailer"/> </bean> <aop:spring-configured/> </beans> 28

Dependency declaration: Based on template class @Configurable public class Order { private Emailer emailer; public void process() {... emailer.send(...) public void setemailer(emailer emailer) { this.emailer = emailer; Dependency configuration: Based on template class <beans...> <bean id="smtpemailer".../> <bean class="example.order" scope="prototype"> <property name="emailer" ref="smtpemailer"/> </bean> <aop:spring-configured/> </beans> 29

Agenda A quick introduction to AOP Various AOP implementation models Advances in AspectJ Advances in Spring/AOP Advances in usage model The future What s on horizon? Improvements in load-time weaver Spring-native load-time weaver Improvements in tools AOSGi Early version already available VM-level AOP Some prototypes available High-value, high-effort project 30

AOP adoption curve B e n e f i t Risky territory Development aspects Refactoring aspects Infrastructure aspects Time and effort Business logic AO architecture Pragmatic adoption Use Spring/AOP when pre-written aspects meet your needs Use Spring AOP with AspectJ to start tapping into AspectJ s power Use AspectJ incrementally Development aspects Refactoring aspects Production aspects 31

Summary AOP is advancing at pragmatic pace Not too fast Stable core model Not too slow Responsive implementation model AspectJ LTW provides easy integration possibilities Spring/AspectJ integration offers full power of AOP in the Spring environment De-facto for new Spring projects The fun has just began Spring and AOP trainings coming to Philadelphia Core Spring (May 1-4) Core AOP (May 22-25) (Discount code: emergingtech) http://interface21.com/training 32

Questions? Ramnivas Laddad Email: ramnivas.laddad@interface21.com Blog: http://ramnivas.com/blog 33