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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Trending

  • Mistaking Code Production for Engineering Progress: AI Productivity Myths Part 1
  • Building a Practical Cloud-Native Golden Path: A Guide to Kubernetes-Based Service Delivery, Self-Service, and Developer-Friendly Defaults
  • Detection and Response Did Its Job. Now Someone Has to Actually Fix It.
  • Introducing senro: A Pipeline Engine You Program in Go

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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.

By 
vikas kasegaonkar
·
Updated Nov. 13, 18 · Presentation
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
14.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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.

Related

  • Building a CRUD Application With Spring and SimpleJdbcMapper
  • How to Marry MDC With Spring Integration
  • How Spring and Hibernate Simplify Web and Database Management
  • Functional Endpoints: Alternative to Controllers in WebFlux

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook