/20@yegor256 1
Practical Example of AOP
with AspectJ
Yegor Bugayenko
/20@yegor256 2
Aspect Oriented Programming (AOP)
AspectJ in 2000

by Gregor Kiczales
in Xerox PARC
/20@yegor256 3
class Page {
public String html() {
// load HTML and return
}
}
class Directory {
public void delete() {
// remove all files
}
}
class File {
public long length() {
// return file size
}
}
/20@yegor256 4
while (true) {
try {
return load_html();
} catch (Exception ex) {
// nothing, just ignore
}
}
/20@yegor256 5
code duplication
/20@yegor256 6
class Page {
@Retry
public String html() {
// load HTML and return
}
}
/20@yegor256 7
@Retention(value=RUNTIME)
@Target(value=METHOD)
public @interface Retry {
}
/20@yegor256 8
@Aspect
class Robustness {
@Around("execution(* *(..)) && @annotation(Retry)")
public Object around(ProceedingJoinPoint point) {
while (true) {
try {
return point.proceed();
} catch (Exception ex) {
// just ignore it
}
}
}
}
/20@yegor256 9
class Page {
@Retry
public String html() {
// load HTML and return
}
}
@Aspect
class Robustness {
@Around(“…”)
public Object around(point) {
while (true) {
try {
return point.proceed();
} catch (Exception ex) {
// just ignore it
}
}
}
}
Page.class
Robustness.class
/20@yegor256 10
binary aspect weaving
javac + ajc
/20@yegor256 11
class Page {
private Robustness r;
public String html() {
return this.r.around(point);
}
public String html_aroundBody() {
// load HTML and return
}
}
@Aspect
class Robustness {
@Around(“…”)
public Object around(point) {
while (true) {
try {
return point.proceed();
} catch (Exception ex) {
// just ignore it
}
}
}
}
Page.class
Robustness.class
/20@yegor256 12
<plugin>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>ajc</goal>
</goals>
</execution>
</executions>
</plugin>
jcabi-maven-plugin
/20@yegor256 13
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
/20@yegor256 14
no code duplication
/20@yegor256 15
procedural and clumsy
/20@yegor256 16
@Aspect
class Robustness {
@Around("execution(* *(..)) && @annotation(Retry)")
public Object around(ProceedingJoinPoint point) {
while (true) {
try {
return point.proceed();
} catch (Exception ex) {
// just ignore it
}
}
}
}
/20@yegor256 17
class RobustPage {
private final Page page;
public String html() {
while (true) {
try {
return this.page.html();
} catch (Exception ex) {
// ignore
}
}
}
}
/20@yegor256 18
new RobustPage(new Page());
/20@yegor256 19
a better Java with AOP?
/20@yegor256 20
@yegor256
/20@yegor256 21
class Page {
public String html() around retry() {
// load HTML and return
}
advice Object retry() {
while (true) {
try {
return proceed;
} catch (Exception ex) {
// ignore
}
}
}
}
/20@yegor256 22
class Robust<T> decorates T {
advice Object retry() {
while (true) {
try {
return proceed;
} catch (Exception ex) {
// ignore
}
}
}
}
/20@yegor256 23
page = new Robust<Page>(new Page()) {
@Override
public String html() around retry() {
return origin.html();
}
}

More Related Content

PDF
bioinfolec7th20071005
PPT
XML - State of the Art
PPTX
Java and SPARQL
PPTX
Java and XML
PDF
Visualize open data with Plone - eea.daviz PLOG 2013
PDF
Functional manipulations of large data graphs 20160601
PPTX
Session 17 - Collections - Lists, Sets
PDF
Odoo Technical Concepts Summary
bioinfolec7th20071005
XML - State of the Art
Java and SPARQL
Java and XML
Visualize open data with Plone - eea.daviz PLOG 2013
Functional manipulations of large data graphs 20160601
Session 17 - Collections - Lists, Sets
Odoo Technical Concepts Summary

