SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
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

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() );

Logging method
executions

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

Intercepts method
invocations and
performs logging

DAOs perform
persistence only

Service layer

(method invocation)

AOP interceptor

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

import org.aspectj.lang.annotation.Aspect;

Any bean with a class
annotated as an aspect
will be automatically
detected by Spring

@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

Any public method defined
in the dao package

Any method with a name
beginning with save

Any method defined by the
EventDAO interface with one param

execution( public * *(..) )

execution( public * no.uio.inf5750.dao.*.*(..) )

execution( * save*(..) )

execution( * no.uio.inf5750.dao.EventDAO.*(*) )
Declaring advice
• Implementation of concern represented as an interceptor
• Types
– Before advice
– After advice
– Around advice

Before advice.
Executes before the
matched method.
Declared using the
@Before annotation.

Provides access to the
current join point (target object,
description of advised method, ect. )

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

@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;
}

Declared using the
@Around annotation.

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
<aop:config>
<aop:aspect id=”logging” ref=”loggingInterceptor”>

Aspect declared using
the <aop:aspect>
element. Backing bean
is referenced
with the ref attribute.

</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

<aop:config>
<aop:pointcut id=”daoLayer”
expression="execution( * no.uio.inf5750.dao.*.*(..) )”/>
</aop:config>

Can also be defined
inside aspects
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

Advice is a regular
Java class

public class LoggingInterceptor
{
public void intercept( JoinPoint joinPoint )
{
// Do some useful intercepting work
}
}

Refers to advising method
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

Transaction management
implemented with
around advice

Enters transaction
before method invocation

