コード例 #1
0
ファイル: GPIOService.java プロジェクト: jdye64/GPIO
  @Override
  public void run(GPIOConfiguration configuration, Environment environment) {
    // nothing to do yet
    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);
    ctx.register(GPIOSpringConfiguration.class);
    ctx.refresh();
    ctx.registerShutdownHook();
    ctx.start();

    // now that Spring is started, let's get all the beans that matter into DropWizard

    // health checks
    Map<String, HealthCheck> healthChecks = ctx.getBeansOfType(HealthCheck.class);
    for (Map.Entry<String, HealthCheck> entry : healthChecks.entrySet()) {
      environment.addHealthCheck(entry.getValue());
    }

    // resources
    Map<String, Object> resources = ctx.getBeansWithAnnotation(Path.class);
    for (Map.Entry<String, Object> entry : resources.entrySet()) {
      environment.addResource(entry.getValue());
    }

    // tasks
    Map<String, Task> tasks = ctx.getBeansOfType(Task.class);
    for (Map.Entry<String, Task> entry : tasks.entrySet()) {
      environment.addTask(entry.getValue());
    }

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

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

    // activate Spring Security filter
    environment.addFilter(DelegatingFilterProxy.class, "/*").setName("springSecurityFilterChain");
  }