Return to site

💥 QUARKUS VS SPRING: DECLARING A SERVICE THE CLEAN WAY

· quarkus,java,springboot,spring
Section image

🚀 QUARKUS WAY

@ApplicationScoped

public class LoggerService {

@Inject

@ConfigProperty(name = "logging.verbose", defaultValue = "false")

boolean verbose;

public void log(String message) {

if (verbose) System.out.println("[VERBOSE] " + message);

}

}

🌱 SPRING WAY

@Service

public class LoggerService {

@Value("${logging.verbose:false}")

private boolean verbose;

public void log(String message) {

if (verbose) System.out.println("[VERBOSE] " + message);

}

}

🔍 KEY DIFFERENCES

🔸 Bean discovery

- Quarkus: CDI annotations (@ApplicationScoped, @Inject).

- Spring: Stereotypes (@Service, @Component) + @Autowired (constructor injection recommended).

🔸 Scope

- Quarkus: @ApplicationScoped = app-wide singleton.

- Spring: default scope is singleton via @Service.

🔸 Config

- Quarkus: MicroProfile Config (@ConfigProperty).

- Spring: @Value or @ConfigurationProperties.

🔸 Startup & footprint

- Quarkus favors fast startup & low RSS (great for containers/GraalVM).

- Spring offers rich ecosystem & autoconfiguration.

🔸 Testing & mocks

- Quarkus: @QuarkusTest, @InjectMock.

- Spring: @SpringBootTest, @MockBean.

🔁 MIGRATION TIPS (SPRING ➜ QUARKUS)

- Replace @Service/@Component ➜ @ApplicationScoped (or appropriate CDI scope).

- Swap @Autowired/@Value ➜ @Inject and @ConfigProperty.

- Move Boot starters ➜ Quarkus extensions (quarkus-smallrye-config, quarkus-rest, etc.).

- Replace Spring annotations/APIs with CDI/JAX-RS counterparts (e.g., @RestController ➜ @Path + @GET).

- Tests: @SpringBootTest ➜ @QuarkusTest, @MockBean ➜ @InjectMock.

- Use the Quarkus Dev Mode (mvn quarkus:dev) to iterate quickly and catch config/DI issues early.

✅ TAKEAWAYS

- Same concept, different idioms: CDI (Quarkus) vs Spring stereotypes.

- Config is first-class in both; choose MicroProfile Config or Spring Config accordingly.

- Quarkus shines for container/GraalVM use cases; Spring shines with its ecosystem & autoconfig.

- Migration is mostly annotation & API mapping—start with DI, config, and web layers.

#️⃣ #Java #Quarkus #Spring #SpringBoot #CDI #JakartaEE #MicroProfile #Microservices #CleanCode #DevTips #CloudNative #GraalVM