Quarkus - Quarkus Extension for Spring DI API

    This guide explains how a Quarkus application can leverage the well known Dependency Injection annotations included in the Spring Framework.

    To complete this guide, you need:

    • less than 15 minutes

    • an IDE

    • JDK 1.8+ installed with JAVA_HOME configured appropriately

    • Apache Maven 3.5.3+

    Solution

    We recommend that you follow the instructions in the next sections and create the application step by step.However, you can go right to the completed example.

    Clone the Git repository: git clone , or download an archive.

    Creating the Maven project

    First, we need a new project. Create a new project with the following command:

    This command generates a Maven project with a REST endpoint and imports the spring-di extension.

    Let’s proceed to create some beans using various Spring annotations.

    First we will create a StringFunction interface that some of our beans will implement and which will be injected into another bean later on.Create a src/main/java/org/acme/spring/di/StringFunction.java file and set the following content:

    1. package org.acme.spring.di;
    2. import java.util.function.Function;
    3. public interface StringFunction extends Function<String, String> {
    4. }

    With the interface in place, we will add an AppConfiguration class which will use the Spring’s Java Config style for defining a bean.It will be used to create a StringFunction bean that will capitalize the text passed as parameter.Create a src/main/java/org/acme/spring/di/AppConfiguration.java file with the following content:

    1. package org.acme.spring.di;
    2. import org.springframework.context.annotation.Bean;
    3. import org.springframework.context.annotation.Configuration;
    4. @Configuration
    5. public class AppConfiguration {
    6. @Bean(name = "capitalizeFunction")
    7. return String::toUpperCase;
    8. }
    9. }

    Now we define another bean that will implement StringFunction using Spring’s stereotype annotation style.This bean will effectively be a no-op bean that simply returns the input as is.Create a src/main/java/org/acme/spring/di/NoOpSingleStringFunction.java file and set the following content:

    Quarkus also provides support for injecting configuration values using Spring’s @Value annotation.To see that in action, first edit the src/main/resources/application.properties with the following content:

    1. greeting.message = hello

    Next create a new Spring bean in src/main/java/org/acme/spring/di/MessageProducer.java with the following content:

    1. package org.acme.spring.di;
    2. import org.springframework.beans.factory.annotation.Value;
    3. import org.springframework.stereotype.Service;
    4. @Service
    5. public class MessageProducer {
    6. @Value("${greeting.message}")
    7. String message;
    8. public String getPrefix() {
    9. return message;
    10. }
    11. }

    In the code above, we see that both field injection and constructor injection are being used (note that constructor injection does not need the @Autowired annotation since there is a single constructor).Furthermore, the @Value annotation on suffix has also a default value defined, which in this case will be used since we have not defined greeting.suffix in application.properties.

    Open the src/main/java/org/acme/spring/di/GreeterResource.java file and update it with the following content:

    1. package org.acme.spring.di;
    2. import org.springframework.beans.factory.annotation.Autowired;
    3. import javax.ws.rs.GET;
    4. import javax.ws.rs.Path;
    5. import javax.ws.rs.Produces;
    6. import javax.ws.rs.core.MediaType;
    7. @Autowired
    8. GreeterBean greeterBean;
    9. @GET
    10. @Produces(MediaType.TEXT_PLAIN)
    11. public String hello() {
    12. return greeterBean.greet("world");
    13. }
    14. }

    Update the test

    We also need to update the functional test to reflect the changes made to the endpoint.Edit the src/test/java/org/acme/spring/di/GreetingResourceTest.java file and change the content of the testHelloEndpoint method to:

    1. import io.quarkus.test.junit.QuarkusTest;
    2. import org.junit.jupiter.api.Test;
    3. import static io.restassured.RestAssured.given;
    4. import static org.hamcrest.CoreMatchers.is;
    5. @QuarkusTest
    6. public class GreetingResourceTest {
    7. @Test
    8. public void testHelloEndpoint() {
    9. given()
    10. .when().get("/greeting")
    11. .then()
    12. .statusCode(200)
    13. .body(is("HELLO WORLD!"));
    14. }
    15. }

    Package and run the application

    Run the application with: ./mvnw compile quarkus:dev.Open your browser to http://localhost:8080/greeting.

    The result should be: .

    You can of course create a native image using instructions similar to guide.

    Important Technical Note

    Please note that the Spring support in Quarkus does not start a Spring Application Context nor are any Spring infrastructure classes run.Spring classes and annotations are only used for reading metadata and / or are used as user code method return types or parameter types.What that means for end users, is that adding arbitrary Spring libraries will not have any effect. Moreover Spring infrastructureclasses (like org.springframework.beans.factory.config.BeanPostProcessor for example) will not be executed.

    More Spring guides

    Quarkus supports additional Spring compatibility features. See the following guides for more details: