Java源码示例:com.google.inject.servlet.RequestScoped
示例1
@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
/**
* 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
/**
* 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
@Provides
@RequestScoped
public ContainerRequestContext providesContainerRequestContext(ServiceLocator serviceLocator) {
return serviceLocator.getService(ContainerRequestContext.class);
}
示例5
@Provides
@RequestScoped
public ExtendedUriInfo providesExtendedUriInfo(ServiceLocator serviceLocator) {
return serviceLocator.getService(ExtendedUriInfo.class);
}
示例6
@Provides
@RequestScoped
public OrientGraph provideOrientGraph(ODatabaseDocumentTx dbTx)
{
return new OrientGraph(dbTx, false);
}
示例7
/**
* 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);
}
}