这是 Java SE 应用程序。我想独家使用Guice进行注射。我已经读过有关HK2活页夹的内容,而我只是使用Guice。我熟悉Guice并经常使用它,但是我无法弄清楚从哪里开始这一点,几乎所有的Google-fu都会导致基于容器的球衣服务器。我对Grizzly也不感兴趣。
那么我该如何实现这一点,以便我可以使用Guice来满足我的注射需求?
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.roycat.rnotes.ws.guice.MainModule;
import com.roycat.rnotes.ws.server.CustomResourceConfig;
public class StartRestServer {
public static void main(String[] args) {
String base = "localhost";
Integer port = 8090;
if (args.length == 1) {
base = args[0];
} else if (args.length == 2) {
base = args[0];
port = Integer.parseInt(args[1]);
}
URI baseUri = UriBuilder.fromUri("http://" + base
+ "/").port(port).build();
ResourceConfig config = new CustomResourceConfig();
JdkHttpServerFactory.createHttpServer(baseUri, config);
System.out.println("Server running on " + baseUri.toString()
+ "...");
}
}
答案 0 :(得分:2)
您可以使用HK2 guice-bridge将Guice服务连接到HK2。
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>guice-bridge</artifactId>
<version>2.4.0-b31</version>
</dependency>
请注意,<version>
应与您的Jersey版本提取的HK2版本相匹配。只要看看泽西岛拉入的HK2罐子。它应该有版本。在我的情况下,我使用的是泽西2.22.1,其中拉入HK2 2.4.0-b31
然后你需要对桥进行编码。我刚刚在Feature
@Priority(1)
public class GuiceFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
ServiceLocator locator = ServiceLocatorProvider.getServiceLocator(context);
GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
Injector injector = Guice.createInjector(new MyGuiceModule());
GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
return true;
}
}
然后使用ResourceConfig
config.register(GuiceFeature.class);
现在您应该能够将Guice服务注入您的Jersey组件。
这是一个完整的工作测试
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.sun.net.httpserver.HttpServer;
import java.net.URI;
import java.util.logging.Logger;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.core.Response;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.ServiceLocatorProvider;
import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.jvnet.hk2.guice.bridge.api.GuiceBridge;
import org.jvnet.hk2.guice.bridge.api.GuiceIntoHK2Bridge;
import static org.junit.Assert.assertEquals;
public class JdkHttpGuiceTest {
@Priority(1)
public static class GuiceFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
ServiceLocator locator = ServiceLocatorProvider.getServiceLocator(context);
GuiceBridge.getGuiceBridge().initializeGuiceBridge(locator);
Injector injector = Guice.createInjector(new EncoderModule());
GuiceIntoHK2Bridge guiceBridge = locator.getService(GuiceIntoHK2Bridge.class);
guiceBridge.bridgeGuiceInjector(injector);
return true;
}
}
public static interface Encoder {
String encode(String value);
}
public static class UppercaseEncoder implements Encoder {
@Override
public String encode(String value) {
return value.toUpperCase();
}
}
static class EncoderModule extends AbstractModule {
@Override
protected void configure() {
bind(Encoder.class).to(UppercaseEncoder.class);
}
}
@Path("test")
public static class TestResource {
@Inject
private Encoder encoder;
@GET
public String get(@QueryParam("s") String s) {
return encoder.encode(s);
}
}
public static class AppConfig extends ResourceConfig {
public AppConfig() {
register(TestResource.class);
register(GuiceFeature.class);
register(new LoggingFilter(Logger.getAnonymousLogger(), true));
}
}
private HttpServer server;
@Before
public void setUp() {
server = JdkHttpServerFactory.createHttpServer(
URI.create("http://localhost:8080/"), new AppConfig());
}
@After
public void tearDown() {
server.stop(1);
server = null;
}
@Test
public void injectionShouldWork() {
final Response response = ClientBuilder.newClient().target("http://localhost:8080/test")
.queryParam("s", "hello")
.request().get();
assertEquals(200, response.getStatus());
assertEquals("HELLO", response.readEntity(String.class));
}
}
使用的依赖关系
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>guice-bridge</artifactId>
<version>2.4.0-b31</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.22.2</version>
</dependency>