Aspect Oriented Programming. with. Spring

Similar documents
SPRING INTERVIEW QUESTIONS

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

Aspect-Oriented Programming

Model-View-Controller. and. Struts 2

Chapter 5 Aspect Oriented Programming

Web Frameworks and WebWork

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

Glassbox: Open Source and Automated Application Troubleshooting. Ron Bodkin Glassbox Project Leader

Spring Data JDBC Extensions Reference Documentation

The Service Revolution software engineering without programming languages

Tutorial for Spring DAO with JDBC

Beginning POJOs. From Novice to Professional. Brian Sam-Bodden

Unit Testing. and. JUnit

Web Applications and Struts 2

OUR COURSES 19 November All prices are per person in Swedish Krona. Solid Beans AB Kungsgatan Göteborg Sweden

Web Application Access Control with Java SE Security

Overview of Web Services API

Using an Aspect Oriented Layer in SOA for Enterprise Application Integration

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

Encapsulating Crosscutting Concerns in System Software

Aspect-Oriented Web Development in PHP

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

Effective logging practices ease enterprise

EJB 3.0 and IIOP.NET. Table of contents. Stefan Jäger / stefanjaeger@bluewin.ch

Generating Aspect Code from UML Models

Research Article. ISSN (Print) *Corresponding author Lili Wang

Progress Report Aspect Oriented Programming meets Design Patterns. Academic Programme MSc in Advanced Computer Science. Guillermo Antonio Toro Bayona

An Overview of Java. overview-1

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Easy-Cassandra User Guide

Running and Testing Java EE Applications in Embedded Mode with JupEEter Framework

COMPARISON BETWEEN SPRING AND ASP.NET FRAMEWORKS

TABLE OF CONTENTS...2 INTRODUCTION...3 APPLETS AND APPLICATIONS...3 JAVABEANS...4 EXCEPTION HANDLING...5 JAVA DATABASE CONNECTIVITY (JDBC)...

A COMPARISON OF AOP BASED MONITORING TOOLS

Tutorial for Creating Resources in Java - Client

Developing Web Services with Apache CXF and Axis2

Amazon Glacier. Developer Guide API Version

Software Development Kit

How to Model Aspect-Oriented Web Services

Introduction to Java

Where Did My Architecture Go?

How To Implement Lightweight ESOA with Java

CHAPTER 10: WEB SERVICES

Enterprise Application Development In Java with AJAX and ORM

Java Web Services SDK

A Sample OFBiz application implementing remote access via RMI and SOAP Table of contents

Introduction to Web Services

Mail User Agent Project

Web Services Development for IBM WebSphere Application Server V7.0. Version: Demo. Page <<1/10>>

Eclipse 4 RCP application Development COURSE OUTLINE

Installing Java (Windows) and Writing your First Program

CS 111 Classes I 1. Software Organization View to this point:

Object Relational Database Mapping. Alex Boughton Spring 2011

Reference Documentation

Remote Method Invocation in JAVA

New Generation of Software Development

IBM Rational Rapid Developer Components & Web Services

Customer Bank Account Management System Technical Specification Document

PROGRAMMERS GUIDE. Version 1.x

Core Java+ J2EE+Struts+Hibernate+Spring

Decomposition into Parts. Software Engineering, Lecture 4. Data and Function Cohesion. Allocation of Functions and Data. Component Interfaces

Countering The Faults Of Web Scanners Through Byte-code Injection

September 18, Modular development in Magento 2. Igor Miniailo Magento

Services. Custom Tag Libraries. Today. Web Development. Role-Based. Development. Code Reuse. Tag Libraries Custom Tags. Tag Lifecycle.

Smalltalk in Enterprise Applications. ESUG Conference 2010 Barcelona

Module 13 Implementing Java EE Web Services with JAX-WS

An Eclipse Plug-In for Visualizing Java Code Dependencies on Relational Databases

Copyright. Restricted Rights Legend. Trademarks or Service Marks. Copyright 2003 BEA Systems, Inc. All Rights Reserved.

IBM Operational Decision Manager Version 8 Release 5. Getting Started with Business Rules

C++ INTERVIEW QUESTIONS

Security in Domain-Driven Design. Author: Michiel Uithol

e ag u g an L g ter lvin v E ram Neal G g ro va P Ja

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

CQCON 2013: five Sling features you should know

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Web Application Development for the SOA Age Thinking in XML

Dynamic Adaptability of Services in Enterprise JavaBeans Architecture

Building and Using Web Services With JDeveloper 11g

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Java Interview Questions and Answers

Lecture J - Exceptions

Transcription:

Aspect Oriented Programming with Spring

Problem area How to modularize concerns that span multiple classes and layers? Examples of cross-cutting concerns: Transaction management Logging Profiling Security Internationalisation

