...
@Inject
- marks a constructor, field, or method for injection. Classes that use this annotation can be obtained from an Injector which handles binding runtime instances to each of the injection points. Marking a no-arg method with@Inject
is effectively an initialization method as these are invoked last. Injection starts with the constructor, then fields, then methods (methods with arguments executed first before no-arg methods). Inject methods must not be abstract.Code Block language java class InjectExample { DependencyA dependencyA; @Inject DependencyB dependencyB; DependencyC dependencyC; @Inject InjectExample(DependencyA a) { dependencyA = a; } @Inject void setC(DependencyC c) { dependencyC = c; } @Inject void postConstruct() { StatusLogger.getLogger().debug("Finished injecting everything"); } }
@QualifierType
- marks an annotation as a qualifier annotation. Qualifiers are used for providing multiple variants of the same type which can be identified by the qualifier annotation. Qualifiers can be specified on a class, an inject method parameter, a factory method, and an inject field.@Named
- qualifier for a named instance. This annotation can specify alias names, too (similar to the existing@PluginAliases
annotation).Code Block class QualifierExample { @Named String foo; // uses field name by default for value in @Named @Named({"primary", "legacy-name"}) String bar; // inline use of name plus aliases }
@ScopeType
- marks an annotation as a scope annotation. Scopes provide a strategy for determining the lifecycle of instances in its scope. Without a scope, instances are created fresh each time they're injected or obtained fromInjector
.@Singleton
- scope type that maintains at most one instance for each qualified type. That is, every injected singleton instance and every singleton instance obtained fromInjector
will be the same instance.@FactoryType
- marks an annotation as a factory type. Factory annotations are used for marking methods as factories for the returned instance of that method. This is intended to support existing plugin factory related annotations like@PluginFactory
and@PluginBuilderFactory
.@Factory
- marks a method as a factory for instances it returns. Factory methods can be static or not, though instance factory methods may only be installed by installing an instance of the class with said factory method.Code Block class FactoryExample { @Inject Dependency dep; @Factory ProducedInstance newInstance(@Named String name) { return new ProducedInstance(name); } }
Then, the load and install using an anonymous class for an installation module.
Code Block Injector injector = DI.createInjector(new Object() { @Factory @Named String getName() { return "example"; } }); injector.installModule(injector.getInstance(, FactoryExample.class)); var producedInstance = injector.getInstance(ProducedInstance.class);
...