Escolar Documentos
Profissional Documentos
Cultura Documentos
with
Spring
Problem area
Aspect
! A concern that cuts across multiple classes and layers
:oin point
! A method invocation during the execution of a program
Ad%ice
!
An implementation of a concern represented as an interceptor
Pointcut
!
An expression mapped to a 9oin point
=Aspect: support
"ypes
!
;efore ad%ice
!
After ad%ice
!
Around ad%ice
;efore ad%ice*
Executes before the
matched method*
&eclared using the
=;efore annotation*
=Aspect
public class #ogging$nterceptor
'
"%efore+ .no*uio*inf?G?E*interceptor*#ogging$nterceptor*dao#ayer+,. ,
public %oid intercept+ :oinPoint 9oinPoint ,
'
log*info+ .Executing . / 9oinPoint*getSignature+,*toShortString+, ,-
2
Pro%ides access to the
current 9oin point +target ob9ect3
description of ad%ised method3 ect* ,
After returning J throwing ad%ice
After returning ad%ice*
Executes after the
matched method has
returned normally*
&eclared using the
=After4eturning
annotation*
=Aspect
public class #ogging$nterceptor
'
"#fter&etrning+ .no*uio*inf?G?E*interceptor*#ogging$nterceptor*dao#ayer+,. ,
public %oid intercept+ :oinPoint 9oinPoint ,
'
log*info+ .Executed successfully . / 9oinPoint*getSignature+,*toShortString+, ,-
2
After throwing ad%ice*
Executes after the
matched method has
thrown an exception*
&eclared using
=After"hrowing*
=Aspect
public class #ogging$nterceptor
'
"#fter'hrowing+ .no*uio*inf?G?E*interceptor*#ogging$nterceptor*dao#ayer+,. ,
public %oid intercept+ :oinPoint 9oinPoint ,
'
log*info+ .Execution failed . / 9oinPoint*getSignature+,*toShortString+, ,-
2
Around ad%ice
Around ad%ice*
"he first parameter
must be of type
Proceeding:oinPoint !
calling proceed+, causes
the target method to
execute*
&eclared using the
=Around annotation*
=Aspect
public class #ogging$nterceptor
'
"#rond+ .no*uio*inf?G?E*interceptor*#ogging$nterceptor*dao#ayer+,. ,
public %oid intercept+ Proceeding:oinPoint 9oinPoint ,
'
log*info+ .Executing . / 9oinPoint*getSignature+,*toShortString+, ,-
try
'
9oinPoint*proceed+,-
2
catch + "hrowable t ,
'
log*error+ t*get0essage+, / . . / 9oinPoint*getSignature+,*toShortString+, ,-
throw t-
2
log*info+ .Successfully executed . / 9oinPoint*getSignature+,*toShortString+, ,-
2
Suitable when
!
Lou are unable to use :a%a ?
! Prefer an >0# based format
! Lou need multiple 9oinpoints for an ad%ice
&eclaring an aspect
Ad%antages of AOP
! Promotes separation of concern
! Promotes code reuse and modularization
! Promotes loosely coupled design
4eferences