下面列出了怎么用com.google.inject.spi.InjectionListener的API类实例代码及写法,或者点击链接到github查看源代码。
private <I> void invokePostConstruct(final TypeEncounter<I> encounter, final Method postConstruct) {
encounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(final I injectee) {
try {
postConstruct.setAccessible(true);
postConstruct.invoke(injectee);
} catch (final IllegalAccessException | InvocationTargetException e) {
if (e.getCause() instanceof UnrecoverableException) {
if (((UnrecoverableException) e.getCause()).isShowException()) {
log.error("An unrecoverable Exception occurred. Exiting HiveMQ", e);
}
System.exit(1);
}
throw new ProvisionException("An error occurred while calling @PostConstruct", e);
}
}
});
}
@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);
}
}
});
}
});
}
private <A extends Annotation> void bind(BindingBuilder<A> binding) {
final Stager<A> stager = binding.stager;
final StageableTypeMapper typeMapper = binding.typeMapper;
bind(type(stager.getStage())).toInstance(stager);
bindListener(binding.typeMatcher, new AbstractMethodTypeListener(asList(stager.getStage())) {
@Override
protected <I> void hear(final Method stageMethod, final TypeLiteral<I> parentType,
final TypeEncounter<I> encounter, final Class<? extends Annotation> annotationType) {
encounter.register((InjectionListener<I>) injectee -> {
Stageable stageable = new StageableMethod(stageMethod, injectee);
stager.register(stageable);
typeMapper.registerType(stageable, parentType);
});
}
});
}
@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);
}
private <I> void addPreDestroyToRegistry(final TypeEncounter<I> encounter, final Method preDestroy, final LifecycleRegistry registry) {
encounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(final I injectee) {
registry.addPreDestroyMethod(preDestroy, injectee);
}
});
}
@Override
public <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
logger.info("Encountered type " + type);
encounter.register((InjectionListener<I>) injectee -> {
logger.info("Injected " + injectee);
});
}
@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);
}
};
}
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);
}
});
}
});
}
private <I> void hear(TypeLiteral<I> type, TypeEncounter<I> encounter) {
InjectionListener<? super I> injectionListener = ignored -> startables.add(type.getRawType().asSubclass(Startable.class));
encounter.register(injectionListener);
}