Logging: A naive approach Retrieving log instance Logging method executions public class HibernateEventDAO private static Log log = LogFactory.getLog( HibernateEventDAO.class ); public Integer saveevent( Event event ) log.info( Executing saveevent( Event ) with argument + event.tostring() ); Session session = sessionmanager.getcurrentsession(); } return (Integer) session.save( event ); public Event getevent( Integer id ) log.info( Executing getevent( int ) ); Session session = sessionmanager.getcurrentsession(); } return (Event) session.get( Event.class, id );

Logging: A naive approach Invokes methods on the DAOs EventManager Service layer (method invocation) DAOs perform both logging and persistence operations PersonDAO EventDAO Persistence layer

Shortcomings of naive approach Mixes persistence and logging functionality Violates the principle of separation of concerns Increases complexity and inter-dependency Involves repetition of code Violates the DRY principle Makes it difficult to change Couples the LogFactory to the HibernateEventDAO Prevents loosely coupled design Makes change, re-use and testing problematic

Logging: The AOP approach EventManager Service layer Intercepts method invocations and performs logging AOP interceptor (method invocation) DAOs perform persistence only PersonDAO EventDAO Persistence layer

Advantages of AOP approach Separates persistence and logging functionality The logging concern taken care of by the interceptor Makes it easier to understand, manage and debug Promotes code reuse and modularization The AOP interceptor is used by all methods in the DAOs Makes it easier to change Decouples the LogFactory from the DAO impl s The HibernateEventDAO is unaware of being logged Makes change, re-use and testing simple

Aspect Oriented Programming Definition: Enables encapsulation of functionality that affects multiple classes in separate units Complements object oriented programming Most popular implementation for Java is AspectJ Aspect oriented extension for Java Based on Eclipse, available as plugin and stand-alone

Spring overview

AOP with Spring The AOP framework is a key component of Spring Provides declarative enterprise services (transactions) Allows for custom aspects Aims at providing integration between AOP and IoC Integrates but doesn t compete with AspectJ Provides two techniques for defining aspects: @AspectJ annotation XML schema-based

AOP concepts Aspect A concern that cuts across multiple classes and layers Join point A method invocation during the execution of a program Advice An implementation of a concern represented as an interceptor Pointcut An expression mapped to a join point

@AspectJ support Style of declaring aspects as regular Java classes with Java 5 annotations Requires aspectjweaver and aspectjrt on the classpath Enabled by including the following information in the Spring configuration file: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <aop:aspectj-autoproxy/>

Declaring an aspect A concern that cuts across multiple classses and layers @Aspect annotation Any bean with a class annotated as an aspect will be automatically detected by Spring import org.aspectj.lang.annotation.aspect; @Aspect public class LoggingInterceptor //... } Regular bean definition pointing to a bean class with the @Aspect annotation <bean id="logginginterceptor" class="no.uio.inf5750.interceptor.logginginterceptor"/>

Declaring a pointcut An expression mapped to a join point (method invocation) AspectJ pointcut expression that determines which method executions to intercept Indicated by the @Pointcut annotation Pointcut signature provided by a regular method definition @Aspect Public class LoggingInterceptor @Pointcut( "execution( * no.uio.inf5750.dao.*.*(..) )" ) private void daolayer() }

Pointcut expression pattern The execution pointcut designator is used most often Pointcut designator. Mandatory. Can use * to match any type. Mandatory. Method name. Can use * to match any type. Mandatory. Exception type. Optional. designator( modifiers return-type declaring-type name(params) throws ) Public, private, etc. Optional. Package and class name. Optional. () = no params. (..) = any nr of params. (*,String) = one of any type, one of String.

Pointcut expression examples Any public method execution( public * *(..) ) Any public method defined in the dao package execution( public * no.uio.inf5750.dao.*.*(..) ) Any method with a name beginning with save execution( * save*(..) ) Any method defined by the EventDAO interface with one param execution( * no.uio.inf5750.dao.eventdao.*(*) )

Declaring advice Implementation of concern represented as an interceptor Types Before advice After advice Around advice Provides access to the current join point (target object, description of advised method, ect. ) Before advice. Executes before the matched method. Declared using the @Before annotation. @Aspect public class LoggingInterceptor @Before( no.uio.inf5750.interceptor.logginginterceptor.daolayer() ) public void intercept( JoinPoint joinpoint ) log.info( Executing + joinpoint.getsignature().toshortstring() ); }

After returning & throwing advice After returning advice. Executes after the matched method has returned normally. Declared using the @AfterReturning annotation. @Aspect public class LoggingInterceptor @AfterReturning( no.uio.inf5750.interceptor.logginginterceptor.daolayer() ) public void intercept( JoinPoint joinpoint ) log.info( Executed successfully + joinpoint.getsignature().toshortstring() ); } After throwing advice. Executes after the matched method has thrown an exception. Declared using @AfterThrowing. @Aspect public class LoggingInterceptor @AfterThrowing( no.uio.inf5750.interceptor.logginginterceptor.daolayer() ) public void intercept( JoinPoint joinpoint ) log.info( Execution failed + joinpoint.getsignature().toshortstring() ); }

