Example usage for org.springframework.web.context.support AnnotationConfigWebApplicationContext getBeansWithAnnotation

List of usage examples for org.springframework.web.context.support AnnotationConfigWebApplicationContext getBeansWithAnnotation

Introduction

In this page you can find the example usage for org.springframework.web.context.support AnnotationConfigWebApplicationContext getBeansWithAnnotation.

Prototype

@Override
    public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType)
            throws BeansException 

Source Link

Usage

From source file:sindica.to.dropwizard.spring.BaseSpringApplication.java

@Override
public final void run(T configuration, Environment environment) throws ClassNotFoundException {
    AnnotationConfigWebApplicationContext parent = new AnnotationConfigWebApplicationContext();
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();

    parent.refresh();/*from ww w. ja  va2s  .  c  o  m*/
    parent.getBeanFactory().registerSingleton("configuration", configuration);
    parent.registerShutdownHook();
    parent.start();

    //the real main app context has a link to the parent context
    ctx.setParent(parent);

    onConfigureSpringContext(ctx);

    ctx.refresh();
    ctx.registerShutdownHook();
    ctx.start();

    //HealthChecks
    Map<String, HealthCheck> healthChecks = ctx.getBeansOfType(HealthCheck.class);
    for (Map.Entry<String, HealthCheck> entry : healthChecks.entrySet()) {
        LOG.info("Registering HealthCheck: ${entry.key}");
        environment.healthChecks().register(entry.getKey(), entry.getValue());
    }

    //resources
    Map<String, Object> resources = ctx.getBeansWithAnnotation(Path.class);
    for (Map.Entry<String, Object> entry : resources.entrySet()) {
        LOG.info("Registering Resource: ${entry.key}");
        environment.jersey().register(entry.getValue());
    }

    //tasks
    Map<String, Task> tasks = ctx.getBeansOfType(Task.class);
    for (Map.Entry<String, Task> entry : tasks.entrySet()) {
        LOG.info("Registering Task: ${entry.key}");
        environment.admin().addTask(entry.getValue());
    }

    //Managed
    Map<String, Managed> managers = ctx.getBeansOfType(Managed.class);
    for (Map.Entry<String, Managed> entry : managers.entrySet()) {
        LOG.info("Registering Task: ${entry.key}");
        environment.lifecycle().manage(entry.getValue());
    }

    //JAX-RS providers
    Map<String, Object> providers = ctx.getBeansWithAnnotation(Provider.class);
    for (Map.Entry<String, Object> entry : providers.entrySet()) {
        LOG.info("Registering Provider: ${entry.key}");
        //environment.jersey().addProvider(entry.getValue())
    }

    //last, but not least, let's link Spring to the embedded Jetty in Dropwizard
    EventListener servletListener = new SpringContextLoaderListener(ctx);
    environment.servlets().addServletListeners(servletListener);

    onRun(configuration, environment, ctx);
}