Spring Boot
Common Library
Java Programming
Software Development
Spring Framework

How to use spring boot making a common library

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

A shared Spring Boot library can eliminate duplicate infrastructure code across services. It works well when the library focuses on stable cross-cutting concerns such as logging, tracing, validation helpers, or security conventions. It becomes harmful when business-specific logic leaks into the shared module.

Core Sections

1. Define strict scope for shared code

Before writing code, decide what the common library owns:

  • reusable configuration
  • utility components
  • integration clients used by many services
  • standardized error and observability support

Avoid putting feature-specific workflow logic in the library. Shared modules should move slowly and remain broadly applicable.

2. Create a minimal dependency library module

Use regular JAR packaging and keep dependencies lean. Heavy transitive dependencies increase classpath conflicts in downstream services.

xml
1<project>
2  <modelVersion>4.0.0</modelVersion>
3  <groupId>com.acme.platform</groupId>
4  <artifactId>platform-common</artifactId>
5  <version>1.0.0</version>
6  <packaging>jar</packaging>
7
8  <dependencies>
9    <dependency>
10      <groupId>org.springframework.boot</groupId>
11      <artifactId>spring-boot-autoconfigure</artifactId>
12    </dependency>
13  </dependencies>
14</project>

Only add dependencies that are required for library functionality.

3. Provide auto-configuration for plug-and-play adoption

Spring Boot auto-configuration is the cleanest way to expose reusable beans.

java
1package com.acme.platform.common;
2
3import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
4import org.springframework.context.annotation.Bean;
5import org.springframework.context.annotation.Configuration;
6
7@Configuration
8public class CommonAutoConfiguration {
9
10    @Bean
11    @ConditionalOnMissingBean
12    public RequestIdGenerator requestIdGenerator() {
13        return new RequestIdGenerator();
14    }
15}

Register configuration for Spring Boot discovery:

text
# META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.acme.platform.common.CommonAutoConfiguration

Consumers get defaults while retaining override capability.

4. Expose configuration properties for controlled behavior

Shared behavior should be configurable, not forced globally. Use configuration properties for feature toggles and tuning.

java
1package com.acme.platform.common;
2
3import org.springframework.boot.context.properties.ConfigurationProperties;
4
5@ConfigurationProperties(prefix = "common.request-id")
6public class RequestIdProperties {
7    private boolean enabled = true;
8
9    public boolean isEnabled() {
10        return enabled;
11    }
12
13    public void setEnabled(boolean enabled) {
14        this.enabled = enabled;
15    }
16}

With explicit properties, teams can adopt features progressively.

5. Consume the library in services predictably

In service projects, add dependency with explicit version:

xml
1<dependency>
2  <groupId>com.acme.platform</groupId>
3  <artifactId>platform-common</artifactId>
4  <version>1.0.0</version>
5</dependency>

Then inject shared beans normally:

java
1@Service
2public class InvoiceService {
3    private final RequestIdGenerator requestIdGenerator;
4
5    public InvoiceService(RequestIdGenerator requestIdGenerator) {
6        this.requestIdGenerator = requestIdGenerator;
7    }
8
9    public String createRequestId() {
10        return requestIdGenerator.newId();
11    }
12}

This keeps service code clean while preserving central control.

6. Versioning and release discipline

Treat the common library as a product with semantic versioning:

  • major for breaking API changes
  • minor for backward-compatible additions
  • patch for fixes

Publish release notes with migration instructions. Consumers should know what changed and whether config updates are required.

7. Testing strategy for shared libraries

Library tests should include:

  • unit tests for core classes
  • auto-configuration tests using Spring context runner
  • one sample consumer integration test

Consumer-style tests catch classpath and bean-loading issues that unit tests alone miss.

8. Publish through artifact repository and avoid copy-based reuse

Do not copy shared classes between repositories. Publish library versions to an artifact repository and consume by versioned dependency. This gives reproducible builds, traceable rollbacks, and clean upgrade planning.

For release automation, add CI steps for dependency checks, tests, and publish actions only on tagged versions.

Common Pitfalls

  • Putting service-specific business logic into the shared module.
  • Shipping heavy transitive dependencies that conflict downstream.
  • Introducing breaking changes without major version increment.
  • Enforcing behavior without opt-in configuration flags.
  • Skipping consumer integration tests for auto-configuration.

Summary

  • Keep Spring Boot common libraries narrow, stable, and cross-cutting.
  • Use auto-configuration plus override-friendly bean design.
  • Expose properties for controlled adoption across services.
  • Version and publish the library like a product.
  • Validate with both internal tests and consumer-style integration checks.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.