Around advice Can do work both before and after the method executes Determines when, how and if the method is executed Around advice. The first parameter must be of type ProceedingJoinPoint calling proceed() causes the target method to execute. Declared using the @Around annotation. @Aspect public class LoggingInterceptor @Around( no.uio.inf5750.interceptor.logginginterceptor.daolayer() ) public void intercept( ProceedingJoinPoint joinpoint ) log.info( Executing + joinpoint.getsignature().toshortstring() ); try joinpoint.proceed(); } catch ( Throwable t ) log.error( t.getmessage() + : + joinpoint.getsignature().toshortstring() ); throw t; } } log.info( Successfully executed + joinpoint.getsignature().toshortstring() );

Accessing arguments The args binding form makes argument values available to the advice body Argument name must correspond with advice method signature Makes the object argument available to the advice body Will restrict matching to methods declaring at least one parameter @Aspect public class LoggingInterceptor @Before( no.uio.inf5750.interceptor.logginginterceptor.daolayer() and + args( object,.. ) ) public void intercept( JoinPoint joinpoint, Object object ) log.info( Executing + joinpoint.getsignature().toshortstring() + with argument + object.tostring() ); }

Accessing return values The returning binding form makes the return value available to the advice body Return value name must correspond with advice method signature Makes the object return value available to the advice body Will restrict matching to methods returning a value of specified type @Aspect public class LoggingInterceptor @AfterReturning( pointcut= no.uio.inf5750.interceptor.logginginterceptor.daolayer(), returning= object ) public void intercept( JoinPoint joinpoint, Object object ) log.info( Executed + joinpoint.getsignature().toshortstring() + with return value + object.tostring() ); }

Schema-based support Lets you define aspects using the aop namespace tags in the Spring configuration file Enabled by importing the Spring aop schema Pointcut expressions and advice types similar to @AspectJ Suitable when: You are unable to use Java 5 Prefer an XML based format You need multiple joinpoints for an advice

Declaring an aspect An aspect is a regular Java object defined as a bean in the Spring context All configuration inside an <aop:config> element Aspect declared using the <aop:aspect> element. Backing bean is referenced with the ref attribute. <aop:config> <aop:aspect id= logging ref= logginginterceptor > </aop:aspect> </aop:config> <bean id="logginginterceptor" class="no.uio.inf5750.interceptor.logginginterceptor"/> Regular bean definition

Declaring a pointcut Pointcut expressions are similar to @AspectJ A pointcut can be shared across advice Pointcut declared inside <aop:config> element using the <aop:pointcut> element Can also be defined inside aspects <aop:config> <aop:pointcut id= daolayer expression="execution( * no.uio.inf5750.dao.*.*(..) ) /> </aop:config>

Declaring advice Before advice. Declared inside an aspect. <aop:aspect id= logging ref= logginginterceptor > <aop:before pointcut-ref= daolayer method= intercept /> </aop:aspect> Refers to pointcut Refers to advising method Advice is a regular Java class public class LoggingInterceptor public void intercept( JoinPoint joinpoint ) // Do some useful intercepting work } }

Declaring advice <aop:aspect id= logging ref= logginginterceptor > After returning advice <aop:after-returning pointcut-ref= daolayer method= intercept /> </aop:aspect> <aop:aspect id= logging ref= logginginterceptor > After throwing advice <aop:after-throwing pointcut-ref= daolayer method= intercept /> </aop:aspect> <aop:aspect id= logging ref= logginginterceptor > Around advice <aop:around pointcut-ref= daolayer method= intercept /> </aop:aspect>

AOP - Transaction Management TransactionManager interface public interface TransactionManager public void enter(); public void abort(); public void leave(); Transaction management implemented with around advice Enters transaction before method invocation Aborts and rolls back transaction if method fails Leaves transaction if method completes norm. @Aspect public interface TransactionInterceptor @Around( execution( public no.uio.inf5750.dao.*.*(..) ) ) // In-line pointcut public void intercept( ProceedingJoinPoint joinpoint ) transactionmanager.enter(); try joinpoint.proceed(); } catch ( Throwable t ) transactionmanager.abort(); throw t; } transactionmanager.leave();

@AspectJ or Schema-based? Advantages of schema style Can be used with any JDK level Clearer which aspects are present in the system Advantages of @AspectJ style One single unit where information is encapsulated for an aspect Can be understood by AspectJ easy to migrate later

Summary Key components in AOP are aspect, pointcut, join point, and advice AOP lets you encapsulate functionality that affects multiple classes in an interceptor Advantages of AOP: Promotes separation of concern Promotes code reuse and modularization Promotes loosely coupled design

References The Spring reference documentation - Chapter 6 www.springframework.org AOP example code www.ifi.uio.no/inf5750/h07/undervisningsplan.xml