Referenced dependency access

One workaround for circular dependency access, is to use Refs. A Ref<T> features lazy access to a dependency.

@Service
class ServiceA {
    Ref<ServiceB> serviceB;
    
    void foo() {
        serviceB.get().baz();
    }
    
    void bar() {
        System.out.println("Hello!");
    }
}

@Service
class ServiceB {
    Ref<ServiceA> serviceA;
    
    void baz() {
        serviceA.get().bar();
    }
}

void useCircularRefs() {
    Container.get(ServiceA.class).foo(); // will print `Hello!`
}

Last updated