下面列出了怎么用com.google.inject.Scope的API类实例代码及写法,或者点击链接到github查看源代码。
private boolean isSingleton(Binding<?> b) {
return b.acceptScopingVisitor(new DefaultBindingScopingVisitor<Boolean>() {
@Override
public Boolean visitEagerSingleton() {
return Boolean.TRUE;
}
@Override
public Boolean visitScope(Scope scope) {
return Scopes.SINGLETON.equals(scope);
}
@Override
protected Boolean visitOther() {
return Boolean.FALSE;
}
});
}
@Override
public Class<? extends Annotation> visitScope(final Scope scope) {
Class<? extends Annotation> res = null;
if (scope == Scopes.SINGLETON) {
res = javax.inject.Singleton.class;
}
if (scope == Scopes.NO_SCOPE) {
res = Prototype.class;
}
if (scope == ServletScopes.REQUEST) {
res = RequestScoped.class;
}
if (scope == ServletScopes.SESSION) {
res = SessionScoped.class;
}
// not supporting custom scopes
return res;
}
@Test
public void test_lazy_singleton_scope_is_singleton() throws Exception {
final Scope scope1 = LazySingletonScope.get();
final Scope scope2 = LazySingletonScope.get();
assertSame(scope1, scope2);
}
@Override
protected void configure() {
final Scope lazySingletonScope = LazySingletonScope.get();
bindScope(LazySingleton.class, lazySingletonScope);
}
@Override
public void in(Scope scope) {
applyScoping(Scoping.forInstance(scope));
}
@Override
protected void configure() {
binder().bindListener(Matchers.any(), new ProvisionListener() {
@Override
public <T> void onProvision(ProvisionInvocation<T> provision) {
final Binding<T> binding = provision.getBinding();
logger.debug("provisioning {}", binding.getKey().getTypeLiteral());
final T provisioned = provision.provision();
if (provisioned != null && Service.class.isAssignableFrom(provisioned.getClass())) {
final AtomicBoolean start = new AtomicBoolean(false);
binding.acceptScopingVisitor(new DefaultBindingScopingVisitor<T>() {
@Override
public T visitEagerSingleton() {
start.set(true);
return super.visitEagerSingleton();
}
@Override
public T visitScope(Scope scope) {
if (scope instanceof SingletonScope) {
start.set(true);
}
return super.visitScope(scope);
}
});
if (start.get()) {
serviceList.push(binding.getKey().getTypeLiteral().getRawType());
try {
logger.debug("starting {}", binding.getKey().getTypeLiteral());
((Service) provisioned).start();
} catch (Exception e) {
throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
}
}
});
}
@Override
public Boolean visitScope(Scope scope) {
return scope == Scopes.SINGLETON;
}
/**
* Perform binding to interface only if implementation type is not null.
*/
private <T> void bindIfNotNull(final Class<T> interfaceType, @Nullable final Class<? extends T> implementationType, final Scope scope) {
if (implementationType != null) {
bind(interfaceType).to(implementationType).in(scope);
}
}
ConfigModule(Flags flags, Config config, Path outputDir, Scope documentationScope) {
this.flags = flags;
this.config = config;
this.outputDir = outputDir;
this.documentationScope = documentationScope;
}
/**
* Returns the LazySingletonScope
*
* @return the LazySingletonScope
*/
public static Scope get() {
return instance;
}
default void installIn(Scope scope, Module... modules) { installIn(Scoping.forInstance(scope), modules); }