Creating Custom Spring Boot Starter To Solve Cross-Cutting Concerns

A guide to build custom starter to solve cross-cutting concerns by leveraging spring’s autoconfiguration and AOP.

Photo by Clément H on Unsplash

What is Spring Boot Auto Configuration

# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration,\
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ CqlSession.class})
@EnableConfigurationProperties(CassandraProperties.class)
public class CassandraAutoConfiguration {
// Configuration code and other methods.
}

Spring Boot AOP

AOP flow digram

Custom Starter with Spring Boot

Let’s Create a starter project that logs Method Execution Time

Create a service project with AOP to log method execution time

TrackTime annotation for AOP
Aspect defining the point cut on annotation TrackTime with advice type Around

Creating our own Autoconfiguration project

spring.factories File

spring.factories

Creating Our Custom Spring Boot Starter Project

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring.boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.vishnu.aop</groupId>
<artifactId>logging-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.vishnu.boot</groupId>
<artifactId>logging-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

How to use the custom starter?

<dependency>
<groupId>org.vishnu.boot</groupId>
<artifactId>logging-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
TrackTime annotation will log the execution time of the Api.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store