Viewers also liked (20)

PDF
ORM is offensive
PDF
Object Oriented Lies
PDF
Chat Bot Architecture
PDF
OOP Is Dead? Not Yet!
PDF
Continuous Integration is Dead
PDF
Meetings Or Discipline
PDF
Who Manages Who?
PDF
How Do You Know When Your Product is Ready to be Shipped?
PDF
How Much Immutability Is Enough?
PDF
Need It Robust? Make It Fragile!
PDF
Fail Fast. Into User's Face.
PDF
How Anemic Objects Kill OOP
PDF
Problems With Chat Bots
PDF
Java vs OOP
PDF
Who Is a Software Architect?
PDF
Built-in Fake Objects
PDF
Management without managers
PDF
How Immutability Helps in OOP
PDF
ORM is a perfect anti-pattern
PDF
RABIES IN TUNISIA:Evolution and result of « National Program of Rabies Control »
ORM is offensive
Object Oriented Lies
Chat Bot Architecture
OOP Is Dead? Not Yet!
Continuous Integration is Dead
Meetings Or Discipline
Who Manages Who?
How Do You Know When Your Product is Ready to be Shipped?
How Much Immutability Is Enough?
Need It Robust? Make It Fragile!
Fail Fast. Into User's Face.
How Anemic Objects Kill OOP
Problems With Chat Bots
Java vs OOP
Who Is a Software Architect?
Built-in Fake Objects
Management without managers
How Immutability Helps in OOP
ORM is a perfect anti-pattern
RABIES IN TUNISIA:Evolution and result of « National Program of Rabies Control »
Ad

Similar to Practical Example of AOP with AspectJ (20)

PDF
dojo.Patterns
PDF
EVOLVE'14 | Enhance | Gabriel Walt | Sightly Component Development
PDF
Davide Cerbo - Kotlin loves React - Codemotion Milan 2018
PDF
當ZK遇見Front-End
PPTX
Androidaop 170105090257
PPT
Struts 2 + Spring
PDF
Frontin like-a-backer
PDF
Reactive Type-safe WebComponents
PDF
A clean(er) architecture
PDF
Scalatra - Massimiliano Dessì (Energeya)
PDF
Scala Italy 2013 extended Scalatra vs Spring MVC
PDF
React.js or why DOM finally makes sense
PDF
Sharper Better Faster Dagger ‡ - Droidcon SF
PDF
Modular and Event-Driven JavaScript
ODP
ActiveWeb: Chicago Java User Group Presentation
PDF
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
PDF
jQuery: Events, Animation, Ajax
PDF
Revolution or Evolution in Page Object
PDF
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
PDF
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
dojo.Patterns
EVOLVE'14 | Enhance | Gabriel Walt | Sightly Component Development
Davide Cerbo - Kotlin loves React - Codemotion Milan 2018
當ZK遇見Front-End
Androidaop 170105090257
Struts 2 + Spring
Frontin like-a-backer
Reactive Type-safe WebComponents
A clean(er) architecture
Scalatra - Massimiliano Dessì (Energeya)
Scala Italy 2013 extended Scalatra vs Spring MVC
React.js or why DOM finally makes sense
Sharper Better Faster Dagger ‡ - Droidcon SF
Modular and Event-Driven JavaScript
ActiveWeb: Chicago Java User Group Presentation
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
jQuery: Events, Animation, Ajax
Revolution or Evolution in Page Object
Enhancing Productivity and Insight A Tour of JDK Tools Progress Beyond Java 17
名古屋SGGAE/J勉強会 Grails、Gaelykでハンズオン
Ad

More from Yegor Bugayenko (20)

