SlideShare a Scribd company logo
Java Spring Training
Spring AOP – Aspect Oriented Programming
Page 1Classification: Restricted
Review
• Spring framework
• Inversion of Control
• Dependency Injection – Two types
• Defining beans using XML
• Inheriting beans
• Auto-wiring
• Annotations based configuration
• Java based configuration
Page 2Classification: Restricted
Agenda
• Spring AOP
• What is AOP
• AOP Terminologies
• AOP Implementations
Java & JEE Training
Spring AOP using AspectJ
Page 4Classification: Restricted
What is Aspect Oriented Programming?
• Oriented Programming entails breaking down program logic into distinct
parts called so-called concerns.
• The functions that span multiple points of an application are called cross-
cutting concerns and these cross-cutting concerns are conceptually
separate from the application's business logic.
• Examples of aspects like
• logging
• auditing
• declarative transactions
• Security
• caching
Page 5Classification: Restricted
AOP does not replace OOP but makes it better
• The key unit of modularity in OOP is the class, whereas in AOP the unit of
modularity is the aspect.
• Dependency Injection helps you decouple your application objects from
each other and AOP helps you decouple cross-cutting concerns from the
objects that they affect.
Page 6Classification: Restricted
AOP
Page 7Classification: Restricted
AOP Terminologies
Terms Description
Aspect A module which has a set of APIs providing cross-cutting requirements.
For example, a logging module would be called AOP aspect for logging.
An application can have any number of aspects depending on the
requirement.
Join point This represents a point in your application where you can plug-in AOP
aspect. You can also say, it is the actual place in the application where an
action will be taken using Spring AOP framework.
Advice This is the actual action to be taken either before or after the method
execution. This is actual piece of code that is invoked during program
execution by Spring AOP framework.
Pointcut This is a set of one or more joinpoints where an advice should be
executed. You can specify pointcuts using expressions or patterns as we
will see in our AOP examples.
Weaving Weaving is the process of linking aspects with other application types or
objects to create an advised object. This can be done at compile time,
load time, or at runtime. Spring AOP performs weaving at runtime.
Page 8Classification: Restricted
Typical AOP implementations…
• AspectJ (Recommended by Spring framework)
• Spring AOP
• JBoss AOP
Page 9Classification: Restricted
Spring AOP AspectJ implementations
• By Annotation
• By XML Configuration
Page 10Classification: Restricted
Spring AspectJ AOP
Spring AspectJ AOP implementation provides many annotations:
• @Aspect declares the class as aspect.
• @Pointcut declares the pointcut expression.
The annotations used to create advices are given below:
• @Before declares the before advice. It is applied before calling the actual method.
• @After declares the after advice. It is applied after calling the actual method and
before returning result.
• @AfterReturning declares the after returning advice. It is applied after calling the
actual method and before returning result. But you can get the result value in the
advice.
• @Around declares the around advice. It is applied before and after calling the
actual method.
• @AfterThrowing declares the throws advice. It is applied if actual method throws
exception.
Page 11Classification: Restricted
• Before Advice: These advices runs before the execution of join point methods. We can use
@Before annotation to mark an advice type as Before advice.
• After (finally) Advice: An advice that gets executed after the join point method finishes
executing, whether normally or by throwing an exception. We can create after advice using
@After annotation.
• After Returning Advice: Sometimes we want advice methods to execute only if the join point
method executes normally. We can use @AfterReturning annotation to mark a method as
after returning advice.
• After Throwing Advice: This advice gets executed only when join point method throws
exception, we can use it to rollback the transaction declaratively. We use @AfterThrowing
annotation for this type of advice.
• Around Advice: This is the most important and powerful advice. This advice surrounds the join
point method and we can also choose whether to execute the join point method or not. We
can write advice code that gets executed before and after the execution of the join point
method. It is the responsibility of around advice to invoke the join point method and return
values if the method is returning something. We use @Around annotation to create around
advice methods.
Page 12Classification: Restricted
Pointcut
@Pointcut("execution(* Operation.*(..))")
private void doSomething() {}
• The name of the pointcut expression is doSomething(). It will be applied on
all the methods of Operation class regardless of return type.
Page 13Classification: Restricted
Pointcut examples
Expression Example Meaning
@Pointcut("execution(public * *(..))") applied on all the public methods.
@Pointcut("execution(public Operation.*(..))") applied on all the public methods
of Operation class.
@Pointcut("execution(* Operation.*(..))") applied on all the methods of
Operation class.
@Pointcut("execution(public Employee.set*(..))") applied on all the public setter
methods of Employee class.
@Pointcut("execution(int Operation.*(..))") applied on all the methods of
Operation class that returns int
value.
Page 14Classification: Restricted
AOP Example
// Operation.java
public class Operation{
public void msg(){System.out.println("msg method invoked");}
public int m(){System.out.println("m method invoked");return 2;}
public int k(){System.out.println("k method invoked");return 3;}
}
Page 15Classification: Restricted
AOP Example
//The Aspect Class that contains Before advice
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackOperation{
@Pointcut("execution(* Operation.*(..))")
public void ptcut(){}//pointcut name
@Before(“ptcut()")//applying pointcut on before advice
public void myadvice(JoinPoint jp)//it is advice (before advice)
{
System.out.println("additional concern");
//System.out.println("Method Signature: " + jp.getSignature());
}
}
Page 16Classification: Restricted
Beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
<bean id="opBean" class=“demo.Operation"> </bean>
<bean id="trackMyBean" class=“demo.TrackOperation"></bean>
</beans>
Page 17Classification: Restricted
More examples: @After
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackOperation{
@Pointcut("execution(* Operation.*(..))")
public void k(){}//pointcut name
@After("k()")//applying pointcut on after advice
public void myadvice(JoinPoint jp)//it is advice (after advice)
{
System.out.println("additional concern");
//System.out.println("Method Signature: " + jp.getSignature());
}
}
Page 18Classification: Restricted
More example: @AfterReturning
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class TrackOperation{
@AfterReturning(pointcut = "execution(* Operation.*(..))", returning= "result")
public void myadvice(JoinPoint jp,Object result){ //ADVICE
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Result in advice: "+result);
System.out.println("end of after returning advice...");
}
}
Page 19Classification: Restricted
More example: @Around
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class TrackOperation
{
@Pointcut("execution(* Operation.*(..))")
public void abcPointcut(){}
@Around("abcPointcut()")
public Object myadvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("Additional Concern Before calling actual method");
Object obj=pjp.proceed();
System.out.println("Additional Concern After calling actual method");
return obj;
}
}
//Note: You need to pass the PreceedingJoinPoint reference in the advice method, so that we can proceed the
request by calling the proceed() method.
Page 20Classification: Restricted
More examples: @AfterThrowing
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class TrackOperation{
@AfterThrowing(
pointcut = "execution(* Operation.*(..))",
throwing= "error")
public void myadvice(JoinPoint jp,Throwable error)//it is advice
{
System.out.println("additional concern");
System.out.println("Method Signature: " + jp.getSignature());
System.out.println("Exception is: "+error);
System.out.println("end of after throwing advice...");
}
}
Page 21Classification: Restricted
Spring – AspectJ AOP – XML configuration - Example
<aop:config>
<aop:aspect id="myaspect" ref="trackAspect" >
<!-- @Before -->
<aop:pointcut id="pointCutBefore" expression="execution(* com.javatpo
int.Operation.*(..))" />
<aop:before method="myadvice" pointcut-ref="pointCutBefore" />
</aop:aspect>
</aop:config>
Page 22Classification: Restricted
Jars needed for AspectJ
• aspectjrt.jar
• aspectjweaver.jar
• aspectj.jar
• aopalliance.jar
Page 23Classification: Restricted
Topics to be covered in next session
• Spring MVC
• Spring MVC – Hibernate Integration
Page 24Classification: Restricted
Thank you!

