use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.
the class InjectionManagerTest method testRegisterBinder.
@Test
public void testRegisterBinder() {
AbstractBinder binder = new AbstractBinder() {
@Override
protected void configure() {
bindAsContract(EnglishGreeting.class);
}
};
InjectionManager injectionManager = new HK2InjectionManager();
injectionManager.initialize();
injectionManager.register(binder);
assertNotNull(injectionManager.getInstance(EnglishGreeting.class));
}
use of org.glassfish.hk2.utilities.Binder 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.utilities.Binder 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();
}
use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.
the class ActiveBindingBindingTest method testReq.
@Test
@Ignore("At the time of ignoring this test, ResourceConfig does not support HK2 Binder registering.")
public void testReq() throws Exception {
// bootstrap the test application
ResourceConfig myConfig = new ResourceConfig();
myConfig.register(ReqResource.class);
myConfig.register(SingletonResource.class);
final MyRequestDataDescriptor activeDescriptor = new MyRequestDataDescriptor();
myConfig.register(new AbstractBinder() {
@Override
protected void configure() {
addActiveDescriptor(activeDescriptor);
}
});
initiateWebApplication(myConfig);
activeDescriptor.injectionManager = app().getInjectionManager();
// end bootstrap
String response;
response = getResponseEntity("/req");
assertThat(response, containsString(REQUEST_TAG));
assertThat(response, not(containsString(PROXY_TAG)));
response = getResponseEntity("/req/param");
assertThat(response, containsString(REQUEST_TAG));
assertThat(response, not(containsString(PROXY_TAG)));
response = getResponseEntity("/singleton");
assertThat(response, containsString(SINGLETON_TAG));
assertThat(response, containsString(PROXY_TAG));
response = getResponseEntity("/req");
assertThat(response, containsString(REQUEST_TAG));
assertThat(response, not(containsString(PROXY_TAG)));
response = getResponseEntity("/singleton");
assertThat(response, containsString(SINGLETON_TAG));
assertThat(response, containsString(PROXY_TAG));
response = getResponseEntity("/singleton/param");
assertThat(response, containsString(SINGLETON_TAG));
assertThat(response, not(containsString(PROXY_TAG)));
response = getResponseEntity("/singleton/param");
assertThat(response, containsString(SINGLETON_TAG));
assertThat(response, not(containsString(PROXY_TAG)));
}
use of org.glassfish.hk2.utilities.Binder in project jersey by jersey.
the class CdiComponentProvider method bindHk2ClassAnalyzer.
private void bindHk2ClassAnalyzer() {
ClassAnalyzer defaultClassAnalyzer = injectionManager.getInstance(ClassAnalyzer.class, ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME);
int skippedElements = methodsToSkip.size() + fieldsToSkip.size();
ClassAnalyzer customizedClassAnalyzer = skippedElements > 0 ? new InjecteeSkippingAnalyzer(defaultClassAnalyzer, methodsToSkip, fieldsToSkip) : defaultClassAnalyzer;
Binder binder = new AbstractBinder() {
@Override
protected void configure() {
bind(customizedClassAnalyzer).analyzeWith(ClassAnalyzer.DEFAULT_IMPLEMENTATION_NAME).to(ClassAnalyzer.class).named(CDI_CLASS_ANALYZER);
}
};
injectionManager.register(binder);
}
Aggregations