public interface TransactionManager
{
public void enter();
public void abort();
public void leave();

@Aspect
public interface TransactionInterceptor
{
@Around( ”execution( public no.uio.inf5750.dao.*.*(..) )” ) // In-line pointcut
public void intercept( ProceedingJoinPoint joinPoint )
{
transactionManager.enter();
try
{
joinPoint.proceed();

Aborts and rolls back
transaction if method fails

Leaves transaction if
method completes norm.

}
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

Más contenido relacionado

La actualidad más candente

Java Collections API
Java Collections APIJava Collections API
Java Collections APIAlex Miller
 
Java Annotation Processing: A Beginner Walkthrough
Java Annotation Processing: A Beginner WalkthroughJava Annotation Processing: A Beginner Walkthrough
Java Annotation Processing: A Beginner WalkthroughMahfuz Islam Bhuiyan
 
ObjectLayout: Closing the (last?) inherent C vs. Java speed gap
ObjectLayout: Closing the (last?) inherent C vs. Java speed gapObjectLayout: Closing the (last?) inherent C vs. Java speed gap
ObjectLayout: Closing the (last?) inherent C vs. Java speed gapAzul Systems Inc.
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part VHari Christian
 
Annotation Processing in Android
Annotation Processing in AndroidAnnotation Processing in Android
Annotation Processing in Androidemanuelez
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMERAndrey Karpov
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpaStaples
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IVHari Christian
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VIHari Christian
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenGraham Royce
 
Object.__class__.__dict__ - python object model and friends - with examples
Object.__class__.__dict__ - python object model and friends - with examplesObject.__class__.__dict__ - python object model and friends - with examples
Object.__class__.__dict__ - python object model and friends - with examplesRobert Lujo
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeletonIram Ramrajkar
 

La actualidad más candente (20)

Java 104
Java 104Java 104
Java 104
 
Java Annotations and Pre-processing
Java  Annotations and Pre-processingJava  Annotations and Pre-processing
Java Annotations and Pre-processing
 
Java Collections API
Java Collections APIJava Collections API
Java Collections API
 
L04 Software Design Examples
L04 Software Design ExamplesL04 Software Design Examples
L04 Software Design Examples
 
Java Annotation Processing: A Beginner Walkthrough
Java Annotation Processing: A Beginner WalkthroughJava Annotation Processing: A Beginner Walkthrough
Java Annotation Processing: A Beginner Walkthrough
 
ObjectLayout: Closing the (last?) inherent C vs. Java speed gap
ObjectLayout: Closing the (last?) inherent C vs. Java speed gapObjectLayout: Closing the (last?) inherent C vs. Java speed gap
ObjectLayout: Closing the (last?) inherent C vs. Java speed gap
 
05 Java Language And OOP Part V
05 Java Language And OOP Part V05 Java Language And OOP Part V
05 Java Language And OOP Part V
 
.Net Framework 2 fundamentals
.Net Framework 2 fundamentals.Net Framework 2 fundamentals
.Net Framework 2 fundamentals
 
Annotation Processing in Android
Annotation Processing in AndroidAnnotation Processing in Android
Annotation Processing in Android
 
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMEREVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
EVERYTHING ABOUT STATIC CODE ANALYSIS FOR A JAVA PROGRAMMER
 
Easy data-with-spring-data-jpa
Easy data-with-spring-data-jpaEasy data-with-spring-data-jpa
Easy data-with-spring-data-jpa
 
04 Java Language And OOP Part IV
04 Java Language And OOP Part IV04 Java Language And OOP Part IV
04 Java Language And OOP Part IV
 
06 Java Language And OOP Part VI
06 Java Language And OOP Part VI06 Java Language And OOP Part VI
06 Java Language And OOP Part VI
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
JAXP
JAXPJAXP
JAXP
 
Object.__class__.__dict__ - python object model and friends - with examples
Object.__class__.__dict__ - python object model and friends - with examplesObject.__class__.__dict__ - python object model and friends - with examples
Object.__class__.__dict__ - python object model and friends - with examples
 
Advance Java Programs skeleton
Advance Java Programs skeletonAdvance Java Programs skeleton
Advance Java Programs skeleton
 
Ad java prac sol set
Ad java prac sol setAd java prac sol set
Ad java prac sol set
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 

Similar a Aspect oriented programming with spring

Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_springGuo Albert
 
Introduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingIntroduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingAmir Kost
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop conceptsRushiBShah
 
Code transformation With Spoon
Code transformation With SpoonCode transformation With Spoon
Code transformation With SpoonGérard Paligot
 
Eclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDTEclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDTdeepakazad
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented ProgrammingFernando Almeida
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programmingdaotuan85
 
WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0
WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0
WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0WSO2
 
Context and Dependency Injection 2.0
Context and Dependency Injection 2.0Context and Dependency Injection 2.0
Context and Dependency Injection 2.0Brian S. Paskin
 

Similar a Aspect oriented programming with spring (20)

Aspect oriented programming_with_spring
Aspect oriented programming_with_springAspect oriented programming_with_spring
Aspect oriented programming_with_spring
 
L04 base patterns
L04 base patternsL04 base patterns
L04 base patterns
 
Introduction to Aspect Oriented Programming
Introduction to Aspect Oriented ProgrammingIntroduction to Aspect Oriented Programming
Introduction to Aspect Oriented Programming
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
 
Spring framework part 2
Spring framework  part 2Spring framework  part 2
Spring framework part 2
 
S313937 cdi dochez
S313937 cdi dochezS313937 cdi dochez
S313937 cdi dochez
 
myslide1
myslide1myslide1
myslide1
 
myslide6
myslide6myslide6
myslide6
 
NewSeriesSlideShare
NewSeriesSlideShareNewSeriesSlideShare
NewSeriesSlideShare
 
Struts2 - 101
Struts2 - 101Struts2 - 101
Struts2 - 101
 
Code transformation With Spoon
Code transformation With SpoonCode transformation With Spoon
Code transformation With Spoon
 
Eclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDTEclipse Day India 2011 - Extending JDT
Eclipse Day India 2011 - Extending JDT
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
 
45 aop-programming
45 aop-programming45 aop-programming
45 aop-programming
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0
WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0
WSO2 Product Release Webinar: WSO2 Complex Event Processor 4.0
 
Context and Dependency Injection 2.0
Context and Dependency Injection 2.0Context and Dependency Injection 2.0
Context and Dependency Injection 2.0
 
Annotations Processor Tools (APT)
Annotations Processor Tools (APT)Annotations Processor Tools (APT)
Annotations Processor Tools (APT)
 
L09 Frameworks
L09 FrameworksL09 Frameworks
L09 Frameworks
 

Último

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 

Último (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 

Aspect oriented programming with spring

  • 2. Problem area • How to modularize concerns that span multiple classes and layers? • Examples of cross-cutting concerns: – – – – – Transaction management Logging Profiling Security Internationalisation
  • 3. Logging: A naive approach Retrieving log instance 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() ); Logging method executions 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 ); }
  • 4. 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
  • 5. 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
  • 6. Logging: The AOP approach EventManager Intercepts method invocations and performs logging DAOs perform persistence only Service layer (method invocation) AOP interceptor PersonDAO EventDAO Persistence layer
  • 7. 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
  • 8. 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
  • 10. 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
  • 11. 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
  • 12. @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/>
  • 13. Declaring an aspect • A concern that cuts across multiple classses and layers @Aspect annotation import org.aspectj.lang.annotation.Aspect; Any bean with a class annotated as an aspect will be automatically detected by Spring @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"/>
  • 14. 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() {}
  • 15. 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.
  • 16. Pointcut expression examples Any public method Any public method defined in the dao package Any method with a name beginning with save Any method defined by the EventDAO interface with one param execution( public * *(..) ) execution( public * no.uio.inf5750.dao.*.*(..) ) execution( * save*(..) ) execution( * no.uio.inf5750.dao.EventDAO.*(*) )
  • 17. Declaring advice • Implementation of concern represented as an interceptor • Types – Before advice – After advice – Around advice Before advice. Executes before the matched method. Declared using the @Before annotation. Provides access to the current join point (target object, description of advised method, ect. ) @Aspect public class LoggingInterceptor { @Before( ”no.uio.inf5750.interceptor.LoggingInterceptor.daoLayer()” ) public void intercept( JoinPoint joinPoint ) { log.info( ”Executing ” + joinPoint.getSignature().toShortString() ); }
  • 18. 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() ); }
  • 19. 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. @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; } Declared using the @Around annotation. log.info( ”Successfully executed ” + joinPoint.getSignature().toShortString() ); }
  • 20. 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() ); }
  • 21. 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() ); }
  • 22. 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
  • 23. 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 <aop:config> <aop:aspect id=”logging” ref=”loggingInterceptor”> Aspect declared using the <aop:aspect> element. Backing bean is referenced with the ref attribute. </aop:aspect> </aop:config> <bean id="loggingInterceptor" class="no.uio.inf5750.interceptor.LoggingInterceptor"/> Regular bean definition
  • 24. 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 <aop:config> <aop:pointcut id=”daoLayer” expression="execution( * no.uio.inf5750.dao.*.*(..) )”/> </aop:config> Can also be defined inside aspects
  • 25. 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 Advice is a regular Java class public class LoggingInterceptor { public void intercept( JoinPoint joinPoint ) { // Do some useful intercepting work } } Refers to advising method
  • 26. 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>
  • 27. AOP - Transaction Management TransactionManager interface Transaction management implemented with around advice Enters transaction before method invocation public interface TransactionManager { public void enter(); public void abort(); public void leave(); @Aspect public interface TransactionInterceptor { @Around( ”execution( public no.uio.inf5750.dao.*.*(..) )” ) // In-line pointcut public void intercept( ProceedingJoinPoint joinPoint ) { transactionManager.enter(); try { joinPoint.proceed(); Aborts and rolls back transaction if method fails Leaves transaction if method completes norm. } catch ( Throwable t ) { transactionManager.abort(); throw t; } transactionManager.leave();
  • 28. @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
  • 29. 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
  • 30. References • The Spring reference documentation - Chapter 6 – www.springframework.org • AOP example code – www.ifi.uio.no/INF5750/h07/undervisningsplan.xml