ServGen
A Java annotation processor born out of laziness, generating repetitive Spring Boot service methods at compile time.
ServGen came out of pure laziness. I kept building MVP on MVP on MVP for school assignments, and every single one needed the same repetitive service layer: findById, save, findAll, deleteById, existsById, over and over for every entity. Writing that by hand for the tenth time felt like a waste, so I built an annotation processor that generates it at compile time instead.
Now instead of writing a full service class, I just annotate it with @ServGen and extend the generated base class. The boilerplate shows up for free, and I only write the actual business logic. There is also a Spring Boot demo application with tests of all the generic methods and integration tests, to prove it actually works.
Before ServGen:
@Service
public class CarService {
private final CarRepository repository;
public CarService(CarRepository repository) {
this.repository = repository;
}
public Car findById(Long id) {
return repository.findById(id).orElse(null);
}
// ...and the same boilerplate again for save, findAll, deleteById, existsById
// Your actual business logic
public List<Car> findAllRedCars() {
return findAll().stream()
.filter(car -> "red".equalsIgnoreCase(car.getColor()))
.collect(Collectors.toList());
}
}After ServGen:
@Service
@ServGen(entity = Car.class, repository = CarRepository.class)
public class CarService extends BaseCarService {
public CarService(CarRepository repository) {
super(repository);
}
// Just write your business logic
public List<Car> findAllRedCars() {
return findAll().stream()
.filter(car -> "red".equalsIgnoreCase(car.getColor()))
.collect(Collectors.toList());
}
}
The CRUD methods get generated at compile-time, without reflection and runtime overhead.
Installation
Add these to your pom.xml:
<dependencies>
<!-- Runtime annotation -->
<dependency>
<groupId>io.servgen</groupId>
<artifactId>servgen-annotation</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- Compile-time processor -->
<dependency>
<groupId>io.servgen</groupId>
<artifactId>servgen-processor</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>Usage
- Create your entity and repository like normal
- Annotate your service class with
@ServGen - Extend the generated base class
- Add your custom business methods
@Entity
public class Car {
@Id @GeneratedValue
private Long id;
private String color;
private int regNo;
// getters/setters
}
@Repository
public interface CarRepository extends JpaRepository<Car, Long> {
}
The service itself looks exactly like the "After ServGen" example above.
How It Works
ServGen is an annotation processor that runs during compilation. When it finds @ServGen, it generates an abstract base class with standard CRUD methods. Your service extends this base class and inherits all the methods.
The generated BaseCarService looks something like:
public abstract class BaseCarService {
protected final CarRepository repository;
public BaseCarService(CarRepository repository) {
this.repository = repository;
}
public Car findById(Long id) {
return repository.findById(id).orElse(null);
}
public Car save(Car entity) {
return repository.save(entity);
}
// ... other CRUD methods
}Generated Methods
Every @ServGen service gets these methods for free:
findById(Long id): Find entity by IDsave(T entity): Save or update entityfindAll(): Get all entitiesdeleteById(Long id): Delete by IDexistsById(Long id): Check if entity exists
Why Not Just Spring Data JPA?
Spring Data JPA gives you repository methods. ServGen gives you service layer methods. Sometimes you want business logic in your service that's more than just repository calls.
Also, this way you can mock your service in tests without dealing with JPA complexity.
Requirements
- Java 21+
- Spring Boot 3.x
- Maven or Gradle
License
Copyright © Valdemar Støvring Storgaard, December 2025