SpringBootAOP 1
SpringBootAOP 1
Mr. RAGHU
(NareshIT)
----------------
AOP : Aspect Oriented Programming
https://www.facebook.com/groups/thejavatemple
Types of Advices(5)
Before Advice : Executing Advice before calling b.method
Around Advice : Advice code made into 2 sections/parts, 1st part executed
before advice and then b.method, later 2nd part of advice.
After Returning Advice : After executing b.method, only on sucess execute advice.
expression : AS RT PACK.CLASS.METHOD(PARAM)
--------------------------------------------------------------------------------
Implementations
a) Spring AOP using XML Based Configuration [Legacy Style]
b) Spring AOP using AspectJ [Annotations]
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
Annotations:
@Aspect
@Before
@After
@Around
@AfterReturning
@AfterThrowing
@Pointcut
------------------------------------------------------------------
Q) What is the difference between After, After Returning, After Throwing Advices?
A) After advices is executed next to b.method either success or fail.
After Returning advice is executed only on succssful execution of b.method
After Throwing advice is executed only on Failure(Exception) execution of
b.method
===================================================================================
====
Pointcut
=> Pointcut is expression, it will select b.class methods which needs advices.
=> Pointcut can never specify which advice is going to be connected.
Pointcut Syntax:-
Specifier ReturnType Package.ClassName.MethodName(ParameterTypes)
-------Examples-----------------------------------------------------
#1) public int in.nit.dao.EmployeeDao.saveEmployee(Employee)
=> saveEmployee() method having Parameter 'Employee' with return type 'int'
of type public defined in class EmployeeDao (in.nit.dao)
is selected to connecte with advice.
=> Zero or more params (of any type) [Any Parameter is fine]
=> all classes which ar inside in.nit.dao package and thier methods
====================
B.Methods
---------------
M#1 public int saveEmployee(Employee e) { ...}
M#2 public void deleteEmployee(Integer id) { ...}
M#3 public void updateEmployee(Employee e) { ...}
M#4 public Employee getEmployee(Integer id) { ...}
-----Pointcut expressions--------------
Specifier ReturnType Package.ClassName.MethodName(ParameterTypes)
d) public * *(Integer)
No of Methods matching : M#2, M#4 (2)
c) public * saveEmployee(..)
a) public * *()
[Zero Params, any method name and any return type]