你可以使用
HK2
迪。您可以做的是创建一个独立的
ServiceLocator
并使用
Jersey property
.
public static void main(String... args) {
SourceHandler source = new SparkHandler(inputSource);
ServiceLocator locator = ServiceLocatorUtilities.bind(new AbstractBinder() {
@Override
protected void configure() {
bind(source).to(SourceHandler.class);
}
});
ServletHolder serHol = ctx.addServlet(ServletContainer.class, "/rest/*");
serHol.setInitParameter(ServletProperties.SERVICE_LOCATOR, locator);
}
那你就可以
@Inject
这个
SourceHandler
任何你需要的地方
@Path("whatever")
public class Resource {
@Inject
private SourceHandler sourceHandler;
}