本文整理汇总了Java中com.google.inject.Scope类的典型用法代码示例。如果您正苦于以下问题:Java Scope类的具体用法?Java Scope怎么用?Java Scope使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Scope类属于com.google.inject包,在下文中一共展示了Scope类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyScoping
import com.google.inject.Scope; //导入依赖的package包/类
protected void applyScoping(Binding<?> binding, final ScopedBindingBuilder scopedBindingBuilder) {
binding.acceptScopingVisitor(new BindingScopingVisitor<Void>() {
public Void visitEagerSingleton() {
if (scopedBindingBuilder != null) {
scopedBindingBuilder.asEagerSingleton();
}
return null;
}
public Void visitScope(Scope scope) {
scopedBindingBuilder.in(scope);
return null;
}
public Void visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
scopedBindingBuilder.in(scopeAnnotation);
return null;
}
public Void visitNoScoping() {
// do nothing
return null;
}
});
}
示例2: scoped
import com.google.inject.Scope; //导入依赖的package包/类
private static String scoped(final Binding<?> binding, final String statement) {
return binding.acceptScopingVisitor(new BindingScopingVisitor<String>() {
@Override
public String visitEagerSingleton() {
if (binding instanceof InstanceBinding<?>) {
return statement;
}
throw new DeguicifierException();
}
@Override
public String visitScope(Scope scope) {
return scopeFieldName(scope) + ".scope(null, " + guicify(statement) + ")";
}
@Override
public String visitScopeAnnotation(Class<? extends Annotation> scopeAnnotation) {
throw new DeguicifierException();
}
@Override
public String visitNoScoping() {
return statement;
}
});
}
示例3: visit
import com.google.inject.Scope; //导入依赖的package包/类
@Override public Boolean visit(ScopeBinding command) {
Scope scope = command.getScope();
Class<? extends Annotation> annotationType = command.getAnnotationType();
if (!Annotations.isScopeAnnotation(annotationType)) {
errors.missingScopeAnnotation(annotationType);
// Go ahead and bind anyway so we don't get collateral errors.
}
if (!Annotations.isRetainedAtRuntime(annotationType)) {
errors.missingRuntimeRetention(annotationType);
// Go ahead and bind anyway so we don't get collateral errors.
}
ScopeBinding existing = injector.state.getScopeBinding(checkNotNull(annotationType, "annotation type"));
if (existing != null) {
errors.duplicateScopes(existing, annotationType, scope);
} else {
checkNotNull(scope, "scope");
injector.state.putScopeBinding(annotationType, command);
}
return true;
}
示例4: forInstance
import com.google.inject.Scope; //导入依赖的package包/类
public static Scoping forInstance(final Scope scope) {
if (scope == Scopes.SINGLETON) {
return SINGLETON_INSTANCE;
}
return new Scoping() {
@Override public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
return visitor.visitScope(scope);
}
@Override public Scope getScopeInstance() {
return scope;
}
@Override public String toString() {
return scope.toString();
}
@Override public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
scopedBindingBuilder.in(scope);
}
};
}
示例5: testOverrideScopeAnnotation
import com.google.inject.Scope; //导入依赖的package包/类
public void testOverrideScopeAnnotation() {
final Scope scope = new Scope() {
public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
throw new AssertionError("Should not be called");
}
};
final SingleUseScope replacementScope = new SingleUseScope();
Module original = new AbstractModule() {
@Override protected void configure() {
bindScope(TestScopeAnnotation.class, scope);
bind(Date.class).in(TestScopeAnnotation.class);
}
};
Module replacements = new AbstractModule() {
@Override protected void configure() {
bindScope(TestScopeAnnotation.class, replacementScope);
}
};
Injector injector = createInjector(Modules.override(original).with(replacements));
injector.getInstance(Date.class);
assertTrue(replacementScope.used);
}
示例6: visit
import com.google.inject.Scope; //导入依赖的package包/类
@Override public Boolean visit(ScopeBinding command) {
Scope scope = command.getScope();
Class<? extends Annotation> annotationType = command.getAnnotationType();
if (!Annotations.isScopeAnnotation(annotationType)) {
errors.withSource(annotationType).missingScopeAnnotation();
// Go ahead and bind anyway so we don't get collateral errors.
}
if (!Annotations.isRetainedAtRuntime(annotationType)) {
errors.withSource(annotationType)
.missingRuntimeRetention(command.getSource());
// Go ahead and bind anyway so we don't get collateral errors.
}
Scope existing = injector.state.getScope(checkNotNull(annotationType, "annotation type"));
if (existing != null) {
errors.duplicateScopes(existing, annotationType, scope);
} else {
injector.state.putAnnotation(annotationType, checkNotNull(scope, "scope"));
}
return true;
}
示例7: forInstance
import com.google.inject.Scope; //导入依赖的package包/类
public static Scoping forInstance(final Scope scope) {
if (scope == Scopes.SINGLETON) {
return SINGLETON_INSTANCE;
}
return new Scoping() {
public <V> V acceptVisitor(BindingScopingVisitor<V> visitor) {
return visitor.visitScope(scope);
}
@Override public Scope getScopeInstance() {
return scope;
}
@Override public String toString() {
return scope.toString();
}
public void applyTo(ScopedBindingBuilder scopedBindingBuilder) {
scopedBindingBuilder.in(scope);
}
};
}
示例8: OutOfScopeException
import com.google.inject.Scope; //导入依赖的package包/类
public OutOfScopeException(Scope scope, Key<?> key, Throwable cause)
{
super(String.format(
"Not in scope %s for key %s: caused by %s",
scope, key, cause
), cause);
}
示例9: OutOfScopeException
import com.google.inject.Scope; //导入依赖的package包/类
public OutOfScopeException(Scope scope, Key<?> key, Throwable cause)
{
super(String.format(
"Not in scope %s for key %s: caused by %s",
scope, key, cause
), cause);
}
示例10: bindScopeInstance
import com.google.inject.Scope; //导入依赖的package包/类
@SuppressWarnings({"unchecked"})
public static void bindScopeInstance(Binder binder, Scope scope, Class<? extends Annotation> annotationType)
{
requireNonNull(scope);
binder.bindScope(annotationType, scope);
binder.bind((Class) scope.getClass()).toInstance(scope);
}
示例11: getScopeAnnotation
import com.google.inject.Scope; //导入依赖的package包/类
/**
* Returns the scope annotation for the given binding or null if there is no
* scope
*/
public static Class<? extends Annotation> getScopeAnnotation(
Binding<?> binding) {
Class<? extends Annotation> scopeAnnotation = null;
if (binding instanceof BindingImpl) {
BindingImpl bindingImpl = (BindingImpl) binding;
Scoping scoping = bindingImpl.getScoping();
if (scoping != null) {
scopeAnnotation = scoping.getScopeAnnotation();
// TODO not sure why we need this hack???
if (scopeAnnotation == null) {
Scope scope = scoping.getScopeInstance();
if (scope instanceof HasScopeAnnotation) {
HasScopeAnnotation hasScopeAnnotation = (HasScopeAnnotation) scope;
scopeAnnotation = hasScopeAnnotation
.getScopeAnnotation();
}
if (scopeAnnotation == null
&& (scoping == Scoping.EAGER_SINGLETON
|| scoping == Scoping.SINGLETON_ANNOTATION || scoping == Scoping.SINGLETON_INSTANCE)) {
scopeAnnotation = Singleton.class;
}
}
}
}
return scopeAnnotation;
}
示例12: RequestScopePropagator
import com.google.inject.Scope; //导入依赖的package包/类
protected RequestScopePropagator(
Scope scope,
ThreadLocalRequestContext local,
Provider<RequestScopedReviewDbProvider> dbProviderProvider) {
this.scope = scope;
this.local = local;
this.dbProviderProvider = dbProviderProvider;
}
示例13: ThreadLocalRequestScopePropagator
import com.google.inject.Scope; //导入依赖的package包/类
protected ThreadLocalRequestScopePropagator(
Scope scope,
ThreadLocal<C> threadLocal,
ThreadLocalRequestContext local,
Provider<RequestScopedReviewDbProvider> dbProviderProvider) {
super(scope, local, dbProviderProvider);
this.threadLocal = threadLocal;
}
示例14: createScopeMap
import com.google.inject.Scope; //导入依赖的package包/类
@Override
public Map<Scope, ComponentScope> createScopeMap() {
Map<Scope, ComponentScope> m = super.createScopeMap();
m.put(ServletScopes.REQUEST, ComponentScope.PerRequest);
return m;
}
示例15: scopeInstance
import com.google.inject.Scope; //导入依赖的package包/类
private static String scopeInstance(Scope scope) {
if (scope == Scopes.SINGLETON) {
return Scopes.class.getCanonicalName() + ".SINGLETON";
} else {
return "new " + scope.getClass().getCanonicalName() + "()";
}
}