Lesson 15. Quarkus syntax

Quarkus has its own syntax and configuration style, similar to Spring Boot, but optimized for GraalVM and cloud-native environments. Here’s how it differs and what makes it unique:


1. Dependency Injection (CDI)

Quarkus uses Jakarta CDI (Contexts and Dependency Injection) but with a build-time optimization approach.

Example: CDI in Quarkus

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class GreetingService {
    public String greet(String name) {
        return "Hello, " + name;
    }
}

🔹 In Spring Boot, this would be done using @Service.


2. REST Endpoints with JAX-RS

Quarkus follows the JAX-RS standard instead of Spring’s @RestController.

Example: REST API in Quarkus

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class HelloResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        return "Hello, Quarkus!";
    }
}

🔹 In Spring Boot, the equivalent would be @RestController + @RequestMapping.


3. Configuration: application.properties vs application.yaml

Quarkus uses MicroProfile Config for configuration.

Example: Configuring a property in Quarkus

application.properties

greeting.message=Hello from Quarkus!

Inject it into a Java class:

import org.eclipse.microprofile.config.inject.ConfigProperty;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class GreetingConfig {

    @ConfigProperty(name = "greeting.message")
    String message;

    public String getMessage() {
        return message;
    }
}

🔹 In Spring Boot, you would use @Value("${greeting.message}").


4. Hibernate with Panache (Simplified ORM)

Quarkus provides Hibernate ORM with Panache for easy entity management.

Example: Quarkus Panache Entity

import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;

@Entity
public class Person extends PanacheEntity {
    public String name;
}

🔹 In Spring Boot, you would use @Entity + @Repository + JpaRepository<Person, Long>.


5. Dependency Injection Alternatives: Arc

Quarkus provides Arc, a lightweight CDI alternative for even faster performance.

Example: Injecting Beans using Arc

import io.quarkus.arc.DefaultBean;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
@DefaultBean
public class MyBean {
    public String sayHello() {
        return "Hello from Arc!";
    }
}

🔹 Spring Boot uses @Component or @Service.


6. Reactive Programming with Mutiny

Quarkus offers Mutiny, an alternative to Spring’s WebFlux.

Example: Quarkus Reactive API

import io.smallrye.mutiny.Uni;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/reactive")
public class ReactiveResource {

    @GET
    public Uni<String> sayHello() {
        return Uni.createFrom().item("Hello, Reactive Quarkus!");
    }
}

🔹 Spring Boot would use Mono or Flux from Project Reactor.


7. GraalVM Native Image Support

Unlike Spring Boot, Quarkus optimizes for GraalVM by default.

./mvnw package -Dnative

🔹 In Spring Boot, this requires Spring Native (not fully optimized).


Conclusion

Quarkus focuses on lightweight, cloud-native performance.
✅ Uses JAX-RS, CDI, Panache, and Mutiny instead of Spring’s annotations.
Better startup time and memory efficiency compared to Spring Boot.