PDF
Can Distributed Teams Deliver Quality?
PDF
Are You Sure You Are Not a Micromanager?
PDF
On Requirements Management (Demotivate Them Right)
PDF
My Experience of 1000 Interviews
PDF
Are you sure you are not a micromanager?
PDF
Quality Assurance vs. Testing
PDF
Is Java Getting Better?
PDF
Typical Pitfalls in Testing
PDF
Software Testing Pitfalls
PDF
Five Trends We Are Afraid Of
PDF
Experts vs Expertise
PDF
Who Cares About Quality?
PDF
Quantity vs. Quality
PDF
Experts vs Expertise
PDF
Zold: a cryptocurrency without Blockchain
PDF
Life Without Blockchain
PDF
How to Cut Corners and Stay Cool
PDF
Math or Love?
PDF
How much do you cost?
PDF
Java Annotations Are a Bad Idea
Can Distributed Teams Deliver Quality?
Are You Sure You Are Not a Micromanager?
On Requirements Management (Demotivate Them Right)
My Experience of 1000 Interviews
Are you sure you are not a micromanager?
Quality Assurance vs. Testing
Is Java Getting Better?
Typical Pitfalls in Testing
Software Testing Pitfalls
Five Trends We Are Afraid Of
Experts vs Expertise
Who Cares About Quality?
Quantity vs. Quality
Experts vs Expertise
Zold: a cryptocurrency without Blockchain
Life Without Blockchain
How to Cut Corners and Stay Cool
Math or Love?
How much do you cost?
Java Annotations Are a Bad Idea

Recently uploaded (20)

PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
PPTX
Independent Consultants’ Biggest Challenges in ERP Projects – and How Apagen ...
PDF
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
PPT
3.Software Design for software engineering
PDF
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
PDF
Adlice Diag Crack With Serial Key Free Download 2025
PPTX
Swiggy API Scraping A Comprehensive Guide on Data Sets and Applications.pptx
PPTX
Improving Audience Engagement ROI with ERP-Powered Insights
PPTX
Comprehensive Guide to Digital Image Processing Concepts and Applications
PPTX
ROI from Efficient Content & Campaign Management in the Digital Media Industry
PPTX
Presentation - Summer Internship at Samatrix.io_template_2.pptx
PDF
Coding with GPT-5- What’s New in GPT 5 That Benefits Developers.pdf
PPTX
Greedy best-first search algorithm always selects the path which appears best...
PDF
Top 10 Project Management Software for Small Teams in 2025.pdf
PDF
IT Consulting Services to Secure Future Growth
PPTX
Relevance Tuning with Genetic Algorithms
PDF
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
PPTX
AI Tools Revolutionizing Software Development Workflows
PPTX
ESDS_SAP Application Cloud Offerings.pptx
PPTX
MCP empowers AI Agents from Zero to Production
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Independent Consultants’ Biggest Challenges in ERP Projects – and How Apagen ...
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
3.Software Design for software engineering
SOFTWARE ENGINEERING Software Engineering (3rd Edition) by K.K. Aggarwal & Yo...
Adlice Diag Crack With Serial Key Free Download 2025
Swiggy API Scraping A Comprehensive Guide on Data Sets and Applications.pptx
Improving Audience Engagement ROI with ERP-Powered Insights
Comprehensive Guide to Digital Image Processing Concepts and Applications
ROI from Efficient Content & Campaign Management in the Digital Media Industry
Presentation - Summer Internship at Samatrix.io_template_2.pptx
Coding with GPT-5- What’s New in GPT 5 That Benefits Developers.pdf
Greedy best-first search algorithm always selects the path which appears best...
Top 10 Project Management Software for Small Teams in 2025.pdf
IT Consulting Services to Secure Future Growth
Relevance Tuning with Genetic Algorithms
Sanket Mhaiskar Resume - Senior Software Engineer (Backend, AI)
AI Tools Revolutionizing Software Development Workflows
ESDS_SAP Application Cloud Offerings.pptx
MCP empowers AI Agents from Zero to Production

Practical Example of AOP with AspectJ