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 Video Library
Refcards
Trend Reports

Events

View Events Video Library

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

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

SBOMs are essential to circumventing software supply chain attacks, and they provide visibility into various software components.

Related

  • Magic of Aspects: How AOP Works in Spring
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Adding Versatility to Java Logging Aspect
  • How to Marry MDC With Spring Integration

Trending

  • How to Build Your First Generative AI App With Langflow: A Step-by-Step Guide
  • Decoding Database Speed: Essential Server Resources and Their Impact
  • Building an AI Nutrition Coach With OpenAI, Gradio, and gTTS
  • Contract-Driven ML: The Missing Link to Trustworthy Machine Learning
  1. DZone
  2. Coding
  3. Frameworks
  4. Aspect Oriented Programming With Spring Boot

Aspect Oriented Programming With Spring Boot

Learn how to achieve aspect orientation by using Spring Boot and Aspect4j annotations.

By 
Emmanouil Gkatziouras user avatar
Emmanouil Gkatziouras
DZone Core CORE ·
Jun. 01, 16 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
63.7K Views

Join the DZone community and get the full member experience.

Join For Free

In a previous post, I provided a simple example on how to achieve aspect orientation in Spring by using a ProxyFactoryBean and implementing the MethodBeforeAdvice interface.

In this example, we will learn how to achieve aspect orientation by using Spring Boot and Aspect4j annotations.

Let’s start with our Gradle file.

group 'com.gkatzioura'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.3.3.RELEASE")
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile("org.springframework.boot:spring-boot-starter-jetty")
    compile("org.slf4j:slf4j-api:1.6.6")
    compile("ch.qos.logback:logback-classic:1.0.13")
    compile("org.aspectj:aspectjweaver:1.8.8")
    testCompile("junit:junit:4.11")
}

Apart from the Spring Boot plugins we have to include the aspectjweaver package.

The application class:

package com.gkatzioura.spring.aop;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

/**
 * Created by gkatzioura on 5/28/16.
 */
@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication springApplication = new SpringApplication();
        ApplicationContext applicationContext = springApplication.run(Application.class,args);
    }
}

We will implement a service that will fetch a sample for the name specified.

The sample model would be a simple POJO:

package com.gkatzioura.spring.aop.model;

/**
 * Created by gkatzioura on 5/28/16.
 */
public class Sample {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

The service will create a sample object:

package com.gkatzioura.spring.aop.service;

import com.gkatzioura.spring.aop.model.Sample;
import org.springframework.stereotype.Service;

/**
 * Created by gkatzioura on 5/28/16.
 */
@Service
public class SampleService {

    public Sample createSample(String sampleName) {

        Sample sample = new Sample();
        sample.setName(sampleName);

        return sample;
    }
}

So far so good.
Suppose that we want to do some actions before and after creating a sample. AOP in spring can help us to do so. The createSample function is a JoinPoint. The main concept is to work with Advices. From the documentation advice is an action taken by an aspect at a particular join point.

In our case we want to do some extra logging before the sample gets created. Therefore we will use the Before advice type:

package com.gkatzioura.spring.aop.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by gkatzioura on 5/28/16.
 */
@Aspect
@Component
public class SampleServiceAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleServiceAspect.class);

    @Before("execution(* com.gkatzioura.spring.aop.service.SampleService.createSample (java.lang.String)) && args(sampleName)")
    public void beforeSampleCreation(String sampleName) {
        LOGGER.info("A request was issued for a sample name: "+sampleName);
    }
}

We implemented a function with the @Before annotation. The argument that we provide to the annotation is a pointcut expression. Pointcut expressions assist us in defining the function that will trigger our advice, and the function arguments that should be used.

Therefore, before the method createSample gets executed, a log message should be displayed on our screen.

Suppose that we want to have more action before and after the method gets executed, or even change the outcome of the createSample function. We can use an @Around Advice.

package com.gkatzioura.spring.aop.aspect;

import com.gkatzioura.spring.aop.model.Sample;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Created by gkatzioura on 5/28/16.
 */
@Aspect
@Component
public class SampleServiceAspect {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleServiceAspect.class);

    @Before("execution(* com.gkatzioura.spring.aop.service.SampleService.createSample (java.lang.String)) && args(sampleName)")
    public void beforeSampleCreation(String sampleName) {

        LOGGER.info("A request was issued for a sample name: "+sampleName);
    }

    @Around("execution(* com.gkatzioura.spring.aop.service.SampleService.createSample (java.lang.String)) && args(sampleName)")
    public Object aroundSampleCreation(ProceedingJoinPoint proceedingJoinPoint,String sampleName) throws Throwable {

        LOGGER.info("A request was issued for a sample name: "+sampleName);

        sampleName = sampleName+"!";

        Sample sample = (Sample) proceedingJoinPoint.proceed(new Object[] {sampleName});
        sample.setName(sample.getName().toUpperCase());

        return sample;
    }
}

As we can see the aroundSampleCreation Advice changes the input and also changes the outcome.
You can find the source code on GitHub.

Aspect-oriented programming Aspect (computer programming) Spring Framework

Published at DZone with permission of Emmanouil Gkatziouras, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Magic of Aspects: How AOP Works in Spring
  • Comprehensive Guide to Unit Testing Spring AOP Aspects
  • Adding Versatility to Java Logging Aspect
  • How to Marry MDC With Spring Integration

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: