Created
July 16, 2013 16:59
-
-
Save efenderbosch/6010537 to your computer and use it in GitHub Desktop.
Initializes a Dropwizard Service health checks, resources, tasks, etc. using Spring @component annotations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.segmint.postdelivery.spring; | |
import java.util.Map; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.ext.Provider; | |
import org.springframework.web.context.ContextLoaderListener; | |
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; | |
import com.yammer.dropwizard.config.Configuration; | |
import com.yammer.dropwizard.config.Environment; | |
import com.yammer.dropwizard.tasks.Task; | |
import com.yammer.metrics.core.HealthCheck; | |
public class SpringServiceInitializer { | |
/** | |
* Annotate your health checks, resources, tasks, etc. w/ | |
* org.springframework.stereotype.Component to auto-inject them in to | |
* Spring, which will then add them to DropWizard env. | |
* | |
* Preferred over Environment.scanPackagesForResourcesAndProviders since it | |
* makes prototypes, not singletons. | |
* | |
* @param configuration | |
* @param environment | |
* @param basePackages | |
*/ | |
public static void initialize(Configuration configuration, Environment environment, String... basePackages) { | |
AnnotationConfigWebApplicationContext parent = new AnnotationConfigWebApplicationContext(); | |
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); | |
parent.refresh(); | |
parent.getBeanFactory().registerSingleton("configuration", configuration); | |
parent.registerShutdownHook(); | |
parent.start(); | |
// the real main app context has a link to the parent context | |
ctx.setParent(parent); | |
// let annotation scanning do its magic | |
ctx.scan(basePackages); | |
ctx.refresh(); | |
ctx.registerShutdownHook(); | |
ctx.start(); | |
// now that Spring is started, let's get all the beans that matter into | |
// DropWizard | |
Map<String, HealthCheck> healthChecks = ctx.getBeansOfType(HealthCheck.class); | |
for (Map.Entry<String, HealthCheck> entry : healthChecks.entrySet()) { | |
environment.addHealthCheck(entry.getValue()); | |
} | |
Map<String, Object> resources = ctx.getBeansWithAnnotation(Path.class); | |
for (Map.Entry<String, Object> entry : resources.entrySet()) { | |
environment.addResource(entry.getValue()); | |
} | |
Map<String, Task> tasks = ctx.getBeansOfType(Task.class); | |
for (Map.Entry<String, Task> entry : tasks.entrySet()) { | |
environment.addTask(entry.getValue()); | |
} | |
Map<String, Object> providers = ctx.getBeansWithAnnotation(Provider.class); | |
for (Map.Entry<String, Object> entry : providers.entrySet()) { | |
environment.addProvider(entry.getValue()); | |
} | |
// link Spring to the embedded Jetty in Dropwizard | |
environment.addServletListeners(new ContextLoaderListener(ctx)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment