JavaHK2:使用立即作用域绑定到接口的实现
我正在构建一个将在Jetty服务器上运行的应用程序。我的主类从ResourceConfig扩展到启动“javax.ws.rs.Application”,init参数设置为此类。我还使用ServiceLocator按需返回服务实例;还使用AbstractBinder将类与Singleton&;直接作用域
我的与依赖项注入相关的ResourceConfig类代码如下所示:
public class MyServer extends ResourceConfig {
@Inject
public MyServer() {
packages("My.REST");
}
public static void main(String[] args) {
final Server server = new Server(MythreadPool);
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.addCustomizer(new ForwardedRequestCustomizer());
ServerConnector connector = new ServerConnector(server, new HttpConnectionFactory(httpConfig));
//Code for connector porperties
server.addConnector(connector);
ContextHandlerCollection contexts = new ContextHandlerCollection();
server.setHandler(contexts);
final WebAppContext webApp = setupWebAppContext(contexts, conf);
ServiceLocator serviceLocator = ServiceLocatorFactory.getInstance().create("my-locator");
ServiceLocatorUtilities.enableImmediateScope(serviceLocator);
ServiceLocatorUtilities.bind(
sharedServiceLocator,
new AbstractBinder() {
@Override
protected void configure() {
bind(ClassA.class).to(InterfaceService.class).in(Immediate.class);
}
});
}
webApp.addEventListener(
new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
servletContextEvent
.getServletContext()
.setAttribute(ServletProperties.SERVICE_LOCATOR, serviceLocator);
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {}
});
setupRestApiContextHandler(webApp, conf);
try {
server.start();
} catch (Exception e) {
LOG.error("Error while running jettyServer", e);
System.exit(-1);
}
}
private static WebAppContext setupWebAppContext(ContextHandlerCollection contexts, Configuration conf) {
WebAppContext webApp = new WebAppContext();
webApp.setContextPath(conf.getServerContextPath());
webApp.addServlet(new ServletHolder(new DefaultServlet()), "/*");
contexts.addHandler(webApp);
webApp.addFilter(new FilterHolder(MyFilter.class), "/*", EnumSet.allOf(DispatcherType.class));
webApp.setInitParameter("org.eclipse.jetty.servlet.Default.dirAllowed","true");
return webApp;
}
private static void setupRestApiContextHandler(WebAppContext webapp, Configuration conf) {
final ServletHolder servletHolder =
new ServletHolder(new org.glassfish.jersey.servlet.ServletContainer());
servletHolder.setInitParameter("javax.ws.rs.Application", MyServer.class.getName());
servletHolder.setName("rest");
servletHolder.setForcedPath("rest");
webapp.setSessionHandler(new SessionHandler());
webapp.addServlet(servletHolder, "/api/*");
webapp.setInitParameter("shiroConfigLocations", "ShiroFilePathURI");
webapp
.addFilter(ShiroFilter.class, "/MyRestApiPath/*", EnumSet.allOf(DispatcherType.class))
.setInitParameter("staticSecurityManagerEnabled", "true");
webapp.addEventListener(new EnvironmentLoaderListener());
}
}
我的ClassA看起来像这样:
public class ClassA implements InterfaceService {
@Inject
public ClassA(SomeClass instances){
//initialize some variable
}
}
有一个注入InterfaceService的REST类:
@Path("/my_rest")
@Produces("application/json")
@Singleton
public class RestApi {
private InterfaceService interfaceService;
@Inject
public RestApi(InterfaceService interfaceService) {
this.interfaceService = interfaceService;
}
}
运行此代码时,会出现以下异常:
"javax.servlet.ServletException: A MultiException has 4 exceptions. They are: 1. java.lang.IllegalStateException: Could not find an active context for org.glassfish.hk2.api.Immediate 2. java.lang.IllegalStateException: While attempting to create a service for SystemDescriptor( implementation=ClassA contracts={InterfaceService}"
在MyServer类中,当我将类与Singleton作用域绑定时,它会按预期工作。即
bind(ClassA.class).to(InterfaceService.class).in(Singleton.class);
然后,如果我将RestApi类更新为以下
@Path("/my_rest")
@Produces("application/json")
@Singleton
public class RestApi {
private ClassA classA;
@Inject
public RestApi(ClassA classA) {
this.classA = classA;
}
}
然后一切都按预期进行,范围也很近
对我来说,将作用域更改为Immediate是必需的,因为我需要在服务器启动时在类中执行某些操作。另外,我不能在RestApi中使用ClassA
的引用,因为将来会有InterfaceService
接口的其他实现,所以我需要在RestApi
中对接口进行编码。
关于我需要做什么才能让代码在立即作用域中工作,并将其绑定到InterfaceService
的任何线索
共 (0) 个答案