More Related Content

What's hot (19)

JDBC Part - 2
JDBC Part - 2JDBC Part - 2
JDBC Part - 2
Hitesh-Java
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
24 collections framework interview questions
24 collections framework interview questions24 collections framework interview questions
24 collections framework interview questions
Arun Vasanth
 
Bea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guideBea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guide
Pankaj Singh
 
Hibernate - Part 1
Hibernate - Part 1Hibernate - Part 1
Hibernate - Part 1
Hitesh-Java
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer Reference
Muthuselvam RS
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
Soba Arjun
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Spring & hibernate
Spring & hibernateSpring & hibernate
Spring & hibernate
Santosh Kumar Kar
 
Java interview-questions-and-answers
Java interview-questions-and-answersJava interview-questions-and-answers
Java interview-questions-and-answers
bestonlinetrainers
 
Technical Interview
Technical InterviewTechnical Interview
Technical Interview
prashant patel
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2
PawanMM
 
Hibernate in Action
Hibernate in ActionHibernate in Action
Hibernate in Action
Akshay Ballarpure
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
Hitesh-Java
 
Hibernate Advance Interview Questions
Hibernate Advance Interview QuestionsHibernate Advance Interview Questions
Hibernate Advance Interview Questions
Rudra Garnaik, PMI-ACP®
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
sourabh aggarwal
 
