下面列出了com.google.inject.matcher.AbstractMatcher#com.google.inject.spi.TypeListener 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void configure() {
final boolean newRegistry = lifecycleRegistry.compareAndSet(null, new LifecycleRegistry());
bind(LifecycleRegistry.class).toInstance(lifecycleRegistry.get());
if (newRegistry) {
bind(LifecycleShutdownRegistration.class).asEagerSingleton();
}
bindListener(Matchers.any(), new TypeListener() {
@Override
public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) {
executePostConstruct(encounter, type.getRawType(), lifecycleRegistry.get());
}
});
}
@Override
protected void configure() {
registerConfigFiles(this.configFiles);
//init system runtime eagerly
bind(SystemRuntime.IRuntime.class).to(BaseRuntime.class).asEagerSingleton();
bind(IResourceClientLibrary.class).to(ResourceClientLibrary.class).in(Scopes.SINGLETON);
bind(IBotStoreClientLibrary.class).to(BotStoreClientLibrary.class).in(Scopes.SINGLETON);
bind(IPackageStoreClientLibrary.class).to(PackageStoreClientLibrary.class).in(Scopes.SINGLETON);
bind(IPackageStoreService.class).to(PackageStoreService.class).in(Scopes.SINGLETON);
bind(IBotStoreService.class).to(BotStoreService.class).in(Scopes.SINGLETON);
bind(IBotFactory.class).to(BotFactory.class).in(Scopes.SINGLETON);
bind(IPackageFactory.class).to(PackageFactory.class).in(Scopes.SINGLETON);
bind(IAutoBotDeployment.class).to(AutoBotDeployment.class).in(Scopes.SINGLETON);
//call init method of system runtime after creation
bindListener(HasInitMethod.INSTANCE, new TypeListener() {
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
encounter.register(InitInvoker.INSTANCE);
}
});
}
@Override
protected void configure() {
final Destroyer destroyer = new Destroyer(errorHandler);
bind(Destroyer.class).toInstance(destroyer);
bindListener(
Matchers.any(),
new TypeListener() {
@Override
public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter) {
encounter.register(
new InjectionListener<T>() {
@Override
public void afterInjection(T injectee) {
final Method[] methods = get(injectee.getClass(), annotationType);
if (methods.length > 0) {
// copy array when pass it outside
final Method[] copy = new Method[methods.length];
System.arraycopy(methods, 0, copy, 0, methods.length);
destroyer.add(injectee, copy);
}
}
});
}
});
}
@Override
protected void configure() {
bind(Boolean.class).annotatedWith(Names.named("testing")).toInstance(true);
bindListener(InstanceOfMatcher.createFor(WebApplication.class), new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) {
final Provider<Injector> injectorProvider = encounter.getProvider(Injector.class);
encounter.register((InjectionListener<Object>) injected -> {
WebApplication app = (WebApplication) injected;
app.getComponentInstantiationListeners().add(new GuiceComponentInjector(app, injectorProvider.get()));
});
}
});
bind(OrienteerTester.class).asEagerSingleton();
Provider<OrienteerTester> provider = binder().getProvider(OrienteerTester.class);
bind(WicketTester.class).toProvider(provider);
bind(WicketOrientDbTester.class).toProvider(provider);
}
@Override
public void configure(Binder binder)
{
binder.disableCircularProxies();
binder.bindListener(Matchers.any(), new TypeListener()
{
@Override
public <T> void hear(TypeLiteral<T> type, TypeEncounter<T> encounter)
{
encounter.register(new InjectionListener<T>()
{
@Override
public void afterInjection(T obj)
{
ServerLifeCycleManager initialized = lifeCycleManagerRef.get();
if (initialized == null) {
earlyInjected.add(obj);
}
else {
try {
initialized.manageInstance(obj);
}
catch (Exception e) {
// really nothing we can do here
throw new Error(e);
}
}
}
});
}
});
}
private TypeListener createTypeListener() {
return new TypeListener() {
@Override
public <I> void hear(final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) {
typeEncounter.register((InjectionListener<I>) LoggerInjector::inject);
}
};
}
@Override
public void configure(final Binder binder) {
binder.bindListener(Matchers.any(), new TypeListener() {
@Override
public <I> void hear(final TypeLiteral<I> type, final TypeEncounter<I> encounter) {
final List<Field> fields = fields(Lists.newArrayList(), type.getRawType());
fields.stream().filter(field -> field.isAnnotationPresent(FieldInject.class)).forEach(field -> {
final FieldInject inject = field.getAnnotation(FieldInject.class);
encounter.register(ReflectUtils.newInstance(inject.value(), field));
});
}
});
}
private void messageBusRegisterRule() {
// This rule registers all objects created by Guice with the message bus.
// As a result, all instances created by Guice can receive messages without explicit
// subscription.
bindListener(Matchers.any(), new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
typeEncounter.register(new InjectionListener<I>() {
public void afterInjection(I i) {
messageBus.subscribe(i);
}
});
}
});
}
@Override
protected void configure() {
LOG.info("Loading PaasModule");
bind(EventBus.class).toInstance(eventBus);
bindListener(Matchers.any(), new TypeListener() {
public <I> void hear(TypeLiteral<I> typeLiteral, TypeEncounter<I> typeEncounter) {
typeEncounter.register(new InjectionListener<I>() {
public void afterInjection(I i) {
eventBus.register(i);
}
});
}
});
bind(TaskManager.class).to(InlineTaskManager.class);
// Constants
bind(String.class).annotatedWith(Names.named("namespace")).toInstance("com.netflix.pass.");
bind(String.class).annotatedWith(Names.named("appname" )).toInstance("paas");
bind(AbstractConfiguration.class).toInstance(ConfigurationManager.getConfigInstance());
// Configuration
bind(PaasConfiguration.class).to(ArchaeusPaasConfiguration.class).in(Scopes.SINGLETON);
// Stuff
bind(ScheduledExecutorService.class).annotatedWith(Names.named("tasks")).toInstance(Executors.newScheduledThreadPool(10));
bind(DaoProvider.class).in(Scopes.SINGLETON);
// Rest resources
bind(DataResource.class).in(Scopes.SINGLETON);
bind(SchemaAdminResource.class).to(JerseySchemaAdminResourceImpl.class).in(Scopes.SINGLETON);
bind(SchemaService.class).to(DaoSchemaService.class).in(Scopes.SINGLETON);
}
@Override
protected void configure() {
bindListener(ClassToTypeLiteralMatcherAdapter.adapt(Matchers.annotatedWith(JmxBean.class)), new TypeListener() {
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
final Provider<JmxExporter> exporter = encounter.getProvider(JmxExporter.class);
encounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(I injectee) {
exporter.get().register(injectee);
}
});
}
});
}
@Override
protected void configure() {
final InjectionLogger listener = new InjectionLogger(logger);
bindListener(Matchers.any(), (TypeListener) listener);
bindListener(Matchers.any(), (ProvisionListener) listener);
}