use of org.glassfish.hk2.api.ServiceLocatorFactory in project jersey by jersey.
the class HK2InjectionManager method createLocator.
/**
* Creates a new {@link ServiceLocator} instance from static {@link ServiceLocatorFactory} and adds the provided parent
* locator if the instance is not null.
*
* @param name name of the injection manager.
* @param parentLocator parent locator, can be {@code null}.
* @return new instance of injection manager.
*/
private static ServiceLocator createLocator(String name, ServiceLocator parentLocator) {
ServiceLocator result = factory.create(name, parentLocator, null, ServiceLocatorFactory.CreatePolicy.DESTROY);
result.setNeutralContextClassLoader(false);
ServiceLocatorUtilities.enablePerThreadScope(result);
return result;
}
use of org.glassfish.hk2.api.ServiceLocatorFactory in project jersey by jersey.
the class Main method main.
public static void main(String[] args) {
ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
ServiceLocator locator = factory.create("myLocator");
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration dc = dcs.createDynamicConfiguration();
AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bind(HelloServiceImpl.class).to(GreetingService.class);
}
};
locator.inject(binder);
binder.bind(dc);
dc.commit();
GreetingWrapper wrapper = new GreetingWrapper(locator);
System.out.println("result: " + wrapper.getInjectedGreeting());
}
use of org.glassfish.hk2.api.ServiceLocatorFactory in project jersey by jersey.
the class MyServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
ServiceLocator locator = factory.create("myLocator");
DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
DynamicConfiguration dc = dcs.createDynamicConfiguration();
AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bind(HelloServiceImpl.class).to(GreetingService.class);
}
};
binder.bind(dc);
dc.commit();
GreetingWrapper wrapper = new GreetingWrapper(locator);
response.getWriter().write("Greeting: " + wrapper.getInjectedGreeting());
locator.shutdown();
}
Aggregations