Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
==========
In above approach primary and secondary logics are combined which is not
recommended. Both are tightly coupled.
Using AOP we can achieve loosely coupling between primary and seconday components.
AOP Terminology
================
Aspect
Advice
JoinPoint
PointCut
Target
Weaving
Proxy
Aspect : Secondary Logic Component is called as Aspect. Aspect represents only name
of the component but it doesnot how it is going to execute in runtime.
Advice: Advice is the implementation of Aspect. Advice represents how Aspect should
execute in runtime.
1) Before Advice
2) After Returning Advice
3) Around Advice
4) Throws Advice
JoinPoint : It represents where target and aspect logics will be combined. (Spring
AOP supports only Method Level JoinPoint).
PointCut: Pointcut defines for which methods of target advice logic should be
applied.
Weaving: The process of integrating target and aspect at join point is called as
weaving.
1) Programmatic approach
3) Annotation approach
1) If we want to execute seconday logic before primary logic then we can go for
Before advice.
3) We can modify target class method parameter values in before advice. They will
reflect in target cls method execution.
4) If exception occured in before advice then controll will not go to target class
logic.
5) Once target cls method execution completed, controll will not come back to
before advice.
-> We can watch target method return value but we can't change it.
-> We don't have any control over target cls method execution because after target
method completed control coming to after-returning-advice.
Use cases : logging, method execution time calculation, user session time
calculation etc.
-> Inside invoke() method we should call proceed() method to give control to target
cls method.
Note: If we don't call proceed() then target method will not be executed.
-> proceed method return type is Object which represents target class method return
value.
-> In Around Advice, we can modify target method parameters and return value also.
Throws Advice
--------------
To execute seconday logic when exception occurs in target class methods then we can
go for Throws Advice.
Note: Throws Advice not for handling exceptions. It is used to monitor the
exception and perform some secondary operation.
Aspect
Advice
- Before advice
- After Returning Advice
- Around Advice (proceed())
- Throws Advice
JoinPoint
Target
Weaving
Proxy
PointCut
PointCut
---------
When we apply advice to a target, then it applies for all methods available in
target class.
Target class methods ==> Demo (m1() & m2(int i) & m2 (String) )
If We want to apply advice for only specific methods in target class then we have
to use PointCut.
Def: Pointcut is used to specify for which methods in target class advice logic
should apply.
We can create Pointcut in below 2 ways
======================================
1) Static PointCut : We can check methodName, target class Type
-- > DynamicMethodMatcherPointCut
- boolean matches(Method m, Class t, Object... args)
Aspect J Annotations
---------------------
@Before
@AfterReturning
@Around
@Throws
@Pointcut
"execution(* com.nit.beans.Calculator.*(..))--
"execution(* com.nit.beans.Calculator.add(..))-
* *.*.*(..)
@PointCut(value="execution(* com.nit.beans.ReportGenerator.*(..)))
public void pce(){
Primary components
---------------------
Controller
Service
Dao