Aspect Oriented Spring

11 06 2016

Aspect Oriented Programming is a powerful technique to separate cross-cutting concerns (aspects) in your application apart from your logic.

Where is it used?

Spring uses it widely. Popular use case is Spring Security where each controller needs to have restricted access. It would make no sense to embed the security logic in each controller method. That is where aspects are useful. Thanks to AOP you can keep your aspect  related logic (called Advice) in one place and just define the points (joint points) where that logic should be run.

What is possible with Spring AOP

Spring AOP allows you to define the joint points on a method level by a means of dynamic proxies. It means that you can define in your advice that it should run before, after or around any method of your choice.

And if I need more?

Method interception will probably be enough for majority of your needs. However if you need more (constructor or field interception) you can use another AOP implementation: AspectJ.

How to Code it?

I coded the demo for you. Go ahead and check it out on my GitHub:

1. The dependency
First of all I’m gonna use the spring-boot-starter-aop pom and include it in my dependencies:


2. The controller with advised method
I use the annotation to define the joint point. This is the annotation I created:

public @interface MyAnnotation {}

No I can create sample controller method and annotate it with MyAnnotation:

public class MyController {

    private Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @RequestMapping(value = "/api/my-resource")
    public String getMyResource(){"Returning the resource");
        return "Your resource is here";

After running the app I can head over to to see that it returns the result and logs the message.

3. Define an Aspect (advice)
Time to define your aspect! Create a class annotated with @Aspect and a method that will be run before the methods annotated with @MyAnnotation. Use @Before annotation with parameter pointing to the MyAnnotation class:

public class MyResourceAdvice {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    public void performImportantJob(){"Performing important job in an advice method");

@Configuration is needed for Spring to include that class in scans.

4. Check the results

After these few steps you can go to and watch that in app logs there is controller’s log and Advice log before it:

eAdvice$$EnhancerBySpringCGLIB$$9dc59d93 : Performing important job in an advice method
p.l.spring.controller.MyController       : Returning the resource

You can also note that my aspect class is logged as EnhancerBySpringCGLIB – my controller class was dynamically proxied by cglib.

Tip 1: Readable @Before
If you don’t like the long @Before annotation param you can make it more readable by extracting it as a @Pointcut like that:

    private void annotatedWithMyAnnotation() {}

    public void performImportantJob(){"Performing important job in an advice method");

By defining @Pointcut you made your @Before definition much more readable. Note that @Pointcut method name may be any of your choice.

Tip 2: Help Spring to find your joint points
Where does the Spring look for annotated methods? Everywhere on your classpath. Do you need to scan all classes? If not, then you can narrow scanned packages:

    private void searchWithinControllersOnly() {}

    private void annotatedWithMyAnnotation() {}

    @Before("annotatedWithMyAnnotation() && searchWithinControllersOnly()")
    public void performImportantJob(){"Performing important job in an advice method");



Give Your feedback:

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: