Skip to content

@Primary ignored when @Lazy present #936

@fdyokanka

Description

@fdyokanka

Environment

avaje-inject: 12.0
Java: 25

Single-File Reproducer

SpanishGreeting is marked as @primary but EnglishGreeting gets injected instead. If I remove @lazy(Lazy.Kind.PROVIDER) from the classes, SpanishGreeting gets injected correctly.

import io.avaje.inject.BeanScope;
import io.avaje.inject.Lazy;
import io.avaje.inject.Primary;
import jakarta.inject.Singleton;

interface GreetingService {
  String greet();
}

@Lazy(Lazy.Kind.PROVIDER)
@Singleton
class EnglishGreeting implements GreetingService {
  @Override public String greet() { return "Hello"; }
}

@Primary
@Lazy(Lazy.Kind.PROVIDER)
@Singleton
class SpanishGreeting implements GreetingService {
  @Override public String greet() { return "Hola"; }
}

@Lazy(Lazy.Kind.PROVIDER)
@Singleton
class Greeter {
  private final GreetingService service;
  Greeter(GreetingService service) { this.service = service; }
  String run() { return service.greet(); }
}

// Minimal test/runner
public class PrimaryLazyRepro {
  public static void main(String[] args) {
    try (var scope = BeanScope.builder().build()) {
      var greeter = scope.get(Greeter.class);
      String actual = greeter.run();
      System.out.println("Selected: " + actual);
      if (!"Hola".equals(actual)) {
        throw new IllegalStateException("Expected @Primary to be selected, got: " + actual);
      }
    }
  }
}

Question

Is this the intended behavior when using @lazy, or is it a bug?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions