I am building an application which will run on Jetty Server. My main class is extended from ResourceConfig to start the "javax.ws.rs.Application" init parameter is set to this class. I have also used ServiceLocator to return the service instances on demand; also used AbstractBinder to bind the class with the Singleton & Immediate scope.
My ResourceConfig class code related to dependency injection looks like this:
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());
}
}
My ClassA looks like this:
public class ClassA implements InterfaceService {
@Inject
public ClassA(SomeClass instances){
//initialize some variable
}
}
There is a REST class in which the InterfaceService is injected:
@Path("/my_rest")
@Produces("application/json")
@Singleton
public class RestApi {
private InterfaceService interfaceService;
@Inject
public RestApi(InterfaceService interfaceService) {
this.interfaceService = interfaceService;
}
}
When I run this code I get this exception:
"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}"
In MyServer class when I bind the class with the Singleton scope it works as expected. i.e.
bind(ClassA.class).to(InterfaceService.class).in(Singleton.class);
Then if I update my RestApi class to below
@Path("/my_rest")
@Produces("application/json")
@Singleton
public class RestApi {
private ClassA classA;
@Inject
public RestApi(ClassA classA) {
this.classA = classA;
}
}
Then everything works as expected, with the immediate scope as well.
For me changing the scope to Immediate is required since I need to do certain operations in the class on server startup. Also, I cannot use the reference of ClassA
in the RestApi since, there will be other implementations of the InterfaceService
interface in the future, so I need to code to the interface in RestApi
.
Any clue on what I need to do to get the code working with immediate scope, with binding it to the InterfaceService