Created
April 18, 2016 14:06
-
-
Save psamsotha/fd77d9bf9f5362453ac0f8bad9fdd751 to your computer and use it in GitHub Desktop.
Inject request scoped service into singleton service/resource. See http://paulsamsotha.blogspot.com/2015/12/injecting-request-scoped-objects-into.html
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
import java.util.logging.Logger; | |
import javax.inject.Inject; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.core.Response; | |
import org.glassfish.hk2.utilities.binding.AbstractBinder; | |
import org.glassfish.jersey.filter.LoggingFilter; | |
import org.glassfish.jersey.process.internal.RequestScoped; | |
import org.glassfish.jersey.server.ResourceConfig; | |
import org.glassfish.jersey.test.JerseyTest; | |
import org.junit.Test; | |
import static org.hamcrest.CoreMatchers.containsString; | |
import static org.junit.Assert.assertThat; | |
/** | |
* See http://paulsamsotha.blogspot.com/2015/12/injecting-request-scoped-objects-into.html | |
* | |
* Run this like any other JUnit test. Only required dependency | |
* | |
* <dependency> | |
* <groupId>org.glassfish.jersey.test-framework.providers</groupId> | |
* <artifactId>jersey-test-framework-provider-grizzly2</artifactId> | |
* <version>2.21</version> | |
* <scope>test</scope> | |
* </dependency> | |
* | |
* @author Paul Samsotha | |
*/ | |
public class RequestScopeProxyTest extends JerseyTest { | |
public static interface Service {} | |
public static class RequestScopedService implements Service {} | |
class Binder extends AbstractBinder { | |
@Override | |
protected void configure() { | |
bind(RequestScopedService.class) | |
.proxy(true) | |
.proxyForSameScope(false) | |
.to(Service.class) | |
.in(RequestScoped.class); | |
} | |
} | |
public static abstract class Resource { | |
@Inject | |
private Service service; | |
@GET | |
public String get() { | |
return service.getClass().getName(); | |
} | |
} | |
@Path("request-scope") | |
public static class RequestScopedResource extends Resource {} | |
@Path("singleton-scope") | |
public static class SingletonResource extends Resource {} | |
@Override | |
public ResourceConfig configure() { | |
return new ResourceConfig() | |
// register as class will default to per-request | |
.register(RequestScopedResource.class) | |
// register as instance will register as singleton | |
.register(new SingletonResource()) | |
.register(new Binder()) | |
.register(new LoggingFilter(Logger.getAnonymousLogger(), true)); | |
} | |
@Test | |
public void singleton_resource_should_get_proxy_service() { | |
final Response response = target("singleton-scope").request().get(); | |
assertThat(response.readEntity(String.class), containsString("Proxy")); | |
} | |
@Test | |
public void request_scope_resource_should_get_actual_service() { | |
final Response response = target("request-scope").request().get(); | |
assertThat(response.readEntity(String.class), containsString("RequestScopedService")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment