Guice-scopes
提供:Dev Guides
Google Guice-スコープ
Guiceは、デフォルトの動作として値を提供するたびに新しいインスタンスを返します。 スコープを介して構成できます。 Guiceがサポートするさまざまなスコープは-
- @ Singleton -アプリケーションの存続期間の単一インスタンス。 @Singletonオブジェクトはスレッドセーフである必要があります。
- @ SessionScoped -Webアプリケーションの特定のセッションの単一インスタンス。 @SessionScopedオブジェクトはスレッドセーフである必要があります。
- @ RequestScoped -Webアプリケーションの特定のリクエストの単一インスタンス。 @RequestScopedオブジェクトはスレッドセーフである必要はありません。
スコープの適用
次の方法でスコープを適用できます-
クラスレベルで
@Singleton
class SpellCheckerImpl implements SpellChecker {
public SpellCheckerImpl(){}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
}
構成レベルで
bind(SpellChecker.class).to(SpellCheckerImpl.class).in(Singleton.class);
メソッドレベルで
@Provides @Singleton
public SpellChecker provideSpellChecker() {
String dbUrl = "jdbc:mysql://localhost:5326/emp";
String user = "user";
int timeout = 100;
SpellChecker SpellChecker = new SpellCheckerImpl(dbUrl, user, timeout);
return SpellChecker;
}
例
動作中のクラスレベルのスコープを見てみましょう。
@Singletonアノテーションの結果
GuiceTesterという名前のJavaクラスを作成します。
*GuiceTester.java*
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;
public class GuiceTester {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TextEditorModule());
SpellChecker spellChecker = new SpellCheckerImpl();
injector.injectMembers(spellChecker);
TextEditor editor = injector.getInstance(TextEditor.class);
System.out.println(editor.getSpellCheckerId());
TextEditor editor1 = injector.getInstance(TextEditor.class);
System.out.println(editor1.getSpellCheckerId());
}
}
class TextEditor {
private SpellChecker spellChecker;
@Inject
public void setSpellChecker(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
public TextEditor() { }
public void makeSpellCheck() {
spellChecker.checkSpelling();
}
public double getSpellCheckerId() {
return spellChecker.getId();
}
}
//Binding Module
class TextEditorModule extends AbstractModule {
@Override
protected void configure() {
bind(SpellChecker.class).to(SpellCheckerImpl.class);
}
}
interface SpellChecker {
public double getId();
public void checkSpelling();
}
@Singleton
class SpellCheckerImpl implements SpellChecker {
double id;
public SpellCheckerImpl() {
id = Math.random();
}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
@Override
public double getId() {
return id;
}
}
ファイルをコンパイルして実行すると、同じ番号の次の出力が表示されます。
0.3055839187063575
0.3055839187063575
@Singletonアノテーションなしの結果
GuiceTesterという名前のJavaクラスを作成します。
*GuiceTester.java*
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
public class GuiceTester {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new TextEditorModule());
SpellChecker spellChecker = new SpellCheckerImpl();
injector.injectMembers(spellChecker);
TextEditor editor = injector.getInstance(TextEditor.class);
System.out.println(editor.getSpellCheckerId());
TextEditor editor1 = injector.getInstance(TextEditor.class);
System.out.println(editor1.getSpellCheckerId());
}
}
class TextEditor {
private SpellChecker spellChecker;
@Inject
public void setSpellChecker(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
public TextEditor() { }
public void makeSpellCheck() {
spellChecker.checkSpelling();
}
public double getSpellCheckerId() {
return spellChecker.getId();
}
}
//Binding Module
class TextEditorModule extends AbstractModule {
@Override
protected void configure() {
bind(SpellChecker.class).to(SpellCheckerImpl.class);
}
}
interface SpellChecker {
public double getId();
public void checkSpelling();
}
class SpellCheckerImpl implements SpellChecker {
double id;
public SpellCheckerImpl() {
id = Math.random();
}
@Override
public void checkSpelling() {
System.out.println("Inside checkSpelling." );
}
@Override
public double getId() {
return id;
}
}
出力
次に、ファイルをコンパイルして実行します。 あなたは異なる番号で次の出力を見ることができます-
0.556007079571739
0.22095011760351602