From 509a03f7d8ddbe943e5f531046e6694517d7e19e Mon Sep 17 00:00:00 2001 From: Bernhard Streit Date: Mon, 23 Feb 2015 10:26:07 +0100 Subject: [PATCH] Lazy register of SpringConfiguration In case you use the AnnotatedSpringConfiguration and construct beans that need the Dropwizard Configuration or Environment. Those beans cannot be instantiated when you create the context, as the registration of the dropwizard configuration and environment as singletons into the context happens after the beans would be constructed. There is now an additional constructor in the SpringBundle which allows you to hand over the annotated Spring configuration class, which then gets registered with the context after those singletons got registered. --- .../dropwizard/spring/SpringBundle.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java b/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java index eddb0d2..e4591e1 100644 --- a/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java +++ b/src/main/java/com/github/nhuray/dropwizard/spring/SpringBundle.java @@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import javax.ws.rs.Path; import javax.ws.rs.ext.Provider; @@ -37,6 +38,7 @@ public class SpringBundle implements ConfiguredBundle springConfigurationClass = null; private boolean registerConfiguration; private boolean registerEnvironment; @@ -69,6 +71,36 @@ public SpringBundle(ConfigurableApplicationContext context, boolean registerConf if (registerPlaceholder) this.placeholderConfigurer = new ConfigurationPlaceholderConfigurer(); } + /** + * Creates a new SpringBundle to automatically initialize Dropwizard {@link Environment} + *

+ * + * @param context the application context to load + * @param springConfigurationClass an annotated spring configuration class to register after configuration, environment + * and placeholderConfigurer got injected into the context. + * @param registerConfiguration register Dropwizard configuration as a Spring Bean. + * @param registerEnvironment register Dropwizard environment as a Spring Bean. + * @param registerPlaceholder resolve Dropwizard configuration as properties. + */ + public SpringBundle(final AnnotationConfigApplicationContext context, final Class springConfigurationClass, + final boolean registerConfiguration, final boolean registerEnvironment, final boolean registerPlaceholder) + { + Preconditions.checkNotNull(springConfigurationClass, "spring configuration class is required"); + + if (registerConfiguration || registerEnvironment || registerPlaceholder) { + Preconditions.checkArgument(!context.isActive(), + "Context must be not active in order to register configuration, environment or placeholder"); + } + this.context = context; + this.registerConfiguration = registerConfiguration; + this.registerEnvironment = registerEnvironment; + this.springConfigurationClass = springConfigurationClass; + if (registerPlaceholder) { + this.placeholderConfigurer = new ConfigurationPlaceholderConfigurer(); + } + } + + /** * Creates a new SpringBundle to automatically initialize Dropwizard {@link Environment} *

@@ -100,6 +132,12 @@ public void run(T configuration, Environment environment) throws Exception { // Register a placeholder to resolve Dropwizard Configuration as properties. if (placeholderConfigurer != null) registerPlaceholder(environment, configuration, context); + // After configuration, environment and placeholder got configured, + // register the spring configuration that instantiate beans that need them + if (springConfigurationClass != null && context instanceof AnnotationConfigApplicationContext) { + ((AnnotationConfigApplicationContext) context).register(springConfigurationClass); + } + // Refresh context if is not active if (!context.isActive()) context.refresh();