9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss
Mark Papis
 
Session 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI BeansSession 43 - Spring - Part 1 - IoC DI Beans
Session 43 - Spring - Part 1 - IoC DI Beans
PawanMM
 
24 collections framework interview questions
24 collections framework interview questions24 collections framework interview questions
24 collections framework interview questions
Arun Vasanth
 
Bea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guideBea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guide
Pankaj Singh
 
Hibernate - Part 1
Hibernate - Part 1Hibernate - Part 1
Hibernate - Part 1
Hitesh-Java
 
Hibernate Developer Reference
Hibernate Developer ReferenceHibernate Developer Reference
Hibernate Developer Reference
Muthuselvam RS
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
Soba Arjun
 
Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans Spring - Part 1 - IoC, Di and Beans
Spring - Part 1 - IoC, Di and Beans
Hitesh-Java
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
guest11106b
 
Java interview-questions-and-answers
Java interview-questions-and-answersJava interview-questions-and-answers
Java interview-questions-and-answers
bestonlinetrainers
 
Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2Session 40 - Hibernate - Part 2
Session 40 - Hibernate - Part 2
PawanMM
 
JSP - Part 2 (Final)
JSP - Part 2 (Final) JSP - Part 2 (Final)
JSP - Part 2 (Final)
Hitesh-Java
 
Java Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and ExampleJava Hibernate Programming with Architecture Diagram and Example
Java Hibernate Programming with Architecture Diagram and Example
kamal kotecha
 
Spring 4 final xtr_presentation
Spring 4 final xtr_presentationSpring 4 final xtr_presentation
Spring 4 final xtr_presentation
sourabh aggarwal
 
9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss
Mark Papis
 

Similar to Spring - Part 3 - AOP (20)

Spring aop
Spring aopSpring aop
Spring aop
UMA MAHESWARI
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
Tata Consultancy Services
 
Spring aop
Spring aopSpring aop
Spring aop
sanskriti agarwal
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
Lhouceine OUHAMZA
 
Spring framework AOP
Spring framework  AOPSpring framework  AOP
Spring framework AOP
Anuj Singh Rajput
 
Spring aop concepts
Spring aop conceptsSpring aop concepts
Spring aop concepts
RushiBShah
 
Spring AOP in Nutshell
Spring AOP in Nutshell Spring AOP in Nutshell
Spring AOP in Nutshell
Onkar Deshpande
 
Spring aop
Spring aopSpring aop
Spring aop
Hamid Ghorbani
 
AOP
AOPAOP
AOP
Joshua Yoon
 
Spring framework aop
Spring framework aopSpring framework aop
Spring framework aop
Taemon Piya-Lumyong
 
AOP (Aspect-Oriented Programming) spring boot
AOP (Aspect-Oriented Programming) spring bootAOP (Aspect-Oriented Programming) spring boot
AOP (Aspect-Oriented Programming) spring boot
PLAYAFIFI
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
cteguh
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
SHAKIL AKHTAR
 
spring aop.pptx aspt oreinted programmin
spring aop.pptx aspt oreinted programminspring aop.pptx aspt oreinted programmin
spring aop.pptx aspt oreinted programmin
zmulani8
 
