Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy register of SpringConfiguration #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,7 @@ public class SpringBundle<T extends Configuration> implements ConfiguredBundle<T

private ConfigurableApplicationContext context;
private ConfigurationPlaceholderConfigurer placeholderConfigurer;
private Class<?> springConfigurationClass = null;
private boolean registerConfiguration;
private boolean registerEnvironment;

Expand Down Expand Up @@ -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}
* <p/>
*
* @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}
* <p/>
Expand Down Expand Up @@ -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();

Expand Down