10. AOP and
OOP
1. Aspect – code unit that
encapsulates pointcuts,
advice, and attributes
2. Pointcut – define the set
of entry points (triggers)
in which advice is
executed
3. Advice –
implementation of
cross cutting concern
4. Weaver – construct
code
(source or object) with
1. Class – code unit that
encapsulates methods
and attributes
2. Method signature – define
the entry points for the
execution of method
bodies
3. Method bodies –
implementation of the
business logic concerns
4. Compiler – convert source
code
to object code
AOP OOP
12. Spring
AOP
implemented in pure java
no need for a special compilation
process
supports only method execution join
points
only runtime weaving is available
AOP proxy
JDK dynamic proxy
CGLIB proxy
configuration
@AspectJ annotation-style
Spring XML configuration-style
17. Format of an execution
expression
execution(
modifiers-pattern
returning-type-pattern
declaring-type-pattern
name-pattern(param-
pattern)
throws-pattern
)
18. Simple pointcut
expressions
@Aspect
public class ItemStatusTracker {
@Pointcut("execution(* approve(..))")
public void ifApprove() {}
@Pointcut("execution(* reject(..))")
public void ifReject() {}
@Pointcut("ifApprove() || ifReject()")
public void ifStateChange() {}
}
19. Execution
examples
any public method
execution(public * * (..))"
any method with a name beginning with
"get"
execution(* get*(..))
any method defined by the appropriate
interface
execution(* bank.BankService.*(..))
any method defined in the appropriate
package
execution(* com.epam.pmc.service.*.*(..))
other examples
http://static.springsource.org/spring/docs/3.0.x/spring-fra
20. Advic
e
associated with a pointcut
expression
a simple reference to a named
pointcut
a pointcut expression declared in
place
runs
before
after returning
after throwing
after (finally)
21. Before
advice
@Aspect
public class BankAspect {
@Pointcut("execution(public * * (..))")
public void anyPublicMethod() {}
@Before("anyPublicMethod()")
public void logBefore(JoinPoint joinPoint) {
//to do something
}
}
22. After returning
advice
@Aspect
public class BankAspect {
@AfterReturning( pointcut="executio
n(* get*(..))",
returning="retVal")
public void logAfter(JoinPoint
joinPoint, Object retVal) {
//to do something
}
}
23. After throwing
advice
@Aspect
public class BankAspect {
@AfterThrowing(
pointcut = "execution(* bank..*ServiceImpl.add*(..))",
throwing = "exception")
public void afterThrowing(Exception exception) {
//to do something
}
}
24. After finally
advice
@Aspect
public class BankAspect {
@Pointcut("execution(public * * (..))")
public void anyPublicMethod() {}
@After(value="anyPublicMethod() && args(from, to)")
public void logAfter(JoinPoint jp, String from, String to) {
//to do something
}
}
25. Around
advice
@Aspect
public class BankCacheAspect {
@Around("@annotation(bank.Cached)")
public Object aroundCache(ProceedingJoinPoint joinPoint){
//to do something before
Object retVal = joinPoint.proceed();
//to do something after
}
}
26. Aspect and advice
ordering
order of advice in the same aspect
before
around
after finally
after returning or after throwing
Spring interface for ordering aspects
org.springframework.core.Ordered
Spring annotation
org.springframework.core.annotation.O
rder
31. Introduction behaviors to
bean
@Aspect
public class CalculatorIntroduction {
@DeclareParents(
value = "calculator.ArithmeticCalculatorImpl",
defaultImpl = MaxCalculatorImpl.class)
public MaxCalculator maxCalculator;
@DeclareParents(
value = "calculator.ArithmeticCalculatorImpl",
defaultImpl = MinCalculatorImpl.class)
public MinCalculator minCalculator;
}
32. Introduction states to
bean
@Aspect
public class BankServiceIntroductionAspect {
@DeclareParents(
value="bank.BankServiceImpl",
defaultImpl=DefaultCounterImpl.class)
public Counter mix;
@Before("execution(* get*(..)) &&
this(auditable)")
public void useBusinessService(Counter auditable) {
auditable.increment();
}
}
33. Spring AOP vs
AspectJ
no need for a
special compilation
process
support only
method execution
pointcuts
advise the
execution of
operations on
need AspectJ
compiler or setup
LTW
support all pointcuts
advice all
domain objects
Spring AOP AspectJ
34. @AspectJ vs
XML
has more
opportunities, such
as combine named
pointcuts
encapsulate the
implementation of the
requirement it
addresses in a single
place
can be used with
any JDK level
good choice to
configure
enterprise
services
@AspectJ XML
35. Link
s
Useful links
Wiki: Aspect-oriented programming
http://en.wikipedia.org/wiki/Aspect-oriented_program
ming
Spring Reference
http://static.springsource.org/spring/docs/3.0.x/sp
ring-
framework-reference/html/aop.html
AspectJ home site
http://www.eclipse.org/aspectj/
https://www.jbktutorials.com/spring-aop/types-