spring aop
spring aopspring aop
spring aop
Kalyani Patil
 
Aspect oriented programming with spring
Aspect oriented programming with springAspect oriented programming with spring
Aspect oriented programming with spring
Sreenivas Kappala
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
Fernando Almeida
 
Spring aop
Spring aopSpring aop
Spring aop
Harshit Choudhary
 
Spring AOP
Spring AOPSpring AOP
Spring AOP
AnushaNaidu
 
spring aop.pptxfgfdgfdgfdgfdgfdgvbvcbvbcvbdf
spring aop.pptxfgfdgfdgfdgfdgfdgvbvcbvbcvbdfspring aop.pptxfgfdgfdgfdgfdgfdgvbvcbvbcvbdf
spring aop.pptxfgfdgfdgfdgfdgfdgvbvcbvbcvbdf
zmulani8
 
Ad

More from Hitesh-Java (20)

Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVC
Hitesh-Java
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
JDBC
JDBCJDBC
JDBC
Hitesh-Java
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
Hitesh-Java
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
Hitesh-Java
 
Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2
Hitesh-Java
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews
Hitesh-Java
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
Hitesh-Java
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
Hitesh-Java
 
Object Class
Object Class Object Class
Object Class
Hitesh-Java
 
Exception Handling - Continued
Exception Handling - Continued Exception Handling - Continued
Exception Handling - Continued
Hitesh-Java
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1
Hitesh-Java
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
Hitesh-Java
 
OOP with Java - Part 3
OOP with Java - Part 3OOP with Java - Part 3
OOP with Java - Part 3
Hitesh-Java
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
Hitesh-Java
 
Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java
Hitesh-Java
 
Practice Session
Practice Session Practice Session
Practice Session
Hitesh-Java
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
Hitesh-Java
 
Spring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVCSpring - Part 4 - Spring MVC
Spring - Part 4 - Spring MVC
Hitesh-Java
 
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slidesSpring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Hitesh-Java
 
Collections - Maps
Collections - Maps Collections - Maps
Collections - Maps
Hitesh-Java
 
Review Session - Part -2
Review Session - Part -2Review Session - Part -2
Review Session - Part -2
Hitesh-Java
 
Review Session and Attending Java Interviews
Review Session and Attending Java Interviews Review Session and Attending Java Interviews
Review Session and Attending Java Interviews
Hitesh-Java
 
Collections - Lists, Sets
Collections - Lists, Sets Collections - Lists, Sets
Collections - Lists, Sets
Hitesh-Java
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
Hitesh-Java
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
Hitesh-Java
 
Exception Handling - Continued
Exception Handling - Continued Exception Handling - Continued
Exception Handling - Continued
Hitesh-Java
 
Exception Handling - Part 1
Exception Handling - Part 1 Exception Handling - Part 1
Exception Handling - Part 1
Hitesh-Java
 
OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers OOPs with Java - Packaging and Access Modifiers
OOPs with Java - Packaging and Access Modifiers
Hitesh-Java
 
OOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and InterfacesOOP with Java - Abstract Classes and Interfaces
OOP with Java - Abstract Classes and Interfaces
Hitesh-Java
 
OOP with Java - Part 3
OOP with Java - Part 3OOP with Java - Part 3
OOP with Java - Part 3
Hitesh-Java
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
Hitesh-Java
 
Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java Intro to Object Oriented Programming with Java
Intro to Object Oriented Programming with Java
Hitesh-Java
 
Practice Session
Practice Session Practice Session
Practice Session
Hitesh-Java
 
Strings in Java
Strings in Java Strings in Java
Strings in Java
Hitesh-Java
 
Ad

Recently uploaded (20)

Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data ResilienceFloods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.Introduction to Internet of things .ppt.
Introduction to Internet of things .ppt.
hok12341073
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too LateKubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free DownloadViral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
Ben Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding WorldBen Blair - Operating Safely in a Vibe Coding World
Ben Blair - Operating Safely in a Vibe Coding World
AWS Chicago
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
“Solving Tomorrow’s AI Problems Today with Cadence’s Newest Processor,” a Pre...
Edge AI and Vision Alliance
 
