T - configuration typepublic static class GuiceBundle.Builder<T extends io.dropwizard.Configuration>
extends java.lang.Object
| Constructor and Description |
|---|
Builder() |
| Modifier and Type | Method and Description |
|---|---|
GuiceBundle<T> |
build() |
GuiceBundle<T> |
build(com.google.inject.Stage stage) |
GuiceBundle.Builder<T> |
bundleLookup(GuiceyBundleLookup bundleLookup)
Configure custom
GuiceyBundleLookup. |
GuiceBundle.Builder<T> |
bundles(GuiceyBundle... bundles)
Guicey bundles are mainly useful for extensions (to group installers and extensions installation without
auto scan).
|
GuiceBundle.Builder<T> |
disable(java.util.function.Predicate<ItemInfo>... predicates)
Disable items using disable predicate: all matched items will be disabled.
|
GuiceBundle.Builder<T> |
disableBundleLookup()
Disables default bundle lookup.
|
GuiceBundle.Builder<T> |
disableBundles(java.lang.Class<? extends GuiceyBundle>... bundles)
Bundles disable is mostly useful for testing.
|
GuiceBundle.Builder<T> |
disableDropwizardBundles(java.lang.Class<? extends io.dropwizard.ConfiguredBundle>... bundles)
Dropwizard bundles disable is mostly useful for testing.
|
GuiceBundle.Builder<T> |
disableExtensions(java.lang.Class<?>... extensions)
Extensions disable is mostly useful for testing.
|
GuiceBundle.Builder<T> |
disableInstallers(java.lang.Class<? extends FeatureInstaller>... installers)
Disabling installer will lead to avoiding all relative installed extensions.
|
GuiceBundle.Builder<T> |
disableModules(java.lang.Class<? extends com.google.inject.Module>... modules)
Modules disable is mostly useful for testing.
|
GuiceBundle.Builder<T> |
dropwizardBundles(io.dropwizard.ConfiguredBundle... bundles)
Dropwizard bundles registration.
|
GuiceBundle.Builder<T> |
duplicateConfigDetector(DuplicateConfigDetector detector)
Duplicate configuration detector decides what configuration items, registered as instance (guicey bundle,
guice module) to consider duplicate (and so avoid duplicates installation).
|
GuiceBundle.Builder<T> |
enableAutoConfig(java.lang.String... basePackages)
Enables auto scan feature.
|
GuiceBundle.Builder<T> |
extensions(java.lang.Class<?>... extensionClasses)
Beans could be registered automatically when auto scan enabled,
but if you don't want to use it, you can register beans manually.
|
GuiceBundle.Builder<T> |
hookAlias(java.lang.String name,
java.lang.Class<? extends GuiceyConfigurationHook> hook)
Guicey hooks (
GuiceyConfigurationHook) may be loaded with system property "guicey.hooks". |
GuiceBundle.Builder<T> |
injectorFactory(InjectorFactory injectorFactory)
Configures custom
InjectorFactory. |
GuiceBundle.Builder<T> |
installers(java.lang.Class<? extends FeatureInstaller>... installers)
Feature installers registered automatically when auto scan enabled,
but if you don't want to use it, you can register installers manually (note: without auto scan default
installers will not be registered).
|
GuiceBundle.Builder<T> |
listen(GuiceyLifecycleListener... listeners)
Guicey broadcast a lot of events in order to indicate lifecycle phases
(
GuiceyLifecycle). |
GuiceBundle.Builder<T> |
modules(com.google.inject.Module... modules)
Multiple module instances of the same type could be registered.
|
GuiceBundle.Builder<T> |
modulesOverride(com.google.inject.Module... modules)
Register overriding modules, used to override bindings of normal modules.
|
GuiceBundle.Builder<T> |
noDefaultInstallers()
Disables automatic
CoreInstallersBundle and WebInstallersBundle registration
(no installers will be registered by default). |
GuiceBundle.Builder<T> |
noGuiceFilter()
Disables
GuiceFilter registration for both application and admin contexts. |
<K extends java.lang.Enum & Option> |
option(K option,
java.lang.Object value)
Options is a generic mechanism to provide internal configuration values for guicey and 3rd party bundles.
|
<K extends java.lang.Enum & Option> |
options(java.util.Map<java.lang.Enum,java.lang.Object> options)
Sets multiple options at once.
|
GuiceBundle.Builder<T> |
printAllGuiceBindings()
Prints all bindings in guice injector.
|
GuiceBundle.Builder<T> |
printAvailableInstallers()
Prints all registered (not disabled) installers with registration source.
|
GuiceBundle.Builder<T> |
printConfigurationBindings()
Prints available configuration bindings.
|
GuiceBundle.Builder<T> |
printCustomConfigurationBindings()
The same as
printConfigurationBindings(), but hides all dropwizard related paths. |
GuiceBundle.Builder<T> |
printDiagnosticInfo()
Print additional diagnostic logs with startup statistics, installed bundles, installers and resolved
extensions and configuration tree.
|
GuiceBundle.Builder<T> |
printGuiceAopMap()
Prints all configured guice AOP interceptors and how they apply to methods.
|
GuiceBundle.Builder<T> |
printGuiceAopMap(GuiceAopConfig config)
Supposed to be used to understand how guice AOP works (the most often reasons: check aop applied to method
and check interceptors order).
|
GuiceBundle.Builder<T> |
printGuiceBindings()
Prints guice bindings configured in user-provided modules (excluding guicey and guice own bindings).
|
GuiceBundle.Builder<T> |
printLifecyclePhases()
Split logs with major lifecycle stage names.
|
GuiceBundle.Builder<T> |
printLifecyclePhasesDetailed()
Same as
printLifecyclePhases(), but also prints resolved and disabled configuration items. |
GuiceBundle.Builder<T> |
searchCommands()
NOTE: will not scan if auto scan not enabled (packages not configured
with
enableAutoConfig(String...)). |
GuiceBundle.Builder<T> |
strictScopeControl()
Enables strict control of beans instantiation context: all beans must be instantiated by guice, except
beans annotated with
JerseyManaged. |
GuiceBundle.Builder<T> |
useHK2ForJerseyExtensions()
Manage jersey extensions (resources, jersey filters etc.) with HK2 by default instead of guice (the
same effect as if
JerseyManaged annotation
would be set on all beans). |
public GuiceBundle.Builder<T> listen(GuiceyLifecycleListener... listeners)
GuiceyLifecycle). This could be useful
for diagnostic logging (like printLifecyclePhases()) or to implement special
behaviours on installers, bundles, modules extensions (listeners have access to everything).
For example, ConfigurationAwareModule like support for guice modules could be implemented
with listeners.
Configuration items (modules, extensions, bundles) are not aware of each other and listeners could be used to tie them. For example, to tell bundle if some other bundles registered (limited applicability, but just for example).
Listener can't directly affect configuration (can't register or disable extensions, modules etc).
But listener could implement GuiceyConfigurationHook
interface and it would be automatically registered. This could be used to register special
extensions required by listener logic (some diagnostic, monitoring or special features support).
You can also use GuiceyLifecycleAdapter when you need to
handle multiple events (it replaces direct events handling with simple methods).
Listener is not registered if equal listener were already registered (Set used as
listeners storage), so if you need to be sure that only one instance of some listener will be used
implement Object.equals(Object) and Object.hashCode().
listeners - guicey lifecycle listeners (listener could be also a hook)GuiceyLifecycle,
GuiceyConfigurationHook,
GuiceyLifecycleAdapterpublic <K extends java.lang.Enum & Option> GuiceBundle.Builder<T> option(K option, java.lang.Object value)
GuiceyOptions as options example. Bundles may define their own enums in the same way to
use options mechanism.
Options intended to be used for development time specific configurations (most likely low level options to slightly change behaviour). Also, in contrast to internal booleans (e.g. in main bundle), options are accessible everywhere and may be used by other 3rd party module or simply for reporting.
Options may be set only on application level. Guicey bundles could access option values through
bootstrap object. Guice service could access options through
options bean. Installers
could use WithOptions to get access to options.
Options metadata for reporting is available through
guice bean.
Options may be used instead of shortcut methods when configuration value is dynamic (without options it would not be impossible to configure without breaking builder flow and additional if statements).
Note: each option declares exact option type and provided value is checked for type compatibility.
K - helper type for option signature definitionoption - option enumvalue - option value (not null)java.lang.NullPointerException - is null value providedjava.lang.IllegalArgumentException - if provided value incompatible with option typefor more details,
GuiceyOptions,
InstallersOptionspublic <K extends java.lang.Enum & Option> GuiceBundle.Builder<T> options(java.util.Map<java.lang.Enum,java.lang.Object> options)
Note: Option type is not mixed with enum in declaration to simplify usage,
but used enums must be correct options.
Use provided OptionsMapper
utility to map options from system properties or environment variables. It supports basic string conversions.
For example:
.options(new OptionsMapper()
.env("STAGE", GuiceyOptions.InjectorStage)
.map())
Will set injector stage option from STAGE environment variable. If variable is not set - default value used.
If STAGE set to, for example "DEVELOPMENT" (must be Stage enum value) then Stage.DEVELOPMENT will be
set as option value.
Also, mapper could map generic options definitions from system properties (prefixed):
.options(new OptionsMapper()
.props("option.")
.map())
See OptionsMapper for more usage details.K - helper type for option signature definitionoptions - options map (not null)java.lang.NullPointerException - is null value provided for any optionjava.lang.IllegalArgumentException - if any provided value incompatible with option typefor more info,
OptionsMapperpublic GuiceBundle.Builder<T> injectorFactory(InjectorFactory injectorFactory)
InjectorFactory. Required by some guice extensions like governator.injectorFactory - custom guice injector factorypublic GuiceBundle.Builder<T> bundleLookup(GuiceyBundleLookup bundleLookup)
GuiceyBundleLookup. Lookup provides an easy way to indirectly install
GuiceyBundle bundles. Default implementation support lookup by system property.bundleLookup - custom bundle lookup implementationDefaultBundleLookup,
duplicateConfigDetector(DuplicateConfigDetector)public GuiceBundle.Builder<T> disableBundleLookup()
public GuiceBundle.Builder<T> enableAutoConfig(java.lang.String... basePackages)
basePackages - packages to scan extensions inGuiceyOptions.ScanPackagespublic GuiceBundle.Builder<T> duplicateConfigDetector(DuplicateConfigDetector detector)
Object.equals(Object)) instances are considered duplicate. If you need to
accept only one instance of bundle or module, simply implement equals method to make all instances equal.
Custom deduplicatation implementation may be required for 3rd party instances, where proper equals
implementation is impossible (or for more complicated duplicates detection logic).
Example situation: suppose one common bundle (or guice module) is used by two other bundles, so it would be registered in two bundles, but, if these bundles would be used together, then two instances of common bundle would be registered, which is often not desired. To workaround such case, bundle must implement proper equals method or custom duplication detector implementation must be used.
Use LegacyModeDuplicatesDetector to simulate
legacy guicey behaviour when only one instance of type is allowed (if old behaviour is important).
detector - detector implementationpublic GuiceBundle.Builder<T> modules(com.google.inject.Module... modules)
UniqueModule with correct
equals implementation or implement custom deduplication logic in
duplicateConfigDetector(DuplicateConfigDetector).
These modules are registered under initialization phase where you don't have access for configuration
or environment objects. To workaround this you can use *AwareModule interfaces, or extend from
DropwizardAwareModule and required objects will be set
just before configuration start. Another option is to register module inside
GuiceyBundle.run(GuiceyEnvironment), which is called under run phase.
modules - one or more guice modulesBootstrapAwareModule,
ConfigurationAwareModule,
EnvironmentAwareModule,
OptionsAwareModule,
DropwizardAwareModulepublic GuiceBundle.Builder<T> modulesOverride(com.google.inject.Module... modules)
Modules.override(Module...) is used. If binding is present in normal module
and overriding module then overriding module binding is used. Bindings in overriding modules, not present
in normal modules are simply added to context (as usual module).
The main purpose for overriding modules is testing, but it also could be used (in rare cases) to amend existing guice context (for example, in case when you do not control all bindings, but need to hot fix something).
Overriding modules behave the same as normal modules: they are inspected for *AwareModule interfaces
to inject dropwizard objects. Overriding module could be disabled with disableModules(Class[]) or
generic disable(Predicate[]).
modules - overriding modulesmodules(Module...),
to override overridden
bindings in test (edge case(public GuiceBundle.Builder<T> searchCommands()
enableAutoConfig(String...)).
Enables commands classpath search. All found commands are instantiated and registered in
bootstrap. Default constructor is used for simple commands, but EnvironmentCommand
must have constructor with Application argument.
By default, commands search is disabled.
CommandSupport,
GuiceyOptions.SearchCommandspublic GuiceBundle.Builder<T> noDefaultInstallers()
CoreInstallersBundle and WebInstallersBundle registration
(no installers will be registered by default).GuiceyOptions.UseCoreInstallerspublic GuiceBundle.Builder<T> noGuiceFilter()
GuiceFilter registration for both application and admin contexts.
Without guice filter registered, guice ServletModule registrations
are useless (because they can't be used). So guice servlet modules support will be disabled:
no request or session scopes may be used and registrations of servlet modules will be denied
(binding already declared exception). Even with disabled guice filter, request and response
objects provider injections still may be used in resources (will work through HK2 provider).
Guice servlets initialization took ~50ms, so injector creation will be a bit faster after disabling.
GuiceyOptions.GuiceFilterRegistration@SafeVarargs public final GuiceBundle.Builder<T> installers(java.lang.Class<? extends FeatureInstaller>... installers)
Also, could be used to add installers from packages not included in auto scanning.
installers - feature installer classes to registerpublic GuiceBundle.Builder<T> extensions(java.lang.Class<?>... extensionClasses)
Guice injector will instantiate beans and registered installers will be used to recognize and properly register provided extension beans.
Also, could be used to add beans from packages not included in auto scanning.
NOTE: startup will fail if bean not recognized by installers.
NOTE: Don't register commands here: either enable auto scan, which will install commands automatically or register command directly to bootstrap object and dependencies will be injected to it after injector creation.
extensionClasses - extension bean classes to registerpublic GuiceBundle.Builder<T> bundles(GuiceyBundle... bundles)
Multiple bundle instances of the same type could be registered. If bundle uniqueness is important
use UniqueGuiceyBundle with correct
equals implementation or implement custom deduplication logic in
duplicateConfigDetector(DuplicateConfigDetector).
bundles - guicey bundlespublic GuiceBundle.Builder<T> dropwizardBundles(io.dropwizard.ConfiguredBundle... bundles)
Bootstrap.addBundle(ConfiguredBundle), which is actually called almost immediately),
except guicey tracking (registered bundles are showed in diagnostic report), ability to disable bundle and
automatic duplicates detection (see duplicateConfigDetector(DuplicateConfigDetector)).
Only bundles registered with guicey api are checked! For example, if you register one instance of the same bundle directly into bootstrap and another one with guicey api - guicey will not be able to track duplicate registration. So it's better to register all bundles through guicey api.
By default, guicey will also track transitive bundles registrations (when registered bundle register
other bundles) so disable and deduplication logic will apply to them too. Tracking could be disabled
with GuiceyOptions.TrackDropwizardBundles option.
bundles - guicey bundles@SafeVarargs public final GuiceBundle.Builder<T> disableInstallers(java.lang.Class<? extends FeatureInstaller>... installers)
Disabling installer may be used to replace some existing installer with modified version (probably fixed): disable existing installer and register new custom installer.
installers - feature installer types to disablepublic final GuiceBundle.Builder<T> disableExtensions(java.lang.Class<?>... extensions)
extensions - extensions to disable (manually added, registered by bundles or with classpath scan)@SafeVarargs public final GuiceBundle.Builder<T> disableModules(java.lang.Class<? extends com.google.inject.Module>... modules)
Option could also disable inner modules (registered by modules transitively), but only if bindings analysis
is not disabled (by GuiceyOptions.AnalyzeGuiceModules). Inner modules can't be removed from
overriding modules, because only normal modules are analyzed.
When bindings analysis is disabled this option can disable only directly registered modules
(with modules(Module...) or in bundle
GuiceyBootstrap.modules(Module...).
modules - guice module types to disable@SafeVarargs public final GuiceBundle.Builder<T> disableBundles(java.lang.Class<? extends GuiceyBundle>... bundles)
GuiceyBootstrap.bundles(GuiceyBundle...)).bundles - guicey bundles to disable@SafeVarargs public final GuiceBundle.Builder<T> disableDropwizardBundles(java.lang.Class<? extends io.dropwizard.ConfiguredBundle>... bundles)
GuiceyOptions.TrackDropwizardBundles).bundles - guicey bundles to disable@SafeVarargs public final GuiceBundle.Builder<T> disable(java.util.function.Predicate<ItemInfo>... predicates)
Also, predicate is called on registration for all already registered items to make predicate registration moment not important.
Essentially, predicates are the same as calling direct disable methods: items, disabled by predicate, will be marked as disabled by predicate registration context (application or guicey bundle).
Mostly useful for testing, but in some cases could be used directly.
Use Predicate.and(Predicate), Predicate.or(Predicate) and Predicate.negate()
to combine complex predicates from simple ones from
Disables helper.
Item passed only one time after initial registration and so item object will have only basic fields: item type, item class and item registration scope (who register it).
For example, disable all installers, registered from application root except SomeInstallerType:
import static ru.vyarus.dropwizard.guice.module.context.Disables.*
builder.disable(installer()
.and(registeredBy(Application.class))
.and(type(SomeInstallerType.class).negate());
Items could be disabled just by class, no matter what type they are:
import static ru.vyarus.dropwizard.guice.module.context.Disables.*
builder.disable(type(MyExtension.class,
MyInstaller.class,
MyBundle.class,
MyModule.class));
For instance types (bundles, modules), when multiple instances of the same class may appear, exact instance could be disabled (predicate would be called for each new instance separately, avoiding equal duplicates).
predicates - disable predicatesfor common predicatespublic GuiceBundle.Builder<T> strictScopeControl()
JerseyManaged.
When bean instantiated in wrong context exception would be thrown.
It is useful if you write your own installers or to simply ensure correctness in doubtful cases.
Do not use for production! It is intended to be used mostly in tests or to diagnose problems during development.
To implicitly enable this check in all tests use
PropertyBundleLookup.enableBundles(HK2DebugBundle.class).
HK2DebugBundlepublic GuiceBundle.Builder<T> useHK2ForJerseyExtensions()
JerseyManaged annotation
would be set on all beans). Use this option if you want to use jersey specific resource features
(like @Context bindings) and completely delegate management to HK2.
IMPORTANT: this will activate HK2 bridge usage (to be able to inject guice beans) and so you will need to provide bridge dependency (org.glassfish.hk2:guice-bridge:2.5.0). Startup will fail if dependency is not available.
WARNING: you will not be able to use guice AOP on beans managed by HK2!
InstallersOptions.JerseyExtensionsManagedByGuice,
JerseyManaged,
GuiceManagedpublic GuiceBundle.Builder<T> printDiagnosticInfo()
Statistics shows mainly where guice spent most time. Configuration info is useful for configuration problems resolution. Also, logs useful for better understanding how guicey works.
If custom logging format is required use ConfigurationDiagnostic directly.
May be enabled on compiled application with a system property: -Dguicey.hooks=diagnostic
(hook will also enable some other reports).
ConfigurationDiagnosticpublic GuiceBundle.Builder<T> printAvailableInstallers()
In contrast to printDiagnosticInfo() shows all installers (including installers not used by
application extensions). Installers report intended only to show available installers and will not
show duplicate installers registrations or installers disabling (use diagnostic reporting for
all configuration aspects). Also, report will indicate installers marker interfaces and so it will
be obvious what installer did: install by type or by object, perform custom guice bindings or perform
jersey specific installations.
ConfigurationDiagnosticpublic GuiceBundle.Builder<T> printConfigurationBindings()
printCustomConfigurationBindings() to see only
custom paths (without dropwizard configurations).
Safe to use with other print* options.
Info: guicey analyze configuration object (prepared by dropwizard) with jackson serialization api
and binds all readable configuration values by path (e.g. @inject @Config("some.path") String prop;.
Also, unique sub configuration objects are recognized and may be used directly
(@Inject @Config SubConfig subConf). Introspected configuration object is accessible from
lifecycle events, gucie modules, guicey bundles and by direct injection.
May be enabled on compiled application with a system property: -Dguicey.hooks=diagnostic
(hook will also enable some other reports).
ConfigurationTree,
Configpublic GuiceBundle.Builder<T> printCustomConfigurationBindings()
printConfigurationBindings(), but hides all dropwizard related paths.
Use to see bindings of your custom configuration classes.public GuiceBundle.Builder<T> printGuiceBindings()
May be enabled on compiled application with a system property: -Dguicey.hooks=diagnostic
(hook will also enable some other reports).
to show entire injector statepublic GuiceBundle.Builder<T> printAllGuiceBindings()
to show only user-provided bindingspublic GuiceBundle.Builder<T> printGuiceAopMap()
printGuiceAopMap(GuiceAopConfig) and filter all non interesting bindings.public GuiceBundle.Builder<T> printGuiceAopMap(GuiceAopConfig config)
Multiple reports could be configured (in some cases it is simpler to declare few different but simple configurations rather then one complex).
config - report configurationpublic GuiceBundle.Builder<T> printLifecyclePhases()
LifecycleDiagnosticpublic GuiceBundle.Builder<T> printLifecyclePhasesDetailed()
printLifecyclePhases(), but also prints resolved and disabled configuration items.
May be enabled on compiled application with a system property: -Dguicey.hooks=diagnostic
(hook will also enable some other reports).
LifecycleDiagnosticpublic GuiceBundle.Builder<T> hookAlias(java.lang.String name, java.lang.Class<? extends GuiceyConfigurationHook> hook)
GuiceyConfigurationHook) may be loaded with system property "guicey.hooks". But
it may be not comfortable to always declare full class name (e.g. -Dguicey.hooks=com.foo.bar.Hook,..).
Instead short alias name may be used: -Dguicey.hooks=alias1, alias2.
By default, diagnostic hook is aliased as "diagnostic" in order to be able to enable diagnostic reporting
for compiled application (-Dguicey.hooks=diagnostic).
name - alias namehook - hook class to aliaspublic GuiceBundle<T> build(com.google.inject.Stage stage)
stage - stage to run injector withGuiceyOptions.InjectorStagepublic GuiceBundle<T> build()
GuiceyOptions.InjectorStage