From 83f6f5730f16552cb662d0401bb5fffc525ef6ee Mon Sep 17 00:00:00 2001 From: Alex Shpak Date: Mon, 12 Feb 2018 09:33:05 +0100 Subject: [PATCH] Bind application config with all contracts --- gradle.properties | 2 +- .../dropwizard/hk2bundle/HK2ConfiguredBundle.java | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/gradle.properties b/gradle.properties index c714165..35d7a61 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ -hk2bundleVersion=0.6.3-SNAPSHOT +hk2bundleVersion=0.6.4-SNAPSHOT dropwizardVersion=1.2.2 junitVersion=4.12 \ No newline at end of file diff --git a/src/main/java/net/winterly/dropwizard/hk2bundle/HK2ConfiguredBundle.java b/src/main/java/net/winterly/dropwizard/hk2bundle/HK2ConfiguredBundle.java index 999fd4a..883ed74 100644 --- a/src/main/java/net/winterly/dropwizard/hk2bundle/HK2ConfiguredBundle.java +++ b/src/main/java/net/winterly/dropwizard/hk2bundle/HK2ConfiguredBundle.java @@ -5,10 +5,14 @@ import io.dropwizard.setup.Bootstrap; import io.dropwizard.setup.Environment; import org.glassfish.hk2.api.ServiceLocator; +import org.jvnet.hk2.annotations.Contract; import javax.inject.Inject; +import java.lang.reflect.Type; +import java.util.Set; import static org.glassfish.hk2.utilities.ServiceLocatorUtilities.addOneConstant; +import static org.glassfish.hk2.utilities.reflection.ReflectionHelper.getAdvertisedTypesFromObject; /** * Configured bundle used to obtain and bind configuration instance into DI container. @@ -25,7 +29,10 @@ public void initialize(Bootstrap bootstrap) { @Override public void run(Configuration configuration, Environment environment) { - addOneConstant(serviceLocator, configuration, null, - configuration.getClass(), Configuration.class); + Set contracts = getAdvertisedTypesFromObject(configuration, Contract.class); + contracts.add(configuration.getClass()); + contracts.add(Configuration.class); + + addOneConstant(serviceLocator, configuration, null, contracts.toArray(new Type[0])); } }