Spring-AOP-Example

Code examples for AOP & custom annotations in spring boot application


Project maintained by hqrd Hosted on GitHub Pages — Theme by mattgraham

Example project using AOP annotations


Usage

Launch the spring boot application and call http://localhost:8080/employee/list?onlyOne=false&test=ok

Or go to http://localhost:8080/swagger-ui.html for the swagger ui interface.


Custom annotation

The @CustomController annotation is defined in package com.hqrd.springaop.annotation;

It uses the @AliasFor annotation to use multiple annotations at once and simplify the duplication of controller methods without forgetting an annotation.

A custom SwaggerBuilder class is used to override the swagger annotations @ApiResponses and @ApiOperation because they required some default values. It uses the springfox pluggin to redefines the swagger variables after the default ones are defined.


AOP code

The CustomControllerAspect class uses the @Aspect annotation to execute some custom code on certain methods. Each method in this class uses an annotation to define on which method the code will be added (@Around is used to execute some code before and after the execution of the targeted method).

You then have access to multiple variables, like the class, arguments or the response returned.


Base project inspired from https://howtodoinjava.com/spring/spring-mvc/spring-mvc-hello-world-example/