Search Tutorials

Spring Boot AOP Integration Hello World Example | JavaInUse

Spring Boot + AOP Simple Example

In this post we develop a Spring Boot Application with AOP(Apsect Oriented Programming).

What is AOP?

  • During software development, functions that span multiple points of an application are called cross-cutting concerns. These cross-cutting concerns differ from the application’s main business logic. Hence ,separating these cross-cutting concerns from the business logic is where aspect-oriented programming (AOP) comes into picture.
  • Using AOP, we define the common functionality in one place, but we can declaratively define how and where this functionality is applied without having to modify the class to which we are applying the new feature. Cross-cutting concerns can now be modularized into special classes called aspects. This has two benefits. First, the logic for each concern is now in one place, as opposed to being scattered all over the code base. Second, our business modules are now cleaner since they only contain code for their primary concern and secondary concerns have been moved to aspects.
  • Aspects have responsibility they are meant to discharge. This responsibility of an aspect is called advice. An aspect's functionality is woven into a program’s execution at one or more join points

  • In addition to describing the responsibility that an aspect will discharge, advice addresses the question of when to discharge this responsibility. Spring aspects can work with five kinds of advice:
    • Before - the advice functionality takes place before the advised method is invoked.
    • After - the advice functionality takes place after the advised method completes, regardless of the outcome.
    • AfterReturning - returning the advice functionality takes place after the advised method successfully completes.
    • AfterThrowing - throwing the advice functionality takes place after the advised method throws an exception.
    • Around - the advice wraps the advised method, providing some functionality before and after the advised method is invoked.

In Spring, aspects are woven into Spring-managed beans at runtime by wrapping them with a proxy class. The proxy class poses as the target bean, intercepting advised method calls and forwarding those calls to the target bean. Between the time when the proxy intercepts the method call and the time when it invokes the target bean's method, the proxy performs the aspect logic.


This tutorial is explained in the below Youtube Video.

Lets Begin-

The Spring Boot Application we will be creating is as follows-
Maven Project will be as follows-


In the Maven we need the spring boot test dependency.Maven will be as follows-

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->




Create the Employee model class as follows-
package com.javainuse.model;

public class Employee {
	private String empId;
	private String name;

	public Employee() {

	public String getName() {
		return name;

	public void setName(String name) { = name;

	public String getEmpId() {
		return empId;

	public void setEmpId(String empId) {
		this.empId = empId;


@RequestMapping maps /employee request to return an employee object. method.
package com.javainuse.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.javainuse.model.Employee;
import com.javainuse.service.EmployeeService;

public class EmployeeController {

	private EmployeeService employeeService;

	@RequestMapping(value = "/add/employee", method = RequestMethod.GET)
	public Employee addEmployee(@RequestParam("name") String name, @RequestParam("empId") String empId) {

		return employeeService.createEmployee(name, empId);


	@RequestMapping(value = "/remove/employee", method = RequestMethod.GET)
	public String removeEmployee( @RequestParam("empId") String empId) {


		return "Employee removed";

Create the service as follows-
package com.javainuse.service;

import org.springframework.stereotype.Service;

import com.javainuse.model.Employee;

public class EmployeeService {

	public Employee createEmployee(String name, String empId) {

		Employee emp = new Employee();
		return emp;

	public void deleteEmployee(String empId) {

Create the Aspect class that define the joinpoints and the pointcuts.
package com.javainuse.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

public class EmployeeServiceAspect {

	@Before(value = "execution(* com.javainuse.service.EmployeeService.*(..)) and args(name,empId)")
	public void beforeAdvice(JoinPoint joinPoint, String name, String empId) {
		System.out.println("Before method:" + joinPoint.getSignature());

		System.out.println("Creating Employee with name - " + name + " and id - " + empId);

	@After(value = "execution(* com.javainuse.service.EmployeeService.*(..)) and args(name,empId)")
	public void afterAdvice(JoinPoint joinPoint, String name, String empId) {
		System.out.println("After method:" + joinPoint.getSignature());

		System.out.println("Successfully created Employee with name - " + name + " and id - " + empId);

Create the as below-
package com.javainuse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

public class SpringBootHelloWorldApplication {

	public static void main(String[] args) {, args);
Compile and the run the as a Java application.
Go to localhost:8080/add/employee
We will see in the console that the advice has been applied.

Download Source Code

Download it -
Spring Boot + AOP

See Also

Spring Boot Hello World Application- Create simple controller and jsp view using Maven Spring Boot Tutorial-Spring Data JPA Spring Boot + Simple Security Configuration Pagination using Spring Boot Simple Example Spring Boot + ActiveMQ Hello world Example Spring Boot + Swagger Example Hello World Example Spring Boot + Swagger- Understanding the various Swagger Annotations Spring Boot Main Menu Spring Boot Interview Questions