| 1 | # Advanced dependency resolution: default provider methods |
| 2 | |
| 3 | This feature (and this document) is aimed at platform developers who intend to provide additional platform features. It |
| 4 | is **not** intended for people who simply want to contribute a technology extension, such as a database implementation |
| 5 | or a storage backend! |
| 6 | |
| 7 | In this document we will use the term "default provider" and "default provider method" synonymously to refer to a method |
| 8 | annotated with `@Provider(isDefault=true)`. Similarly, "provider", "provider method" or "factory method" |
| 9 | refer to methods annotated with just `@Provider`. |
| 10 | |
| 11 | ## Fallbacks versus extensibility |
| 12 | |
| 13 | Default provider methods are intended to provide fallback implementations for services rather than to achieve |
| 14 | extensibility - that is what extensions are for. There is a subtle but important semantic difference between _fallback |
| 15 | implementations_ and _extensibility_: |
| 16 | |
| 17 | ### Fallback implementations |
| 18 | |
| 19 | Fallbacks are meant as safety net, in case developers forget or don't want to add a specific implementation for a |
| 20 | service. It is there so as not to end up _without_ an implementation for a service interface. A good example for this |
| 21 | are in-memory store implementations. It is expected that an actual persistence implementation is contributed by another |
| 22 | extension. In-mem stores get you up and running quickly, but we wouldn't recommend using them in production |
| 23 | environments. Typically, fallbacks should not have any dependencies onto other services. |
| 24 | |
| 25 | > Default-provided services, even though they are on the classpath, only get instantiated if there is no other |
| 26 | > implementation. |
| 27 | |
| 28 | ### Extensibility |
| 29 | |
| 30 | In contrast, _extensibility_ refers to the possibility of swapping out one implementation of a service for another by |
| 31 | choosing the respective module at compile time. Each implementation must therefore be contained in its own java module, |
| 32 | and the choice between one or the other is made by referencing one or the other in the build file. The service |
| 33 | implementation is typically instantiated and provided by its own extension. In this case, the `@Provider`-annotation ** |
| 34 | must not** have the `isDefault` attribute. This is also the case if there will likely only ever be one implementation |
| 35 | for a service. |
| 36 | |
| 37 | One example for extensibility is the `IdentityService`: there could be several implementations for it (OAuth, |
| 38 | DecentralizedIdentity, Keycloak etc.), but providing either one as default would make little sense, because all of them |
| 39 | require external services to work. Each implementation would be in its own module and get instantiated by its own |
| 40 | extension. |
| 41 | |
| 42 | > Provided services get instantiated only if they are on the classpath, but always get instantiated. |
| 43 | |
| 44 | ## Deep-dive into extension lifecycle management |
| 45 | |
| 46 | Generally speaking every extension goes through these lifecycle stages during loading: |
| 47 | |
| 48 | - `inject`: all fields annotated with `@Inject` are resolved |
| 49 | - `initialize`: the `initialize()` method is invoked. All required collaborators are expected to be resolved after this. |
| 50 | - `provide`: all `@Provider` methods are invoked, the object they return is registered in the context. |
| 51 | |
| 52 | Due to the fact that default provider methods act a safety net, they only get invoked if no other provider method offers |
| 53 | the same service type. However, what may be a bit misleading is the fact that they typically get invoked _during the |
| 54 | `inject` phase_. The following section will demonstrate this. |
| 55 | |
| 56 | ### Example 1 - provider method |
| 57 | |
| 58 | Recall that `@Provider` methods get invoked regardless, and after the `initialze` phase. That means, assuming both |
| 59 | extensions are on the classpath, the extension that declares the provider method (= `ExtensionA`) will get fully |
| 60 | instantiated before another extension (= `ExtensionB`) can use the provided object: |
| 61 | |
| 62 | ```java |
| 63 | public class ExtensionA { // gets loaded first |
| 64 | @Inject |
| 65 | private SomeStore store; // provided by some other extension |
| 66 | |
| 67 | @Provider |
| 68 | public SomeService getSomeService() { |
| 69 | return new SomeServiceImpl(store); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public class ExtensionB { // gets loaded second |
| 74 | @Inject |
| 75 | private SomeService service; |
| 76 | } |
| 77 | ``` |
| 78 | |
| 79 | After building the dependency graph, the loader mechanism would first fully construct `ExtensionA`, i.e. |
| 80 | `getSomeService()` is invoked, and the instance of `SomeServiceImpl` is registered in the context. Note that this is |
| 81 | done regardless whether another extension _actually injects a `SomeService`_. After that, `ExtensionB` gets constructed, |
| 82 | and by the time it goes through its `inject` phase, the injected `SomeService` is already in the context, so the |
| 83 | `SomeService` field gets resolved properly. |
| 84 | |
| 85 | ### Example 2 - default provider method |
| 86 | |
| 87 | Methods annotated with `@Provider(isDefault=true)` only get invoked if there is no other provider method for that |
| 88 | service, and at the time when the corresponding `@Inject` is resolved. Modifying example 1 slightly we get: |
| 89 | |
| 90 | ```java |
| 91 | public class ExtensionA { |
| 92 | |
| 93 | @Inject |
| 94 | private SomeStore store; |
| 95 | |
| 96 | @Provider(isDefault = true) |
| 97 | public SomeService getSomeService() { |
| 98 | return new SomeServiceImpl(store); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public class ExtensionB { |
| 103 | @Inject |
| 104 | private SomeService service; |
| 105 | } |
| 106 | ``` |
| 107 | |
| 108 | The biggest difference here is the point in time at which `getSomeService` is invoked. Default provider methods get |
| 109 | invoked _when the `@Inject` dependency is resolved_, because that is the "latest" point in time that that decision can |
| 110 | be made. That means, they get invoked during `ExtensionB`'s inject phase, and _not_ during `ExtensionA`'s provide phase. |
| 111 | There is no guarantee that `ExtensionA` is already initialized by that time, because the extension loader does not know |
| 112 | whether it needs to invoke `getSomeService` at all, until the very last moment, i.e. when resolving `ExtensionB`'s |
| 113 | `service` field. By that time, the dependency graph is already built. |
| 114 | |
| 115 | Consequently, default provider methods could (and likely would) get invoked before the defining extension's `provide` |
| 116 | phase has completed. They even could get invoked before the `initialize` phase has completed: consider the following |
| 117 | situation the previous example: |
| 118 | |
| 119 | 1. all implementors of `ServiceExtension` get constructed by the Java `ServiceLoader` |
| 120 | 2. `ExtensionB` gets loaded, runs through its inject phase |
| 121 | 3. no provider for `SomeService`, thus the default provider kicks in |
| 122 | 4. `ExtensionA.getSomeService()` is invoked, but `ExtensionA` is not yet loaded -> `store` is null |
| 123 | 5. -> potential NPE |
| 124 | |
| 125 | Because there is no explicit ordering in how the `@Inject` fields are resolved, the order may depend on several factors, |
| 126 | like the Java version or specific JVM used, the classloader and/or implementation of reflection used, etc. |
| 127 | |
| 128 | ## Usage guidelines when using default providers |
| 129 | |
| 130 | From the previous sections and the examples demonstrated above we can derive a few important guidelines: |
| 131 | |
| 132 | - do not use them to achieve extensibility. That is what extensions are for. |
| 133 | - use them only to provide a _fallback implementation_ |
| 134 | - they should not depend on other injected fields (as those may still be null) |
| 135 | - they should be in their own dedicated extension (cf. `DefaultServicesExtension`) and Java module |
| 136 | - do not provide and inject the same service in one extension |
| 137 | - rule of thumb: unless you know exactly what you're doing and why you need them - don't use them! |