Friday, 3 October 2025

How to Create a Pure Java Service-Layer JAR from a Spring Boot Project

When working on a Spring Boot application, the service layer often contains the business logic that you may want to reuse in other projects. But what if you want to create a JAR of only the service layer and use it in a plain Java project without any Spring dependencies? This guide walks you through the process step by step.


Why Separate the Service Layer?

Separating the service layer into its own module or project has several advantages:

  • Reusability: You can use it in multiple applications, including plain Java apps.

  • Decoupling: Keeps business logic separate from controllers or web-specific code.

  • Simpler Testing: Service logic can be tested independently of web or Spring Boot context.

Step 1: Remove Spring Dependencies

Spring annotations like @Service, @Component, @Autowired, or @Transactional won’t work in a plain Java application because they rely on Spring’s dependency injection and application context.

You need to:

  • Remove all Spring annotations.

  • Replace @Autowired dependencies with constructor or setter injection.

  • Handle transactions manually if needed.

Before (Spring Boot Service):

@Service

public class MyService {


    @Autowired

    private MyRepository repo;


    @Transactional

    public void doSomething() {

        repo.saveData();

    }

}

After (Plain Java Service):
public class MyService {

    private final MyRepository repo;

    public MyService(MyRepository repo) {
        this.repo = repo;
    }

    public void doSomething() {
        repo.saveData();
    }
}

Step 2: Prepare the Service Layer Module

Organize your project into a separate module for the service layer. Example structure:

my-app

├─ service-layer       <-- This will become the JAR

│   └─ src/main/java/... (all service classes)

│   └─ pom.xml

├─ web-layer           <-- Spring Boot app (controllers)

│   └─ src/main/java/...

│   └─ pom.xml

└─ pom.xml             <-- parent pom

Step 3: Create a Minimal pom.xml

Since you don’t want any Spring dependency, the pom.xml is very simple:

<project>

    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>

    <artifactId>service-layer</artifactId>

    <version>1.0.0</version>

    <packaging>jar</packaging>


    <dependencies>

        <!-- Include only libraries your service actually needs -->

    </dependencies>

</project>

Only include libraries like JDBC drivers, Redis client, or other third-party APIs if your service uses them.

Step 4: Build the JAR

From the service-layer module folder, run:

mvn clean install

service-layer/target/service-layer-1.0.0.jar

This JAR now contains only your service layer and no controllers or Spring Boot classes.

Step 5: Use the JAR in a Plain Java Application

Add the JAR to your Java project classpath (or as a Maven dependency if installed in the local repo) and use it like any normal Java library:

public class Main {

    public static void main(String[] args) {

        MyRepository repo = new MyRepository();

        MyService service = new MyService(repo);


        service.doSomething();

    }

}

No Spring required, everything works in pure Java.


Key Takeaways

  1. Spring annotations do nothing in a plain Java app. To use your service layer outside Spring Boot, remove them.

  2. Use constructor or setter injection to manage dependencies manually.

  3. Keep only necessary dependencies in the service-layer pom.xml to make it lightweight.

  4. Build as a JAR using Maven and reuse it anywhere, including plain Java projects.

This approach allows you to decouple your business logic from Spring, making your service layer reusable, lightweight, and independent of any specific framework.


 

No comments:

Post a Comment