spring-attic / spring-native

Spring Native is now superseded by Spring Boot 3 official native support
https://docs.spring.io/spring-boot/docs/current/reference/html/native-image.html
Apache License 2.0
2.74k stars 355 forks source link

@Order doesnt work with native build #1694

Closed thekalinga closed 2 years ago

thekalinga commented 2 years ago

When I create a native image of spring application that uses @Order, native executable doesnt respect @Order specified while the non-native image version respects it.

  interface Contract {
    void perform();
  }

  @Component
  @Order(1)
  static class FirstContract implements Contract {
    @Override
    public void perform() {
      System.out.println("First contract: Done");
    }
  }

  @Component
  @Order(555)
  static class SecondContract implements Contract {
    @Override
    public void perform() {
      System.out.println("Second contract: Done");
    }
  }

  @Component
  @Order(9999)
  static class LastContract implements Contract {
    @Override
    public void perform() {
      System.out.println("Last contract: Done");
    }
  }

  @Bean
  ApplicationRunner onInit(List<Contract> contracts) {
    return args -> contracts.forEach(Contract::perform);
  }

When I run native image (./gradlew nativeCompile) I get the following output (ordered based on classname)

First contract: Done
Last contract: Done
Second contract: Done

If I run the same program in regular JVM, I get the following output (correctly ordered based on @Order)

First contract: Done
Second contract: Done
Last contract: Done

Version info

  1. Java
    openjdk version "17.0.4" 2022-07-19
    OpenJDK Runtime Environment Temurin-17.0.4+8 (build 17.0.4+8)
    OpenJDK 64-Bit Server VM Temurin-17.0.4+8 (build 17.0.4+8, mixed mode, sharing)
  2. OS Ubuntu 20.04.4 LTS
  3. Graal VM 22.2.r17-grl
  4. Spring & Gradle versions can be found can be found at this minimum reproducible sample

PS: I havent tried with org.springframework.core.Ordered, javax.annotation.Priority, org.springframework.core.PriorityOrdered & other ways to specify order. Issue might exist even there too. Please check if the issue occurs even with that.

sdeleuze commented 2 years ago

@snicoll Same happen with Spring Boot 3 snapshots, should we ask creating an issue on Spring Framework 6 side?

thekalinga commented 2 years ago

Thanks for the fix. Does this fix address all of these aswell?

PS: I havent tried with org.springframework.core.Ordered, javax.annotation.Priority, org.springframework.core.PriorityOrdered & other ways to specify order. Issue might exist even there too. Please check if the issue occurs even with that.