Bha*_*tel 6 java dependency-injection glassfish jersey hk2
我正在构建一个将在Jetty Server上运行的应用程序。我的主类从ResourceConfig扩展为启动,将“ javax.ws.rs.Application” init参数设置为此类。我还使用ServiceLocator来按需返回服务实例。还使用AbstractBinder将类与Singleton&Instant范围绑定。
与依赖项注入相关的我的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());
}
}
Run Code Online (Sandbox Code Playgroud)
我的ClassA看起来像这样:
public class ClassA implements InterfaceService {
@Inject
public ClassA(SomeClass instances){
//initialize some variable
}
}
Run Code Online (Sandbox Code Playgroud)
有一个REST类,其中注入了InterfaceService:
@Path("/my_rest")
@Produces("application/json")
@Singleton
public class RestApi {
private InterfaceService interfaceService;
@Inject
public RestApi(InterfaceService interfaceService) {
this.interfaceService = interfaceService;
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,出现以下异常:
“ javax.servlet.ServletException:MultiException有4个异常。它们是:1. java.lang.IllegalStateException:找不到org.glassfish.hk2.api.Immediate的活动上下文。java.lang.IllegalStateException:尝试时为SystemDescriptor创建服务(实现= ClassA合同= {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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,一切都会按预期进行,并且立即生效。
对于我来说,需要将范围更改为“立即”,因为我需要在服务器启动时在类中进行某些操作。另外,ClassA由于将来还会有该InterfaceService接口的其他实现,因此我无法在RestApi中使用的引用,因此我需要在中编码该接口RestApi。我需要做什么才能使代码在立即作用域中工作并将其绑定到InterfaceService
| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |