DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Frameworks
  4. Basics of Spring AOP

Basics of Spring AOP

Want to learn more about the basics of Spring AOP? Check out this post where we explore aspects of Spring AOP with sample code.

vikas kasegaonkar user avatar by
vikas kasegaonkar
CORE ·
Nov. 13, 18 · Presentation
Like (7)
Save
Tweet
Share
13.17K Views

Join the DZone community and get the full member experience.

Join For Free

What Is AOP?

AOP (Aspect-Oriented Programming) is a way of programming that helps increase modularity and avoid strong coupling of code by separating cross-cutting concerns. Additionally, it is one of the key components of the Spring Framework.

A cross-cutting concern is code logic, which is scattered throughout the application. It affects the entire application. Transaction management and logging are the best examples of cross-cutting concerns.

The key unit of modularity in OOP is class, whereas aspect is the unit of modularity in AOP.

Why AOP Is Needed?

Sometimes, we add code logic and it gets scattered and tangled across the application. Let's say that we want to add the code to check the performance of each method of services. Without AOP, we need to repeat some lines of code to each expected method. But using AOP, we can put the logic in one place and call it whenever it is necessary instead of duplicating and scattering the entire project.

AOP Concepts:

1. Aspect: A modularization of a concern that cuts across multiple classes, e.g transaction management.

2. Join-Point: A point during execution of the program, such as execution of method or handling of an exception. In Spring AOP, join-point always represents method execution.

3. Advice: Action taken by an aspect at the particular join point.

4. Pointcut: Predicate that matches join point.

5. Target Object: Target to which aspect applies.

How to Implement AOP?

Ways to implement AOP:

1. By Spring 1.2 - dtd-based style (old style)

2. By AspectJ annotation style

3. Spring XML Configuration Style

Image title

Example (Spring XML Configuration Style):

Logging aspect implementation for target service with spring XML-based configuration.

Step 1: Create a Maven project structure, as shown below.

Image title

Step 2: Add required Maven dependencies

Image title

Step 3: Write a Target class/service that we want to apply aspect:

Image title

Step 4: Write an Aspect class.

Image title

Step 5: Create an applicationContext.xml file for adding aspect and bean configurations, as shown below.

Image title


Step 6: Write an Application.java class for executing an example. This is the main class for execution.

Image title

After execution of Application.java, we get an output that looks like the following:

Image title

Conclusion

In this article, I attempted to explain the Basics of Spring AOP (Aspect-Oriented Programming) with step-by-step implementations as an example. If you need more details, check out the Spring official document. The examples from this article are available on GitHub.

Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Java Development Trends 2023
  • Too Many Tools? Streamline Your Stack With AIOps
  • Automated Performance Testing With ArgoCD and Iter8
  • API Design Patterns Review

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: