com.google.inject.servlet.RequestScoped源码实例Demo

类com.google.inject.servlet.RequestScoped源码实例Demo

下面列出了com.google.inject.servlet.RequestScoped 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dropwizard-guicey   文件: GuiceScopingVisitor.java
@Override
public Class<? extends Annotation> visitScope(final Scope scope) {
    Class<? extends Annotation> res = null;
    if (scope == Scopes.SINGLETON) {
        res = javax.inject.Singleton.class;
    }
    if (scope == Scopes.NO_SCOPE) {
        res = Prototype.class;
    }
    if (scope == ServletScopes.REQUEST) {
        res = RequestScoped.class;
    }
    if (scope == ServletScopes.SESSION) {
        res = SessionScoped.class;
    }
    // not supporting custom scopes
    return res;
}
 
源代码2 项目: hadoop   文件: WebAppTests.java
/**
 * Create a mock injector for tests
 * @param <T> type of class/interface
 * @param api the interface class of the object to inject
 * @param impl the implementation object to inject
 * @param modules additional guice modules
 * @return an injector
 */
public static <T> Injector createMockInjector(final Class<T> api,
                                              final T impl,
                                              final Module... modules) {
  return Guice.createInjector(new AbstractModule() {
    final PrintWriter writer = spy(new PrintWriter(System.out));
    final HttpServletRequest request = createRequest();
    final HttpServletResponse response = createResponse();

    @Override
    protected void configure() {
      if (api != null) {
        bind(api).toInstance(impl);
      }
      bindScope(RequestScoped.class, Scopes.SINGLETON);
      if (modules != null) {
        for (Module module : modules) {
          install(module);
        }
      }
    }

    @Provides HttpServletRequest request() {
      return request;
    }

    @Provides HttpServletResponse response() {
      return response;
    }

    @Provides PrintWriter writer() {
      return writer;
    }

    HttpServletRequest createRequest() {
      // the default suffices for now
      return mock(HttpServletRequest.class);
    }

    HttpServletResponse createResponse() {
      try {
        HttpServletResponse res = mock(HttpServletResponse.class);
        when(res.getWriter()).thenReturn(writer);
        return res;
      } catch (Exception e) {
        throw new WebAppException(e);
      }
    }
  });
}
 
源代码3 项目: big-c   文件: WebAppTests.java
/**
 * Create a mock injector for tests
 * @param <T> type of class/interface
 * @param api the interface class of the object to inject
 * @param impl the implementation object to inject
 * @param modules additional guice modules
 * @return an injector
 */
public static <T> Injector createMockInjector(final Class<T> api,
                                              final T impl,
                                              final Module... modules) {
  return Guice.createInjector(new AbstractModule() {
    final PrintWriter writer = spy(new PrintWriter(System.out));
    final HttpServletRequest request = createRequest();
    final HttpServletResponse response = createResponse();

    @Override
    protected void configure() {
      if (api != null) {
        bind(api).toInstance(impl);
      }
      bindScope(RequestScoped.class, Scopes.SINGLETON);
      if (modules != null) {
        for (Module module : modules) {
          install(module);
        }
      }
    }

    @Provides HttpServletRequest request() {
      return request;
    }

    @Provides HttpServletResponse response() {
      return response;
    }

    @Provides PrintWriter writer() {
      return writer;
    }

    HttpServletRequest createRequest() {
      // the default suffices for now
      return mock(HttpServletRequest.class);
    }

    HttpServletResponse createResponse() {
      try {
        HttpServletResponse res = mock(HttpServletResponse.class);
        when(res.getWriter()).thenReturn(writer);
        return res;
      } catch (Exception e) {
        throw new WebAppException(e);
      }
    }
  });
}
 
源代码4 项目: dropwizard-guicier   文件: JerseyGuicierModule.java
@Provides
@RequestScoped
public ContainerRequestContext providesContainerRequestContext(ServiceLocator serviceLocator) {
  return serviceLocator.getService(ContainerRequestContext.class);
}
 
源代码5 项目: dropwizard-guicier   文件: JerseyGuicierModule.java
@Provides
@RequestScoped
public ExtendedUriInfo providesExtendedUriInfo(ServiceLocator serviceLocator) {
  return serviceLocator.getService(ExtendedUriInfo.class);
}
 
源代码6 项目: Orienteer   文件: GraphGuiceModule.java
@Provides
@RequestScoped
public OrientGraph provideOrientGraph(ODatabaseDocumentTx dbTx)
{
	return new OrientGraph(dbTx, false);
}
 
源代码7 项目: dropwizard-guicey   文件: GuiceBindingsModule.java
/**
 * Important moment: request scoped jersey objects must be bound to guice request scope (if guice web used)
 * because otherwise scope delegation to other thread will not work
 * (see {@link com.google.inject.servlet.ServletScopes#transferRequest(java.util.concurrent.Callable)}).
 * <p>
 * WARNING: bean instance must be obtained in current (request) thread in order to be us used later
 * inside transferred thread (simply call {@code provider.get()} (for jersey-managed bean like {@link UriInfo})
 * before {@code ServletScopes.transferRequest()}.
 *
 * @param type   jersey type to bind
 * @param global true for global type binding
 */
private void jerseyToGuiceBinding(final Class<?> type, final boolean global) {
    final ScopedBindingBuilder binding = bindJerseyComponent(binder(), provider, type);
    if (!global && guiceServletSupport) {
        binding.in(RequestScoped.class);
    }
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 类方法
 同包方法