Technology Blog Posts by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
Konrad-Gawelek
Associate
Associate
1,887

1. Introduction

In the world of enterprise software, enhancing maintainability and scalability is the key. This is especially true for SAP Commerce, where customization often means diving deep into the architecture. One powerful yet underutilized feature in the Spring Framework, integral to SAP Commerce, is Aspect-Oriented Programming (AOP).

In this blog, we’ll explore what AOP is, why it’s useful, and how you can use it in SAP Commerce. We’ll also walk through a real-world scenario where AOP saved the day.

What is AOP?

Aspect-Oriented Programming (AOP) is a programming paradigm that allows you to separate cross-cutting concerns (like logging, security, and transaction management) from your business logic.

Without AOP, developers often scatter code for these concerns across multiple layers, leading to duplication and less maintainable solutions. AOP helps centralize these concerns into aspects, making your code cleaner and easier to manage.

 

2. Key Concepts of AOP

 Aspect

An aspect is a piece of code designed to handle repetitive tasks—things like logging, security checks, or performance tracking—that affect multiple parts of an application. Instead of scattering these tasks across the system, you define them in one place (the aspect).

For example, if you want to log every time a price is calculated, you don’t need to manually add logging code to every pricing-related method. You define an aspect to handle all logging for you, automatically and consistently.

Join Point

A join point is any point in your application where a specific action takes place, like when a method is called or an object is created. In AOP, these are the opportunities for your aspect to step in and do its job.

For example, every time a customer places an order, that’s a join point. It’s a moment where you might want to track the action (logging), check user permissions (security), or monitor performance (profiling).

Advice

Advice is what the aspect actually does at the join point. It’s the specific instruction or action triggered when a condition is met. Think of advice as the logic that answers: “What should happen at this join point?”

For example:

  • Before Advice: Check if the user is authorized before they access a service.
  • After Advice: Log the outcome of a transaction after it’s processed.
  • Around Advice: Measure how long a method takes by wrapping logic before and after its execution.

Pointcut

A pointcut is the filter that tells your aspect when and where to act. It defines the criteria for selecting join points.

For example, if you only want to log price calculations, you create a pointcut that targets methods in your pricing service, like getPrice(). This ensures your advice runs exactly where it’s needed—and nowhere else.

Weaving

Weaving is the process of connecting your aspects with the main codebase. It’s like stitching a patch onto fabric, seamlessly embedding your cross-cutting concerns into the core functionality.

In Spring, this weaving happens automatically at runtime using proxies. This means you can inject additional behavior without permanently altering your original code.

 

Why Use AOP in SAP Commerce?

SAP Commerce, built on the Spring Framework, is a highly extensible platform, but its complexity can lead to challenges:

  • Logging: You may want detailed logging for specific services without altering their code.
  • Performance Monitoring: Track method execution times for optimization.
  • Security: Enforce security checks for sensitive operations.
  • Special use cases: Situations where traditional approach can’t be used.

AOP allows you to address these scenarios with minimal impact on your core logic, keeping customizations modular and maintainable.

 

3. Use case

One practical scenario where AOP shines is when you need to override or modify functionality in an SAP Commerce method without directly altering its code. Here’s an example from my experience: customizing the email-sending behavior of a CronJob.

The challenge arose when the customer switched their email service provider, necessitating changes in the cronjob functionality. However, the CronJob class in SAP Commerce is a Jalo class, not a bean service, making it problematic to override in a way that would apply the new implementation to all previously created CronJobs. In this scenario, Aspect-Oriented Programming (AOP) can be utilized to handle the requirement more easily and clearly from a code perspective

The first step was to find the specific method in the CronJob class responsible for sending emails and creating an aspect class.

In the CronJobAspect class we can configure the Pointcut, so the method which will be intercepted by AOP, method which will be called and when it will be called – before, after or instead of the original method.

It can look like this and in this case, we call the new method instead of the original one:

@Aspect

public class CronJobAspect {

    @Autowired

    CustomCronJobEmailService cronjobEmailService;

    @Pointcut("execution(* de.hybris.platform.cronjob.jalo.CronJob.sendEmail(..))")

    public void cronjobEmailSendingPointcut(){}//pointcut name

    @Around("cronjobEmailSendingPointcut()")//applying pointcut

    public void sendEmailWithCronJobStatus(JoinPoint jp) throws EmailException{...}

}

It’s also possible to configure that in one annotation, without special one for the pointcut, like this:

@Around("execution(* de.hybris.platform.cronjob.jalo.CronJob.sendEmail(..))")

public void sendEmailWithCronJobStatus(JoinPoint jp) throws EmailException

 

4. Conclusion

Aspect-Oriented Programming can be a game-changer for SAP Commerce developers seeking clean, modular solutions to common challenges. Whether you’re logging, monitoring, or adding custom business logic, AOP keeps your codebase maintainable.

It's worth considering the capabilities that AOP offers and keeping them in mind when searching for solutions to your problems.