Edge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdfEdge-banding-machines-edgeteq-s-200-en-.pdf
Edge-banding-machines-edgeteq-s-200-en-.pdf
AmirStern2
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent IntegrationPyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdfArtificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdfCrypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 

Spring - Part 3 - AOP

  • 1. Java Spring Training Spring AOP – Aspect Oriented Programming
  • 2. Page 1Classification: Restricted Review • Spring framework • Inversion of Control • Dependency Injection – Two types • Defining beans using XML • Inheriting beans • Auto-wiring • Annotations based configuration • Java based configuration
  • 3. Page 2Classification: Restricted Agenda • Spring AOP • What is AOP • AOP Terminologies • AOP Implementations
  • 4. Java & JEE Training Spring AOP using AspectJ
  • 5. Page 4Classification: Restricted What is Aspect Oriented Programming? • Oriented Programming entails breaking down program logic into distinct parts called so-called concerns. • The functions that span multiple points of an application are called cross- cutting concerns and these cross-cutting concerns are conceptually separate from the application's business logic. • Examples of aspects like • logging • auditing • declarative transactions • Security • caching
  • 6. Page 5Classification: Restricted AOP does not replace OOP but makes it better • The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. • Dependency Injection helps you decouple your application objects from each other and AOP helps you decouple cross-cutting concerns from the objects that they affect.
  • 8. Page 7Classification: Restricted AOP Terminologies Terms Description Aspect A module which has a set of APIs providing cross-cutting requirements. For example, a logging module would be called AOP aspect for logging. An application can have any number of aspects depending on the requirement. Join point This represents a point in your application where you can plug-in AOP aspect. You can also say, it is the actual place in the application where an action will be taken using Spring AOP framework. Advice This is the actual action to be taken either before or after the method execution. This is actual piece of code that is invoked during program execution by Spring AOP framework. Pointcut This is a set of one or more joinpoints where an advice should be executed. You can specify pointcuts using expressions or patterns as we will see in our AOP examples. Weaving Weaving is the process of linking aspects with other application types or objects to create an advised object. This can be done at compile time, load time, or at runtime. Spring AOP performs weaving at runtime.
  • 9. Page 8Classification: Restricted Typical AOP implementations… • AspectJ (Recommended by Spring framework) • Spring AOP • JBoss AOP
  • 10. Page 9Classification: Restricted Spring AOP AspectJ implementations • By Annotation • By XML Configuration
  • 11. Page 10Classification: Restricted Spring AspectJ AOP Spring AspectJ AOP implementation provides many annotations: • @Aspect declares the class as aspect. • @Pointcut declares the pointcut expression. The annotations used to create advices are given below: • @Before declares the before advice. It is applied before calling the actual method. • @After declares the after advice. It is applied after calling the actual method and before returning result. • @AfterReturning declares the after returning advice. It is applied after calling the actual method and before returning result. But you can get the result value in the advice. • @Around declares the around advice. It is applied before and after calling the actual method. • @AfterThrowing declares the throws advice. It is applied if actual method throws exception.
  • 12. Page 11Classification: Restricted • Before Advice: These advices runs before the execution of join point methods. We can use @Before annotation to mark an advice type as Before advice. • After (finally) Advice: An advice that gets executed after the join point method finishes executing, whether normally or by throwing an exception. We can create after advice using @After annotation. • After Returning Advice: Sometimes we want advice methods to execute only if the join point method executes normally. We can use @AfterReturning annotation to mark a method as after returning advice. • After Throwing Advice: This advice gets executed only when join point method throws exception, we can use it to rollback the transaction declaratively. We use @AfterThrowing annotation for this type of advice. • Around Advice: This is the most important and powerful advice. This advice surrounds the join point method and we can also choose whether to execute the join point method or not. We can write advice code that gets executed before and after the execution of the join point method. It is the responsibility of around advice to invoke the join point method and return values if the method is returning something. We use @Around annotation to create around advice methods.
  • 13. Page 12Classification: Restricted Pointcut @Pointcut("execution(* Operation.*(..))") private void doSomething() {} • The name of the pointcut expression is doSomething(). It will be applied on all the methods of Operation class regardless of return type.
  • 14. Page 13Classification: Restricted Pointcut examples Expression Example Meaning @Pointcut("execution(public * *(..))") applied on all the public methods. @Pointcut("execution(public Operation.*(..))") applied on all the public methods of Operation class. @Pointcut("execution(* Operation.*(..))") applied on all the methods of Operation class. @Pointcut("execution(public Employee.set*(..))") applied on all the public setter methods of Employee class. @Pointcut("execution(int Operation.*(..))") applied on all the methods of Operation class that returns int value.
  • 15. Page 14Classification: Restricted AOP Example // Operation.java public class Operation{ public void msg(){System.out.println("msg method invoked");} public int m(){System.out.println("m method invoked");return 2;} public int k(){System.out.println("k method invoked");return 3;} }
  • 16. Page 15Classification: Restricted AOP Example //The Aspect Class that contains Before advice import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @Aspect public class TrackOperation{ @Pointcut("execution(* Operation.*(..))") public void ptcut(){}//pointcut name @Before(“ptcut()")//applying pointcut on before advice public void myadvice(JoinPoint jp)//it is advice (before advice) { System.out.println("additional concern"); //System.out.println("Method Signature: " + jp.getSignature()); } }
  • 17. Page 16Classification: Restricted Beans.xml <?xml version="1.0" encoding="UTF-8"?> <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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <aop:aspectj-autoproxy/> <bean id="opBean" class=“demo.Operation"> </bean> <bean id="trackMyBean" class=“demo.TrackOperation"></bean> </beans>
  • 18. Page 17Classification: Restricted More examples: @After import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Pointcut; @Aspect public class TrackOperation{ @Pointcut("execution(* Operation.*(..))") public void k(){}//pointcut name @After("k()")//applying pointcut on after advice public void myadvice(JoinPoint jp)//it is advice (after advice) { System.out.println("additional concern"); //System.out.println("Method Signature: " + jp.getSignature()); } }
  • 19. Page 18Classification: Restricted More example: @AfterReturning import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; @Aspect public class TrackOperation{ @AfterReturning(pointcut = "execution(* Operation.*(..))", returning= "result") public void myadvice(JoinPoint jp,Object result){ //ADVICE System.out.println("additional concern"); System.out.println("Method Signature: " + jp.getSignature()); System.out.println("Result in advice: "+result); System.out.println("end of after returning advice..."); } }
  • 20. Page 19Classification: Restricted More example: @Around import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Aspect public class TrackOperation { @Pointcut("execution(* Operation.*(..))") public void abcPointcut(){} @Around("abcPointcut()") public Object myadvice(ProceedingJoinPoint pjp) throws Throwable { System.out.println("Additional Concern Before calling actual method"); Object obj=pjp.proceed(); System.out.println("Additional Concern After calling actual method"); return obj; } } //Note: You need to pass the PreceedingJoinPoint reference in the advice method, so that we can proceed the request by calling the proceed() method.
  • 21. Page 20Classification: Restricted More examples: @AfterThrowing import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; @Aspect public class TrackOperation{ @AfterThrowing( pointcut = "execution(* Operation.*(..))", throwing= "error") public void myadvice(JoinPoint jp,Throwable error)//it is advice { System.out.println("additional concern"); System.out.println("Method Signature: " + jp.getSignature()); System.out.println("Exception is: "+error); System.out.println("end of after throwing advice..."); } }
  • 22. Page 21Classification: Restricted Spring – AspectJ AOP – XML configuration - Example <aop:config> <aop:aspect id="myaspect" ref="trackAspect" > <!-- @Before --> <aop:pointcut id="pointCutBefore" expression="execution(* com.javatpo int.Operation.*(..))" /> <aop:before method="myadvice" pointcut-ref="pointCutBefore" /> </aop:aspect> </aop:config>
  • 23. Page 22Classification: Restricted Jars needed for AspectJ • aspectjrt.jar • aspectjweaver.jar • aspectj.jar • aopalliance.jar
  • 24. Page 23Classification: Restricted Topics to be covered in next session • Spring MVC • Spring MVC – Hibernate Integration